From f8eca5fa03760aa85baedf9917ab0ea40aba7cf2 Mon Sep 17 00:00:00 2001 From: Emir Taletovic <5982655+etaletovic@users.noreply.github.com> Date: Wed, 16 Mar 2022 13:45:47 -0700 Subject: [PATCH] Calibre plugin - Series/Tag browser updated to show back button. (#8869) * Calibre plugin - Series/Tag browser updated to show back button. * Handling page number * Few updates to search logic: - Moved "find books" option from CalibreSearch:find(option) into CalibreSearch:browse(option) function. This way all search options are handled in one place only. - Menu is created only once and it is in CalibreSearch:browse(option) function. This is where it is also invoked with UIManager:show(w). Switching between different menu content (tags/series/books) is done using CalibreSearch:switchResults function which will internally call Menu:switchItemTable. Previously menu was being instantiated in 2 places depending on are we searching books or series/tags - Return arrow navigation: Border around Menu is gone to give space for back arrow. Navigation seems to be working fine so far but will give it some time to test in case I find any other bugs --- plugins/calibre.koplugin/search.lua | 120 +++++++++++++--------------- 1 file changed, 57 insertions(+), 63 deletions(-) diff --git a/plugins/calibre.koplugin/search.lua b/plugins/calibre.koplugin/search.lua index 076045009..f68804197 100644 --- a/plugins/calibre.koplugin/search.lua +++ b/plugins/calibre.koplugin/search.lua @@ -3,7 +3,6 @@ --]] local CalibreMetadata = require("metadata") -local CenterContainer = require("ui/widget/container/centercontainer") local ConfirmBox = require("ui/widget/confirmbox") local DataStorage = require("datastorage") local Device = require("device") @@ -14,7 +13,6 @@ local InputContainer = require("ui/widget/container/inputcontainer") local Menu = require("ui/widget/menu") local Persist = require("persist") local Screen = require("device").screen -local Size = require("ui/size") local TimeVal = require("ui/timeval") local UIManager = require("ui/uimanager") local lfs = require("libs/libkoreader-lfs") @@ -328,13 +326,7 @@ function CalibreSearch:find(option) -- measure time elapsed searching local start = TimeVal:now() - if option == "find" then - local books = self:findBooks(self.search_value) - local result = self:bookCatalog(books) - self:showresults(result) - else - self:browse(option, 1) - end + self:browse(option) logger.info(string.format("search done in %.3f milliseconds (%s, %s, %s, %s, %s)", TimeVal:getDurationMs(start), option == "find" and "books" or option, @@ -383,28 +375,19 @@ function CalibreSearch:findBooks(query) end -- browse tags or series -function CalibreSearch:browse(option, run, chosen) - local menu_container = CenterContainer:new{ - dimen = Screen:getSize(), - } - self.search_menu = Menu:new{ - width = Screen:getWidth() - (Size.margin.fullscreen_popout * 2), - height = Screen:getHeight() - (Size.margin.fullscreen_popout * 2), - show_parent = menu_container, - onMenuHold = self.onMenuHold, - _manager = self, - } - table.insert(menu_container, self.search_menu) - self.search_menu.close_callback = function() - UIManager:close(menu_container) - end - if run == 1 then - local menu_entries = {} - local search_value - if self.search_value ~= "" then - search_value = self.search_value - end - local name, source +function CalibreSearch:browse(option) + local search_value + if self.search_value ~= "" then + search_value = self.search_value + end + local name + local menu_entries = {} + + if option == "find" then + name = _("Books") + menu_entries = self:bookCatalog(self:findBooks(self.search_value)) + else + local source if option == "tags" then name = _("Browse by tags") source = searchByTag(self.books, search_value, self.case_insensitive) @@ -416,51 +399,62 @@ function CalibreSearch:browse(option, run, chosen) local entry = {} entry.text = string.format("%s (%d)", k, v) entry.callback = function() - self:browse(option, 2, k) + self:expandTagOrSeries(option,k) end table.insert(menu_entries, entry) end - table.sort(menu_entries, function(v1,v2) return v1.text < v2.text end) - self.search_menu:switchItemTable(name, menu_entries) - UIManager:show(menu_container) - else - local results - if option == "tags" then - results = getBooksByTag(self.books, chosen) - elseif option == "series" then - results = getBooksBySeries(self.books, chosen) - end - if results then - local catalog = self:bookCatalog(results, option) - self:showresults(catalog, chosen) + end + + self.search_menu = self.search_menu or Menu:new{ + width = Screen:getWidth(), + height = Screen:getHeight(), + parent = nil, + is_borderless = true, + onMenuHold = self.onMenuHold, + } + self.search_menu.paths = {} + self.search_menu.onReturn = function () + local path_entry = table.remove(self.search_menu.paths) + local page = path_entry.page or 1 + if #self.search_menu.paths < 1 then + -- If nothing is left in paths we switch to original items and title + self.search_menu.paths = {} + self:switchResults(menu_entries, name, false, page) end end + self:switchResults(menu_entries, name) + UIManager:show(self.search_menu) end --- show search results -function CalibreSearch:showresults(t, title) +function CalibreSearch:expandTagOrSeries(option, chosen_item) + local results + + if option == "tags" then + results = getBooksByTag(self.books, chosen_item) + elseif option == "series" then + results = getBooksBySeries(self.books, chosen_item) + end + if results then + local catalog = self:bookCatalog(results, option) + self:switchResults(catalog, chosen_item, true) + end +end + +-- update search results +function CalibreSearch:switchResults(t, title, is_child, page) if not title then title = _("Search results") end - local menu_container = CenterContainer:new{ - dimen = Screen:getSize(), - } - self.search_menu = Menu:new{ - width = Screen:getWidth() - (Size.margin.fullscreen_popout * 2), - height = Screen:getHeight() - (Size.margin.fullscreen_popout * 2), - show_parent = menu_container, - onMenuHold = self.onMenuHold, - _manager = self, - } - table.insert(menu_container, self.search_menu) - self.search_menu.close_callback = function() - UIManager:close(menu_container) - end table.sort(t, function(v1,v2) return v1.text < v2.text end) - self.search_menu:switchItemTable(title, t) - UIManager:show(menu_container) + + if is_child then + local path_entry = {} + path_entry.page = (self.search_menu.perpage or 1) * (self.search_menu.page or 1) + table.insert(self.search_menu.paths, path_entry) + end + self.search_menu:switchItemTable(title, t, page or 1) end -- prompt the user for a library scan