12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import { book_item_data } from '../data/data';
- import { config } from '../config/config';
- import { util } from '../framework/util';
- export class ReadHistoryManager {
- private static book_list:book_item_data[] = null
- private static max_count = 20
- // 获取书列表
- public static getBookList():book_item_data[] {
- if(ReadHistoryManager.book_list) {
- return ReadHistoryManager.book_list
- }
- ReadHistoryManager.book_list = []
- let string = util.getStorage(config.storage_key.READ_HISTORY)
- if(string) {
- ReadHistoryManager.book_list = JSON.parse(string)
- }
- return ReadHistoryManager.book_list
- }
-
- // 保存本地书列表
- private static saveLocalBookList() {
- util.setStorage(config.storage_key.READ_HISTORY, JSON.stringify(ReadHistoryManager.book_list))
- }
- // 检查在阅读历史
- public static async checkBookOnReadHistory(book_id:number, cb:Function) {
- let index = -1
- let book_item = null
- for (let i = 0; i < ReadHistoryManager.getBookList().length; i++) {
- let element = ReadHistoryManager.getBookList()[i]
- if(element.book_id == book_id) {
- index = i
- book_item = element
- break
- }
- }
- cb && cb(index,book_item)
- }
-
- // 添加书
- public static async addBook(book_data:book_item_data, cb:Function) {
- let read_hisroty_time = util.timeFormat(new Date().getTime())
- book_data.read_hisroty_time = read_hisroty_time
-
- ReadHistoryManager.checkBookOnReadHistory(book_data.book_id, (index:number)=>{
- if(index>-1) {
- ReadHistoryManager.getBookList().splice(index,1)
- } else {
- if(ReadHistoryManager.getBookList().length >= ReadHistoryManager.max_count) {
- ReadHistoryManager.getBookList().pop()
- }
- }
- ReadHistoryManager.getBookList().unshift(book_data)
- // console.log('Readhistory-getBookList()=',ReadHistoryManager.getBookList())
- ReadHistoryManager.saveLocalBookList()
- cb && cb()
- })
- }
-
- // 删除书
- public static deleteBook(book_id_list:number[]) {
- for (let i = 0; i < book_id_list.length; i++) {
- let element = book_id_list[i]
- for (let j = 0; j < ReadHistoryManager.getBookList().length; j++) {
- let j_element = ReadHistoryManager.getBookList()[j]
- if(element==j_element.book_id) {
- ReadHistoryManager.getBookList().splice(j,1)
- }
- }
- }
- ReadHistoryManager.saveLocalBookList()
- }
-
- // 重置数据
- public static resetData(book_list:book_item_data[]) {
- ReadHistoryManager.book_list = book_list
- ReadHistoryManager.saveLocalBookList()
- }
- }
|