diff --git a/plugins/vocabbuilder.koplugin/db.lua b/plugins/vocabbuilder.koplugin/db.lua index f871b6ebc..19163407f 100644 --- a/plugins/vocabbuilder.koplugin/db.lua +++ b/plugins/vocabbuilder.koplugin/db.lua @@ -38,7 +38,7 @@ 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 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() @@ -138,7 +138,7 @@ function VocabularyBuilder:_select_items(items, start_idx, reload_time) else sql = string.format([[SELECT * FROM vocabulary INNER JOIN title ON title_id = title.id AND filter = true - WHERE due_time < ]] .. reload_time .. + WHERE due_time <= ]] .. reload_time .. " ORDER BY due_time desc limit %d OFFSET %d;", 32, start_idx-1) end diff --git a/plugins/vocabbuilder.koplugin/main.lua b/plugins/vocabbuilder.koplugin/main.lua index 2fcd63039..cddb79ed6 100644 --- a/plugins/vocabbuilder.koplugin/main.lua +++ b/plugins/vocabbuilder.koplugin/main.lua @@ -14,6 +14,7 @@ local CenterContainer = require("ui/widget/container/centercontainer") local ConfirmBox = require("ui/widget/confirmbox") local Device = require("device") local DictQuickLookUp = require("ui/widget/dictquicklookup") +local Dispatcher = require("dispatcher") local Event = require("ui/event") local Font = require("ui/font") local FocusManager = require("ui/widget/focusmanager") @@ -1339,6 +1340,7 @@ function VocabularyBuilderWidget:resetItems() } end end + self.reload_time = os.time() self:_populateItems() end @@ -1443,16 +1445,28 @@ local VocabBuilder = WidgetContainer:extend{ function VocabBuilder:init() self.ui.menu:registerToMainMenu(self) + self:onDispatcherRegisterActions() end function VocabBuilder:addToMainMenu(menu_items) menu_items.vocabbuilder = { text = _("Vocabulary builder"), callback = function() - local reload_items = function(builder_widget) - builder_widget.reload_time = os.time() + self:onShowVocabBuilder() + end + } +end + +function VocabBuilder:setupWidget() + if self.widget then + self.widget:reloadItems() + else + -- We initiate the widget with proper + -- callback definition for reload_items + local reload_items = function(widget) + widget.reload_time = os.time() local vocab_items = {} - for i = 1, DB:selectCount(builder_widget) do + for i = 1, DB:selectCount(widget) do table.insert(vocab_items, { callback = function(item) -- custom button table @@ -1468,7 +1482,7 @@ function VocabBuilder:addToMainMenu(menu_items) id = "got_it", text = _("Got it"), callback = function() - self.builder_widget:gotItFromDict(item.word) + self.widget:gotItFromDict(item.word) UIManager:sendEvent(Event:new("Close")) end } @@ -1482,7 +1496,7 @@ function VocabBuilder:addToMainMenu(menu_items) id = "forgot", text = _("Forgot"), callback = function() - self.builder_widget:forgotFromDict(item.word) + self.widget:forgotFromDict(item.word) UIManager:sendEvent(Event:new("Close")) end } @@ -1498,31 +1512,38 @@ function VocabBuilder:addToMainMenu(menu_items) end end - builder_widget.current_lookup_word = item.word + widget.current_lookup_word = item.word self.ui:handleEvent(Event:new("LookupWord", item.word, true, nil, nil, nil, tweak_buttons_func)) end }) end - return vocab_items - end + return vocab_items + end - self.builder_widget = VocabularyBuilderWidget:new{ - title = _("Vocabulary builder"), - select_items_callback = function(obj, start_idx, end_idx) - DB:select_items(obj, start_idx, end_idx) - end, - reload_items_callback = reload_items - } + self.widget = VocabularyBuilderWidget:new{ + title = _("Vocabulary builder"), + select_items_callback = function(obj, start_idx, end_idx) + DB:select_items(obj, start_idx, end_idx) + end, + reload_items_callback = reload_items + } + end +end - UIManager:show(self.builder_widget) - end - } +function VocabBuilder:onDispatcherRegisterActions() + Dispatcher:registerAction("show_vocab_builder", + {category="none", event="ShowVocabBuilder", title=_("Open vocabulary builder"), general=true, separator=true}) +end + +function VocabBuilder:onShowVocabBuilder() + self:setupWidget() + UIManager:show(self.widget) end -- Event sent by readerdictionary "WordLookedUp" function VocabBuilder:onWordLookedUp(word, title, is_manual) if not settings.enabled and not is_manual then return end - if self.builder_widget and self.builder_widget.current_lookup_word == word then return true end + if self.widget and self.widget.current_lookup_word == word then return true end local prev_context local next_context if settings.with_context and self.ui.highlight then