From 502bb0ccbfd94addda0dcf67cc0d5c7446df7ce1 Mon Sep 17 00:00:00 2001 From: Melik <10296053+melyux@users.noreply.github.com> Date: Sat, 4 Feb 2023 12:32:43 -0800 Subject: [PATCH] Create genStatusButton() for buttons, use item.dim for deleted in history --- frontend/apps/filemanager/filemanager.lua | 44 +++++------- .../filemanager/filemanagercollection.lua | 50 ++++++------- .../apps/filemanager/filemanagerhistory.lua | 71 +++++++------------ plugins/coverbrowser.koplugin/covermenu.lua | 54 +++++++------- 4 files changed, 89 insertions(+), 130 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index f50d31430..33a9b52cd 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -340,37 +340,27 @@ function FileManager:setupLayout() if is_file then local status = filemanagerutil.getStatus(file) - table.insert(buttons, { - { - text = _("Reading"), - id = "mark_as_reading", -- used by covermenu - enabled = status ~= "reading", - callback = function() - filemanagerutil.setStatus(file, "reading") - self:refreshPath() - UIManager:close(self.file_dialog) - end, - }, - { - text = _("On hold"), - id = "put_on_hold", -- used by covermenu - enabled = status ~= "abandoned", - callback = function() - filemanagerutil.setStatus(file, "abandoned") - self:refreshPath() - UIManager:close(self.file_dialog) - end, - }, - { - text = _("Finished"), - id = "mark_as_read", -- used by covermenu - enabled = status ~= "complete", + local function genStatusButton(to_status) + local status_text = { + reading = _("Reading"), + abandoned = _("On hold"), + complete = _("Finished"), + } + return { + text = status_text[to_status], + id = to_status, -- used by covermenu + enabled = status ~= to_status, callback = function() - filemanagerutil.setStatus(file, "complete") + filemanagerutil.setStatus(file, to_status) self:refreshPath() UIManager:close(self.file_dialog) end, - }, + } + end + table.insert(buttons, { + genStatusButton("reading"), + genStatusButton("abandoned"), + genStatusButton("complete"), }) table.insert(buttons, {}) -- separator table.insert(buttons, { diff --git a/frontend/apps/filemanager/filemanagercollection.lua b/frontend/apps/filemanager/filemanagercollection.lua index 96aac36f2..528ba1a37 100644 --- a/frontend/apps/filemanager/filemanagercollection.lua +++ b/frontend/apps/filemanager/filemanagercollection.lua @@ -46,38 +46,28 @@ end function FileManagerCollection:onMenuHold(item) self.collfile_dialog = nil local status = filemanagerutil.getStatus(item.file) + local function genStatusButton(to_status) + local status_text = { + reading = _("Reading"), + abandoned = _("On hold"), + complete = _("Finished"), + } + return { + text = status_text[to_status], + id = to_status, -- used by covermenu + enabled = status ~= to_status, + callback = function() + filemanagerutil.setStatus(item.file, to_status) + self._manager:updateItemTable() + UIManager:close(self.collfile_dialog) + end, + } + end local buttons = { { - { - text = _("Reading"), - id = "mark_as_reading", -- used by covermenu - enabled = status ~= "reading", - callback = function() - filemanagerutil.setStatus(item.file, "reading") - self._manager:updateItemTable() - UIManager:close(self.collfile_dialog) - end, - }, - { - text = _("On hold"), - id = "put_on_hold", -- used by covermenu - enabled = status ~= "abandoned", - callback = function() - filemanagerutil.setStatus(item.file, "abandoned") - self._manager:updateItemTable() - UIManager:close(self.collfile_dialog) - end, - }, - { - text = _("Finished"), - id = "mark_as_read", -- used by covermenu - enabled = status ~= "complete", - callback = function() - filemanagerutil.setStatus(item.file, "complete") - self._manager:updateItemTable() - UIManager:close(self.collfile_dialog) - end, - }, + genStatusButton("reading"), + genStatusButton("abandoned"), + genStatusButton("complete"), }, {}, { diff --git a/frontend/apps/filemanager/filemanagerhistory.lua b/frontend/apps/filemanager/filemanagerhistory.lua index a702f8b87..921c4cfd9 100644 --- a/frontend/apps/filemanager/filemanagerhistory.lua +++ b/frontend/apps/filemanager/filemanagerhistory.lua @@ -89,55 +89,34 @@ function FileManagerHistory:onMenuHold(item) local readerui_instance = require("apps/reader/readerui"):_getRunningInstance() local currently_opened_file = readerui_instance and readerui_instance.document and readerui_instance.document.file self.histfile_dialog = nil - local is_file = lfs.attributes(item.file, "mode") == "file" local status = filemanagerutil.getStatus(item.file) + local function genStatusButton(to_status) + local status_text = { + reading = _("Reading"), + abandoned = _("On hold"), + complete = _("Finished"), + } + return { + text = status_text[to_status], + id = to_status, -- used by covermenu + enabled = not item.dim and status ~= to_status, + callback = function() + filemanagerutil.setStatus(item.file, to_status) + if self._manager.filter ~= "all" then + self._manager:fetchStatuses(false) + else + self._manager.statuses_fetched = false + end + self._manager:updateItemTable() + UIManager:close(self.histfile_dialog) + end, + } + end local buttons = { { - { - text = _("Reading"), - id = "mark_as_reading", -- used by covermenu - enabled = is_file and status ~= "reading", - callback = function() - filemanagerutil.setStatus(item.file, "reading") - if self._manager.filter ~= "all" then - self._manager:fetchStatuses(false) - else - self._manager.statuses_fetched = false - end - self._manager:updateItemTable() - UIManager:close(self.histfile_dialog) - end, - }, - { - text = _("On hold"), - id = "put_on_hold", -- used by covermenu - enabled = is_file and status ~= "abandoned", - callback = function() - filemanagerutil.setStatus(item.file, "abandoned") - if self._manager.filter ~= "all" then - self._manager:fetchStatuses(false) - else - self._manager.statuses_fetched = false - end - self._manager:updateItemTable() - UIManager:close(self.histfile_dialog) - end, - }, - { - text = _("Finished"), - id = "mark_as_read", -- used by covermenu - enabled = is_file and status ~= "complete", - callback = function() - filemanagerutil.setStatus(item.file, "complete") - if self._manager.filter ~= "all" then - self._manager:fetchStatuses(false) - else - self._manager.statuses_fetched = false - end - self._manager:updateItemTable() - UIManager:close(self.histfile_dialog) - end, - }, + genStatusButton("reading"), + genStatusButton("abandoned"), + genStatusButton("complete"), }, {}, { diff --git a/plugins/coverbrowser.koplugin/covermenu.lua b/plugins/coverbrowser.koplugin/covermenu.lua index 729047df5..281e08490 100644 --- a/plugins/coverbrowser.koplugin/covermenu.lua +++ b/plugins/coverbrowser.koplugin/covermenu.lua @@ -342,35 +342,35 @@ function CoverMenu:updateItems(select_number) end -- Fudge the status change button callbacks to also update the cover_info_cache - button = self.file_dialog.button_table:getButtonById("mark_as_reading") - local orig_mark_as_reading_callback = button.callback + button = self.file_dialog.button_table:getButtonById("reading") + local orig_reading_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "reading" end -- And then set the status on file as expected - orig_mark_as_reading_callback() + orig_reading_callback() end - button = self.file_dialog.button_table:getButtonById("put_on_hold") - local orig_put_on_hold_callback = button.callback + button = self.file_dialog.button_table:getButtonById("abandoned") + local orig_abandoned_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "abandoned" end -- And then set the status on file as expected - orig_put_on_hold_callback() + orig_abandoned_callback() end - button = self.file_dialog.button_table:getButtonById("mark_as_read") - local orig_mark_as_read_callback = button.callback + button = self.file_dialog.button_table:getButtonById("complete") + local orig_complete_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "complete" end -- And then set the status on file as expected - orig_mark_as_read_callback() + orig_complete_callback() end -- Replace the "Book information" button callback to use directly our bookinfo @@ -519,35 +519,35 @@ function CoverMenu:onHistoryMenuHold(item) end -- Fudge the status change button callbacks to also update the cover_info_cache - button = self.histfile_dialog.button_table:getButtonById("mark_as_reading") - local orig_mark_as_reading_callback = button.callback + button = self.histfile_dialog.button_table:getButtonById("reading") + local orig_reading_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "reading" end -- And then set the status on file as expected - orig_mark_as_reading_callback() + orig_reading_callback() end - button = self.histfile_dialog.button_table:getButtonById("put_on_hold") - local orig_put_on_hold_callback = button.callback + button = self.histfile_dialog.button_table:getButtonById("abandoned") + local orig_abandoned_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "abandoned" end -- And then set the status on file as expected - orig_put_on_hold_callback() + orig_abandoned_callback() end - button = self.histfile_dialog.button_table:getButtonById("mark_as_read") - local orig_mark_as_read_callback = button.callback + button = self.histfile_dialog.button_table:getButtonById("complete") + local orig_complete_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "complete" end -- And then set the status on file as expected - orig_mark_as_read_callback() + orig_complete_callback() end -- Replace Book information callback to use directly our bookinfo @@ -688,35 +688,35 @@ function CoverMenu:onCollectionsMenuHold(item) end -- Fudge the status change button callbacks to also update the cover_info_cache - button = self.collfile_dialog.button_table:getButtonById("mark_as_reading") - local orig_mark_as_reading_callback = button.callback + button = self.collfile_dialog.button_table:getButtonById("reading") + local orig_reading_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "reading" end -- And then set the status on file as expected - orig_mark_as_reading_callback() + orig_reading_callback() end - button = self.collfile_dialog.button_table:getButtonById("put_on_hold") - local orig_put_on_hold_callback = button.callback + button = self.collfile_dialog.button_table:getButtonById("abandoned") + local orig_abandoned_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "abandoned" end -- And then set the status on file as expected - orig_put_on_hold_callback() + orig_abandoned_callback() end - button = self.collfile_dialog.button_table:getButtonById("mark_as_read") - local orig_mark_as_read_callback = button.callback + button = self.collfile_dialog.button_table:getButtonById("complete") + local orig_complete_callback = button.callback button.callback = function() -- Update the cache if self.cover_info_cache and self.cover_info_cache[file] then self.cover_info_cache[file][3] = "complete" end -- And then set the status on file as expected - orig_mark_as_read_callback() + orig_complete_callback() end -- Replace Book information callback to use directly our bookinfo