Vocabulary builder: supports review in reverse order (#9605)

reviewable/pr9631/r1
weijiuqiao 2 years ago committed by GitHub
parent ae8156ff9e
commit 7fc803ffee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,15 +36,13 @@ function VocabularyBuilder:init()
VocabularyBuilder:createDB() VocabularyBuilder:createDB()
end end
function VocabularyBuilder:selectCount(conn) function VocabularyBuilder:selectCount(vocab_widget)
if conn then local db_conn = SQ3.open(db_location)
return tonumber(conn:rowexec("SELECT count(0) FROM vocabulary INNER JOIN title ON filter=true AND title_id=id;")) local where_clause = vocab_widget:check_reverse() and " WHERE due_time < " .. vocab_widget.reload_time or ""
else local sql = "SELECT count(0) FROM vocabulary INNER JOIN title ON filter=true AND title_id=id" .. where_clause .. ";"
local db_conn = SQ3.open(db_location) local count = tonumber(db_conn:rowexec(sql))
local count = tonumber(db_conn:rowexec("SELECT count(0) FROM vocabulary INNER JOIN title ON filter=true AND title_id=id;")) db_conn:close()
db_conn:close() return count
return count
end
end end
function VocabularyBuilder:createDB() function VocabularyBuilder:createDB()
@ -132,9 +130,17 @@ function VocabularyBuilder:insertLookupData(db_conn)
end end
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 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) local results = conn:exec(sql)
conn:close() conn:close()
@ -170,7 +176,8 @@ function VocabularyBuilder:_select_items(items, start_idx)
end 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 local start_cursor
if #items == 0 then if #items == 0 then
start_cursor = 0 start_cursor = 0
@ -184,7 +191,7 @@ function VocabularyBuilder:select_items(items, start_idx, end_idx)
end end
if not start_cursor then return 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 end

@ -134,6 +134,10 @@ local function onShowFilter(widget)
UIManager:show(sort_widget) UIManager:show(sort_widget)
end end
local function saveSettings()
G_reader_settings:saveSetting("vocabulary_builder", settings)
end
--[[-- --[[--
Menu dialogue widget Menu dialogue widget
--]]-- --]]--
@ -224,6 +228,16 @@ function MenuDialog:init()
end 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 = { local edit_button = {
text = self.is_edit_mode and _("Resume") or _("Quick deletion"), text = self.is_edit_mode and _("Resume") or _("Quick deletion"),
callback = function() callback = function()
@ -266,6 +280,7 @@ function MenuDialog:init()
width = width, width = width,
buttons = { buttons = {
{filter_button}, {filter_button},
{reverse_button},
{edit_button}, {edit_button},
{reset_button}, {reset_button},
{clean_button} {clean_button}
@ -356,12 +371,12 @@ end
function MenuDialog:onChangeContextStatus(args, position) function MenuDialog:onChangeContextStatus(args, position)
settings.with_context = position == 2 settings.with_context = position == 2
G_reader_settings:saveSetting("vocabulary_builder", settings) saveSettings()
end end
function MenuDialog:onChangeEnableStatus(args, position) function MenuDialog:onChangeEnableStatus(args, position)
settings.enabled = position == 2 settings.enabled = position == 2
G_reader_settings:saveSetting("vocabulary_builder", settings) saveSettings()
resetButtonOnLookupWindow() resetButtonOnLookupWindow()
end end
@ -998,6 +1013,7 @@ local VocabularyBuilderWidget = FocusManager:extend{
} }
function VocabularyBuilderWidget:init() function VocabularyBuilderWidget:init()
self.item_table = self:reload_items_callback()
self.layout = {} self.layout = {}
self.dimen = Geom:new{ self.dimen = Geom:new{
@ -1233,7 +1249,7 @@ function VocabularyBuilderWidget:_populateItems()
end end
if self.select_items_callback then 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 end
for idx = idx_offset + 1, page_last do for idx = idx_offset + 1, page_last do
@ -1264,7 +1280,9 @@ function VocabularyBuilderWidget:_populateItems()
end end
if self.pages == 0 then if self.pages == 0 then
local has_filtered_book = DB:hasFilteredBook() 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_first_up:hide()
self.footer_last_down:hide() self.footer_last_down:hide()
self.footer_left:hide() self.footer_left:hide()
@ -1344,9 +1362,13 @@ function VocabularyBuilderWidget:showMenu()
}) })
end end
function VocabularyBuilderWidget:check_reverse()
return settings.reverse
end
function VocabularyBuilderWidget:reloadItems() function VocabularyBuilderWidget:reloadItems()
DB:batchUpdateItems(self.item_table) 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.pages = math.ceil(#self.item_table / self.items_per_page)
self:goToPage(1) self:goToPage(1)
end end
@ -1427,9 +1449,10 @@ 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() local reload_items = function(builder_widget)
builder_widget.reload_time = os.time()
local vocab_items = {} local vocab_items = {}
for i = 1, DB:selectCount() do for i = 1, DB:selectCount(builder_widget) do
table.insert(vocab_items, { table.insert(vocab_items, {
callback = function(item) callback = function(item)
-- custom button table -- custom button table
@ -1475,7 +1498,7 @@ function VocabBuilder:addToMainMenu(menu_items)
end end
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)) self.ui:handleEvent(Event:new("LookupWord", item.word, true, nil, nil, nil, tweak_buttons_func))
end end
}) })
@ -1485,9 +1508,8 @@ function VocabBuilder:addToMainMenu(menu_items)
self.builder_widget = VocabularyBuilderWidget:new{ self.builder_widget = VocabularyBuilderWidget:new{
title = _("Vocabulary builder"), title = _("Vocabulary builder"),
item_table = reload_items(), select_items_callback = function(obj, start_idx, end_idx)
select_items_callback = function(items, start_idx, end_idx) DB:select_items(obj, start_idx, end_idx)
DB:select_items(items, start_idx, end_idx)
end, end,
reload_items_callback = reload_items reload_items_callback = reload_items
} }

Loading…
Cancel
Save