History: remember status filter, show filtered count in title (#9822)

reviewable/pr9857/r1
melyux 2 years ago committed by GitHub
parent fdbba7b020
commit cc53ceb039
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,7 @@ local FileManagerHistory = WidgetContainer:extend{
hist_menu_title = _("History"), hist_menu_title = _("History"),
} }
local status_text = { local filter_text = {
all = C_("Book status filter", "All"), all = C_("Book status filter", "All"),
reading = C_("Book status filter", "Reading"), reading = C_("Book status filter", "Reading"),
abandoned = C_("Book status filter", "On hold"), abandoned = C_("Book status filter", "On hold"),
@ -41,6 +41,32 @@ function FileManagerHistory:addToMainMenu(menu_items)
} }
end end
function FileManagerHistory:fetchStatuses(count)
local status
for _, v in ipairs(require("readhistory").hist) do
if v.dim then
status = "deleted"
else
if DocSettings:hasSidecarFile(v.file) then
local docinfo = DocSettings:open(v.file) -- no io handles created, do not close
if docinfo.data.summary and docinfo.data.summary.status
and docinfo.data.summary.status ~= "" then
status = docinfo.data.summary.status
else
status = "reading"
end
else
status = "new"
end
end
v.status = status
if count then
self.count[status] = self.count[status] + 1
end
end
self.statuses_fetched = true
end
function FileManagerHistory:updateItemTable() function FileManagerHistory:updateItemTable()
-- try to stay on current page -- try to stay on current page
local select_number = nil local select_number = nil
@ -51,7 +77,7 @@ function FileManagerHistory:updateItemTable()
reading = 0, abandoned = 0, complete = 0, deleted = 0, new = 0, } reading = 0, abandoned = 0, complete = 0, deleted = 0, new = 0, }
local item_table = {} local item_table = {}
for _, v in ipairs(require("readhistory").hist) do for _, v in ipairs(require("readhistory").hist) do
if not self.filter or v.status == self.filter then if self.filter == "all" or v.status == self.filter then
table.insert(item_table, v) table.insert(item_table, v)
end end
if self.statuses_fetched then if self.statuses_fetched then
@ -59,8 +85,8 @@ function FileManagerHistory:updateItemTable()
end end
end end
local title = self.hist_menu_title local title = self.hist_menu_title
if self.filter then if self.filter ~= "all" then
title = title .. " (" .. status_text[self.filter] .. ")" title = title .. " (" .. filter_text[self.filter] .. ": " .. self.count[self.filter] .. ")"
end end
self.hist_menu:switchItemTable(title, item_table, select_number) self.hist_menu:switchItemTable(title, item_table, select_number)
end end
@ -86,6 +112,11 @@ function FileManagerHistory:onMenuHold(item)
ok_callback = function() ok_callback = function()
filemanagerutil.purgeSettings(item.file) filemanagerutil.purgeSettings(item.file)
require("readhistory"):fileSettingsPurged(item.file) require("readhistory"):fileSettingsPurged(item.file)
if self._manager.filter ~= "all" then
self._manager:fetchStatuses(false)
else
self._manager.statuses_fetched = false
end
self._manager:updateItemTable() self._manager:updateItemTable()
UIManager:close(self.histfile_dialog) UIManager:close(self.histfile_dialog)
end, end,
@ -171,11 +202,15 @@ function FileManagerHistory:onShowHist()
_manager = self, _manager = self,
} }
self.filter = G_reader_settings:readSetting("history_filter", "all")
if self.filter ~= "all" then
self:fetchStatuses(false)
end
self:updateItemTable() self:updateItemTable()
self.hist_menu.close_callback = function() self.hist_menu.close_callback = function()
self.statuses_fetched = nil self.statuses_fetched = nil
self.filter = nil
UIManager:close(self.hist_menu) UIManager:close(self.hist_menu)
G_reader_settings:saveSetting("history_filter", self.filter)
end end
UIManager:show(self.hist_menu) UIManager:show(self.hist_menu)
return true return true
@ -183,37 +218,17 @@ end
function FileManagerHistory:showHistDialog() function FileManagerHistory:showHistDialog()
if not self.statuses_fetched then if not self.statuses_fetched then
local status self:fetchStatuses(true)
for _, v in ipairs(require("readhistory").hist) do
if v.dim then
status = "deleted"
else
if DocSettings:hasSidecarFile(v.file) then
local docinfo = DocSettings:open(v.file) -- no io handles created, do not close
if docinfo.data.summary and docinfo.data.summary.status
and docinfo.data.summary.status ~= "" then
status = docinfo.data.summary.status
else
status = "reading"
end
else
status = "new"
end
end
v.status = status
self.count[status] = self.count[status] + 1
end
self.statuses_fetched = true
end end
local hist_dialog local hist_dialog
local buttons = {} local buttons = {}
local function genFilterButton(status) local function genFilterButton(filter)
return { return {
text = T(_("%1 (%2)"), status_text[status], self.count[status]), text = T(_("%1 (%2)"), filter_text[filter], self.count[filter]),
callback = function() callback = function()
UIManager:close(hist_dialog) UIManager:close(hist_dialog)
self.filter = status ~= "all" and status self.filter = filter
self:updateItemTable() self:updateItemTable()
end, end,
} }

Loading…
Cancel
Save