Vocabulary builder: add "Open..." to Dispatcher (#9634)

So it can be called from a gesture.
Also accommodate reset all in reversed order.
pull/9680/head
weijiuqiao 2 years ago committed by GitHub
parent edf7cc9a61
commit 6bd7dfdf41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -38,7 +38,7 @@ end
function VocabularyBuilder:selectCount(vocab_widget) function VocabularyBuilder:selectCount(vocab_widget)
local db_conn = SQ3.open(db_location) 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 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)) local count = tonumber(db_conn:rowexec(sql))
db_conn:close() db_conn:close()
@ -138,7 +138,7 @@ function VocabularyBuilder:_select_items(items, start_idx, reload_time)
else else
sql = string.format([[SELECT * FROM vocabulary INNER JOIN title sql = string.format([[SELECT * FROM vocabulary INNER JOIN title
ON title_id = title.id AND filter = true 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) " ORDER BY due_time desc limit %d OFFSET %d;", 32, start_idx-1)
end end

@ -14,6 +14,7 @@ local CenterContainer = require("ui/widget/container/centercontainer")
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device") local Device = require("device")
local DictQuickLookUp = require("ui/widget/dictquicklookup") local DictQuickLookUp = require("ui/widget/dictquicklookup")
local Dispatcher = require("dispatcher")
local Event = require("ui/event") local Event = require("ui/event")
local Font = require("ui/font") local Font = require("ui/font")
local FocusManager = require("ui/widget/focusmanager") local FocusManager = require("ui/widget/focusmanager")
@ -1339,6 +1340,7 @@ function VocabularyBuilderWidget:resetItems()
} }
end end
end end
self.reload_time = os.time()
self:_populateItems() self:_populateItems()
end end
@ -1443,16 +1445,28 @@ local VocabBuilder = WidgetContainer:extend{
function VocabBuilder:init() function VocabBuilder:init()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
self:onDispatcherRegisterActions()
end end
function VocabBuilder:addToMainMenu(menu_items) function VocabBuilder:addToMainMenu(menu_items)
menu_items.vocabbuilder = { menu_items.vocabbuilder = {
text = _("Vocabulary builder"), text = _("Vocabulary builder"),
callback = function() callback = function()
local reload_items = function(builder_widget) self:onShowVocabBuilder()
builder_widget.reload_time = os.time() 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 = {} local vocab_items = {}
for i = 1, DB:selectCount(builder_widget) do for i = 1, DB:selectCount(widget) do
table.insert(vocab_items, { table.insert(vocab_items, {
callback = function(item) callback = function(item)
-- custom button table -- custom button table
@ -1468,7 +1482,7 @@ function VocabBuilder:addToMainMenu(menu_items)
id = "got_it", id = "got_it",
text = _("Got it"), text = _("Got it"),
callback = function() callback = function()
self.builder_widget:gotItFromDict(item.word) self.widget:gotItFromDict(item.word)
UIManager:sendEvent(Event:new("Close")) UIManager:sendEvent(Event:new("Close"))
end end
} }
@ -1482,7 +1496,7 @@ function VocabBuilder:addToMainMenu(menu_items)
id = "forgot", id = "forgot",
text = _("Forgot"), text = _("Forgot"),
callback = function() callback = function()
self.builder_widget:forgotFromDict(item.word) self.widget:forgotFromDict(item.word)
UIManager:sendEvent(Event:new("Close")) UIManager:sendEvent(Event:new("Close"))
end end
} }
@ -1498,31 +1512,38 @@ function VocabBuilder:addToMainMenu(menu_items)
end end
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)) self.ui:handleEvent(Event:new("LookupWord", item.word, true, nil, nil, nil, tweak_buttons_func))
end end
}) })
end end
return vocab_items return vocab_items
end end
self.builder_widget = VocabularyBuilderWidget:new{ self.widget = VocabularyBuilderWidget:new{
title = _("Vocabulary builder"), title = _("Vocabulary builder"),
select_items_callback = function(obj, start_idx, end_idx) select_items_callback = function(obj, start_idx, end_idx)
DB:select_items(obj, start_idx, end_idx) DB:select_items(obj, start_idx, end_idx)
end, end,
reload_items_callback = reload_items reload_items_callback = reload_items
} }
end
end
UIManager:show(self.builder_widget) function VocabBuilder:onDispatcherRegisterActions()
end 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 end
-- Event sent by readerdictionary "WordLookedUp" -- Event sent by readerdictionary "WordLookedUp"
function VocabBuilder:onWordLookedUp(word, title, is_manual) function VocabBuilder:onWordLookedUp(word, title, is_manual)
if not settings.enabled and not is_manual then return end 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 prev_context
local next_context local next_context
if settings.with_context and self.ui.highlight then if settings.with_context and self.ui.highlight then

Loading…
Cancel
Save