From 7fc803ffee08a9eb7155ea49d09f64fe61f67833 Mon Sep 17 00:00:00 2001 From: weijiuqiao <59040746+weijiuqiao@users.noreply.github.com> Date: Thu, 13 Oct 2022 05:45:29 +0800 Subject: [PATCH] Vocabulary builder: supports review in reverse order (#9605) --- plugins/vocabbuilder.koplugin/db.lua | 33 +++++++++++-------- plugins/vocabbuilder.koplugin/main.lua | 44 +++++++++++++++++++------- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/plugins/vocabbuilder.koplugin/db.lua b/plugins/vocabbuilder.koplugin/db.lua index 24da2a029..f871b6ebc 100644 --- a/plugins/vocabbuilder.koplugin/db.lua +++ b/plugins/vocabbuilder.koplugin/db.lua @@ -36,15 +36,13 @@ function VocabularyBuilder:init() VocabularyBuilder:createDB() end -function VocabularyBuilder:selectCount(conn) - if conn then - return tonumber(conn:rowexec("SELECT count(0) FROM vocabulary INNER JOIN title ON filter=true AND title_id=id;")) - else - local db_conn = SQ3.open(db_location) - local count = tonumber(db_conn:rowexec("SELECT count(0) FROM vocabulary INNER JOIN title ON filter=true AND title_id=id;")) - db_conn:close() - return count - end +function VocabularyBuilder:selectCount(vocab_widget) + local db_conn = SQ3.open(db_location) + local where_clause = vocab_widget:check_reverse() and " WHERE due_time < " .. vocab_widget.reload_time or "" + local sql = "SELECT count(0) FROM vocabulary INNER JOIN title ON filter=true AND title_id=id" .. where_clause .. ";" + local count = tonumber(db_conn:rowexec(sql)) + db_conn:close() + return count end function VocabularyBuilder:createDB() @@ -132,9 +130,17 @@ function VocabularyBuilder:insertLookupData(db_conn) end end -function VocabularyBuilder:_select_items(items, start_idx) +function VocabularyBuilder:_select_items(items, start_idx, reload_time) local conn = SQ3.open(db_location) - local sql = string.format("SELECT * FROM vocabulary INNER JOIN title ON title_id = title.id AND filter = true ORDER BY due_time limit %d OFFSET %d;", 32, start_idx-1) + local sql + if not reload_time then + sql = string.format("SELECT * FROM vocabulary INNER JOIN title ON title_id = title.id AND filter = true ORDER BY due_time limit %d OFFSET %d;", 32, start_idx-1) + else + sql = string.format([[SELECT * FROM vocabulary INNER JOIN title + ON title_id = title.id AND filter = true + WHERE due_time < ]] .. reload_time .. + " ORDER BY due_time desc limit %d OFFSET %d;", 32, start_idx-1) + end local results = conn:exec(sql) conn:close() @@ -170,7 +176,8 @@ function VocabularyBuilder:_select_items(items, start_idx) end -function VocabularyBuilder:select_items(items, start_idx, end_idx) +function VocabularyBuilder:select_items(vocab_widget, start_idx, end_idx) + local items = vocab_widget.item_table local start_cursor if #items == 0 then start_cursor = 0 @@ -184,7 +191,7 @@ function VocabularyBuilder:select_items(items, start_idx, end_idx) end if not start_cursor then return end - self:_select_items(items, start_cursor) + self:_select_items(items, start_cursor, vocab_widget:check_reverse() and vocab_widget.reload_time) end diff --git a/plugins/vocabbuilder.koplugin/main.lua b/plugins/vocabbuilder.koplugin/main.lua index 4f5c67fbc..f852b5ebe 100644 --- a/plugins/vocabbuilder.koplugin/main.lua +++ b/plugins/vocabbuilder.koplugin/main.lua @@ -134,6 +134,10 @@ local function onShowFilter(widget) UIManager:show(sort_widget) end +local function saveSettings() + G_reader_settings:saveSetting("vocabulary_builder", settings) +end + --[[-- Menu dialogue widget --]]-- @@ -224,6 +228,16 @@ function MenuDialog:init() end } + local reverse_button = { + text = settings.reverse and _("Reverse order") or _("Reverse order and show only reviewable"), + callback = function() + self:onClose() + settings.reverse = not settings.reverse + saveSettings() + self.show_parent:reloadItems() + end + } + local edit_button = { text = self.is_edit_mode and _("Resume") or _("Quick deletion"), callback = function() @@ -266,6 +280,7 @@ function MenuDialog:init() width = width, buttons = { {filter_button}, + {reverse_button}, {edit_button}, {reset_button}, {clean_button} @@ -356,12 +371,12 @@ end function MenuDialog:onChangeContextStatus(args, position) settings.with_context = position == 2 - G_reader_settings:saveSetting("vocabulary_builder", settings) + saveSettings() end function MenuDialog:onChangeEnableStatus(args, position) settings.enabled = position == 2 - G_reader_settings:saveSetting("vocabulary_builder", settings) + saveSettings() resetButtonOnLookupWindow() end @@ -998,6 +1013,7 @@ local VocabularyBuilderWidget = FocusManager:extend{ } function VocabularyBuilderWidget:init() + self.item_table = self:reload_items_callback() self.layout = {} self.dimen = Geom:new{ @@ -1233,7 +1249,7 @@ function VocabularyBuilderWidget:_populateItems() end if self.select_items_callback then - self.select_items_callback(self.item_table ,idx_offset, page_last) + self:select_items_callback(idx_offset, page_last) end for idx = idx_offset + 1, page_last do @@ -1264,7 +1280,9 @@ function VocabularyBuilderWidget:_populateItems() end if self.pages == 0 then local has_filtered_book = DB:hasFilteredBook() - self.footer_page:setText(has_filtered_book and _("Filter in effect") or _("No items"), self.footer_center_width) + local text = has_filtered_book and _("Filter in effect") or + self:check_reverse() and _("No reviewable items") or _("No items") + self.footer_page:setText(text, self.footer_center_width) self.footer_first_up:hide() self.footer_last_down:hide() self.footer_left:hide() @@ -1344,9 +1362,13 @@ function VocabularyBuilderWidget:showMenu() }) end +function VocabularyBuilderWidget:check_reverse() + return settings.reverse +end + function VocabularyBuilderWidget:reloadItems() DB:batchUpdateItems(self.item_table) - self.item_table = self.reload_items_callback() + self.item_table = self:reload_items_callback() self.pages = math.ceil(#self.item_table / self.items_per_page) self:goToPage(1) end @@ -1427,9 +1449,10 @@ function VocabBuilder:addToMainMenu(menu_items) menu_items.vocabbuilder = { text = _("Vocabulary builder"), callback = function() - local reload_items = function() + local reload_items = function(builder_widget) + builder_widget.reload_time = os.time() local vocab_items = {} - for i = 1, DB:selectCount() do + for i = 1, DB:selectCount(builder_widget) do table.insert(vocab_items, { callback = function(item) -- custom button table @@ -1475,7 +1498,7 @@ function VocabBuilder:addToMainMenu(menu_items) end end - self.builder_widget.current_lookup_word = item.word + builder_widget.current_lookup_word = item.word self.ui:handleEvent(Event:new("LookupWord", item.word, true, nil, nil, nil, tweak_buttons_func)) end }) @@ -1485,9 +1508,8 @@ function VocabBuilder:addToMainMenu(menu_items) self.builder_widget = VocabularyBuilderWidget:new{ title = _("Vocabulary builder"), - item_table = reload_items(), - select_items_callback = function(items, start_idx, end_idx) - DB:select_items(items, start_idx, end_idx) + select_items_callback = function(obj, start_idx, end_idx) + DB:select_items(obj, start_idx, end_idx) end, reload_items_callback = reload_items }