readHistoryManager.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { book_item_data } from '../data/data';
  2. import { config } from '../config/config';
  3. import { util } from '../framework/util';
  4. export class ReadHistoryManager {
  5. private static book_list:book_item_data[] = null
  6. private static max_count = 20
  7. // 获取书列表
  8. public static getBookList():book_item_data[] {
  9. if(ReadHistoryManager.book_list) {
  10. return ReadHistoryManager.book_list
  11. }
  12. ReadHistoryManager.book_list = []
  13. let string = util.getStorage(config.storage_key.READ_HISTORY)
  14. if(string) {
  15. ReadHistoryManager.book_list = JSON.parse(string)
  16. }
  17. return ReadHistoryManager.book_list
  18. }
  19. // 保存本地书列表
  20. private static saveLocalBookList() {
  21. util.setStorage(config.storage_key.READ_HISTORY, JSON.stringify(ReadHistoryManager.book_list))
  22. }
  23. // 检查在阅读历史
  24. public static async checkBookOnReadHistory(book_id:number, cb:Function) {
  25. let index = -1
  26. let book_item = null
  27. for (let i = 0; i < ReadHistoryManager.getBookList().length; i++) {
  28. let element = ReadHistoryManager.getBookList()[i]
  29. if(element.book_id == book_id) {
  30. index = i
  31. book_item = element
  32. break
  33. }
  34. }
  35. cb && cb(index,book_item)
  36. }
  37. // 添加书
  38. public static async addBook(book_data:book_item_data, cb:Function) {
  39. let read_hisroty_time = util.timeFormat(new Date().getTime())
  40. book_data.read_hisroty_time = read_hisroty_time
  41. ReadHistoryManager.checkBookOnReadHistory(book_data.book_id, (index:number)=>{
  42. if(index>-1) {
  43. ReadHistoryManager.getBookList().splice(index,1)
  44. } else {
  45. if(ReadHistoryManager.getBookList().length >= ReadHistoryManager.max_count) {
  46. ReadHistoryManager.getBookList().pop()
  47. }
  48. }
  49. ReadHistoryManager.getBookList().unshift(book_data)
  50. // console.log('Readhistory-getBookList()=',ReadHistoryManager.getBookList())
  51. ReadHistoryManager.saveLocalBookList()
  52. cb && cb()
  53. })
  54. }
  55. // 删除书
  56. public static deleteBook(book_id_list:number[]) {
  57. for (let i = 0; i < book_id_list.length; i++) {
  58. let element = book_id_list[i]
  59. for (let j = 0; j < ReadHistoryManager.getBookList().length; j++) {
  60. let j_element = ReadHistoryManager.getBookList()[j]
  61. if(element==j_element.book_id) {
  62. ReadHistoryManager.getBookList().splice(j,1)
  63. }
  64. }
  65. }
  66. ReadHistoryManager.saveLocalBookList()
  67. }
  68. // 重置数据
  69. public static resetData(book_list:book_item_data[]) {
  70. ReadHistoryManager.book_list = book_list
  71. ReadHistoryManager.saveLocalBookList()
  72. }
  73. }