From 73b4ff84b7b131c0f41b0276c3700c0afd43c446 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 17 Feb 2017 19:32:52 +0530 Subject: [PATCH] Start work on currently reading section of home page --- src/pyj/book_list/home.pyj | 9 +++++++++ src/pyj/read_book/db.pyj | 34 +++++++++++++++++++++++++++------- src/pyj/read_book/ui.pyj | 2 +- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/pyj/book_list/home.pyj b/src/pyj/book_list/home.pyj index 2f3a7abf35..c1ae1dc291 100644 --- a/src/pyj/book_list/home.pyj +++ b/src/pyj/book_list/home.pyj @@ -14,12 +14,21 @@ from book_list.ui import set_default_panel_handler, show_panel from book_list.router import update_window_title +def show_recent_stage2(books): + container = document.getElementById(this) + if not container: + return + for book in books: + print(book.key, book.metadata.title, book.last_read) + + def show_recent(): container = this db = get_db() if not db.initialized: conditional_timeout(container.id, 5, show_recent) return + db.get_recently_read_books(show_recent_stage2.bind(container.id)) def init(container_id): diff --git a/src/pyj/read_book/db.pyj b/src/pyj/read_book/db.pyj index 6901a8f991..5fe9b88614 100644 --- a/src/pyj/read_book/db.pyj +++ b/src/pyj/read_book/db.pyj @@ -6,12 +6,10 @@ from gettext import gettext as _ from encodings import base64encode, base64decode from modals import error_dialog -from book_list.library_data import current_library_id from book_list.router import is_reading_book -current_library_id -def upgrade_schema(idb, old_version, new_version): +def upgrade_schema(idb, old_version, new_version, transaction): print('upgrade_schema:', old_version, new_version) if not idb.objectStoreNames.contains('books'): idb.createObjectStore('books', {'keyPath':'key'}) @@ -22,6 +20,11 @@ def upgrade_schema(idb, old_version, new_version): if not idb.objectStoreNames.contains('objects'): idb.createObjectStore('objects', {'keyPath':'key'}) + # Create indices + books_store = transaction.objectStore('books') + if not books_store.indexNames.contains('last_read_index'): + books_store.createIndex('last_read_index', 'last_read') + def file_store_name(book, name): return book.book_hash + ' ' + name @@ -33,7 +36,7 @@ def get_error_details(event): desc = desc.errorCode DB_NAME = 'calibre-books-db-testing' # TODO: Remove test suffix and change version back to 1 -DB_VERSION = 2 +DB_VERSION = 3 class DB: @@ -60,7 +63,7 @@ class DB: request = window.indexedDB.open(DB_NAME, DB_VERSION) request.onupgradeneeded = def(event): - upgrade_schema(event.target.result, event.oldVersion, event.newVersion) + upgrade_schema(event.target.result, event.oldVersion, event.newVersion, event.target.transaction) request.onblocked = def(event): self.initialize_error_msg = _('Please close all other browser tabs with calibre open') @@ -126,12 +129,12 @@ class DB: req.onerror = def(event): self.display_error(error_msg, event) - def get_book(self, book_id, fmt, metadata, proceed): + def get_book(self, library_id, book_id, fmt, metadata, proceed): fmt = fmt.toUpperCase() # The key has to be a JavaScript array as otherwise it cannot be stored # into indexed db, because the RapydScript list has properties that # refer to non-serializable objects like functions. - key = v'[current_library_id(), book_id, fmt]' + key = v'[library_id, book_id, fmt]' self.do_op(['books'], key, _('Failed to read from the books database'), def(result): proceed(result or { 'key':key, @@ -262,6 +265,23 @@ class DB: else: proceed(data) + def get_recently_read_books(self, proceed, limit): + limit = limit or 3 + c = self.idb.transaction(['books'], 'readonly').objectStore('books').index('last_read_index').openCursor(None, 'prev') + books = v'[]' + c.onerror = def(event): + err = _('Failed to read recent books from local storage') + self.display_error(err, event) + c.onsuccess = def (ev): + cursor = ev.target.result + if cursor: + books.push(cursor.value) + if books.length >= limit or not cursor: + proceed(books) + return + if cursor: + cursor.continue() + def delete_book(self, book, proceed): c = self.idb.transaction(['books', 'files'], 'readwrite') files = c.objectStore('files') diff --git a/src/pyj/read_book/ui.pyj b/src/pyj/read_book/ui.pyj index a1cf258baa..3c094a003d 100644 --- a/src/pyj/read_book/ui.pyj +++ b/src/pyj/read_book/ui.pyj @@ -145,7 +145,7 @@ class ReadUI: if jstype(self.db) is 'string': self.show_error(_('Cannot read book'), self.db) return - self.db.get_book(book_id, fmt, metadata, self.got_book.bind(self, force_reload)) + self.db.get_book(current_library_id(), book_id, fmt, metadata, self.got_book.bind(self, force_reload)) def got_book(self, force_reload, book): if not book.manifest or book.manifest.version is not RENDER_VERSION or not book.is_complete: