2
0
mirror of https://github.com/koreader/koreader synced 2024-11-11 19:11:14 +00:00
koreader/frontend/apps/filemanager/filemanagercollection.lua

219 lines
7.6 KiB
Lua
Raw Normal View History

2023-06-08 05:27:52 +00:00
local ButtonDialog = require("ui/widget/buttondialog")
local Device = require("device")
2023-06-08 05:27:52 +00:00
local DocumentRegistry = require("document/documentregistry")
local Menu = require("ui/widget/menu")
local ReadCollection = require("readcollection")
local UIManager = require("ui/uimanager")
Clarify our OOP semantics across the codebase (#9586) Basically: * Use `extend` for class definitions * Use `new` for object instantiations That includes some minor code cleanups along the way: * Updated `Widget`'s docs to make the semantics clearer. * Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283) * Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass). * Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events. * Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier. * Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references. * ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak). * Terminal: Make sure the shell is killed on plugin teardown. * InputText: Fix Home/End/Del physical keys to behave sensibly. * InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...). * OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of. * ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed! * Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local Screen = require("device").screen
2022-12-25 07:46:44 +00:00
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local _ = require("gettext")
Clarify our OOP semantics across the codebase (#9586) Basically: * Use `extend` for class definitions * Use `new` for object instantiations That includes some minor code cleanups along the way: * Updated `Widget`'s docs to make the semantics clearer. * Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283) * Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass). * Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events. * Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier. * Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references. * ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak). * Terminal: Make sure the shell is killed on plugin teardown. * InputText: Fix Home/End/Del physical keys to behave sensibly. * InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...). * OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of. * ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed! * Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local FileManagerCollection = WidgetContainer:extend{
coll_menu_title = _("Favorites"),
}
function FileManagerCollection:init()
self.ui.menu:registerToMainMenu(self)
end
function FileManagerCollection:addToMainMenu(menu_items)
menu_items.collections = {
text = self.coll_menu_title,
callback = function()
self:onShowColl("favorites")
end,
}
end
function FileManagerCollection:updateItemTable()
-- Try to stay on current page.
local select_number = nil
if self.coll_menu.page and self.coll_menu.perpage then
select_number = (self.coll_menu.page - 1) * self.coll_menu.perpage + 1
end
self.coll_menu:switchItemTable(self.coll_menu_title,
ReadCollection:prepareList(self.coll_menu.collection), select_number)
end
2023-03-31 16:35:27 +00:00
function FileManagerCollection:onMenuChoice(item)
require("apps/reader/readerui"):showReader(item.file)
end
function FileManagerCollection:onMenuHold(item)
self.collfile_dialog = nil
2023-03-31 16:35:27 +00:00
local function close_dialog_callback()
UIManager:close(self.collfile_dialog)
end
2023-06-08 05:27:52 +00:00
local function close_dialog_menu_callback()
UIManager:close(self.collfile_dialog)
self._manager.coll_menu.close_callback()
end
local function status_button_callback()
UIManager:close(self.collfile_dialog)
self._manager:updateItemTable()
end
local is_currently_opened = item.file == (self.ui.document and self.ui.document.file)
local buttons = {}
if not item.dim then
local doc_settings_or_file = is_currently_opened and self.ui.doc_settings or item.file
table.insert(buttons, filemanagerutil.genStatusButtonsRow(doc_settings_or_file, status_button_callback))
table.insert(buttons, {}) -- separator
end
table.insert(buttons, {
filemanagerutil.genResetSettingsButton(item.file, status_button_callback, is_currently_opened),
{
text = _("Remove from favorites"),
callback = function()
UIManager:close(self.collfile_dialog)
ReadCollection:removeItem(item.file, self._manager.coll_menu.collection)
self._manager:updateItemTable()
end,
},
})
table.insert(buttons, {
2023-06-08 05:27:52 +00:00
filemanagerutil.genShowFolderButton(item.file, close_dialog_menu_callback, item.dim),
2023-03-31 16:35:27 +00:00
filemanagerutil.genBookInformationButton(item.file, close_dialog_callback, item.dim),
})
table.insert(buttons, {
2023-03-31 16:35:27 +00:00
filemanagerutil.genBookCoverButton(item.file, close_dialog_callback, item.dim),
filemanagerutil.genBookDescriptionButton(item.file, close_dialog_callback, item.dim),
})
if Device:canExecuteScript(item.file) then
table.insert(buttons, {
2023-06-08 05:27:52 +00:00
filemanagerutil.genExecuteScriptButton(item.file, close_dialog_menu_callback)
})
end
2023-06-08 05:27:52 +00:00
self.collfile_dialog = ButtonDialog:new{
title = item.text:match("([^/]+)$"),
title_align = "center",
buttons = buttons,
}
UIManager:show(self.collfile_dialog)
return true
end
function FileManagerCollection:MenuSetRotationModeHandler(rotation)
if rotation ~= nil and rotation ~= Screen:getRotationMode() then
UIManager:close(self._manager.coll_menu)
if self._manager.ui.view and self._manager.ui.view.onSetRotationMode then
self._manager.ui.view:onSetRotationMode(rotation)
elseif self._manager.ui.onSetRotationMode then
self._manager.ui:onSetRotationMode(rotation)
else
Screen:setRotationMode(rotation)
end
self._manager:onShowColl()
end
return true
end
function FileManagerCollection:onShowColl(collection)
self.coll_menu = Menu:new{
ui = self.ui,
covers_fullscreen = true, -- hint for UIManager:_repaint()
is_borderless = true,
is_popout = false,
2023-06-08 05:27:52 +00:00
title_bar_left_icon = "appbar.menu",
onLeftButtonTap = function() self:showCollDialog() end,
2023-03-31 16:35:27 +00:00
onMenuChoice = self.onMenuChoice,
onMenuHold = self.onMenuHold,
onSetRotationMode = self.MenuSetRotationModeHandler,
_manager = self,
collection = collection,
}
self:updateItemTable()
self.coll_menu.close_callback = function()
UIManager:close(self.coll_menu)
end
UIManager:show(self.coll_menu)
return true
end
2023-06-08 05:27:52 +00:00
function FileManagerCollection:showCollDialog()
local coll_dialog
local is_added = self.ui.document and ReadCollection:checkItemExist(self.ui.document.file)
local buttons = {
{{
text_func = function()
return is_added and _("Remove current book from favorites") or _("Add current book to favorites")
end,
enabled = self.ui.document and true or false,
callback = function()
UIManager:close(coll_dialog)
if is_added then
ReadCollection:removeItem(self.ui.document.file)
else
ReadCollection:addItem(self.ui.document.file)
end
self:updateItemTable()
end,
}},
{{
text = _("Add a book to favorites"),
callback = function()
UIManager:close(coll_dialog)
local PathChooser = require("ui/widget/pathchooser")
local path_chooser = PathChooser:new{
path = G_reader_settings:readSetting("home_dir"),
select_directory = false,
file_filter = function(file)
return DocumentRegistry:getProviders(file) ~= nil
end,
onConfirm = function(file)
if not ReadCollection:checkItemExist(file) then
ReadCollection:addItem(file)
self:updateItemTable()
end
end,
}
UIManager:show(path_chooser)
end,
}},
{{
text = _("Sort favorites"),
callback = function()
UIManager:close(coll_dialog)
self:sortCollection()
end,
}},
}
coll_dialog = ButtonDialog:new{
buttons = buttons,
}
UIManager:show(coll_dialog)
end
function FileManagerCollection:sortCollection()
local item_table = {}
for _, v in ipairs(self.coll_menu.item_table) do
table.insert(item_table, { text = v.text, label = v.file })
end
local SortWidget = require("ui/widget/sortwidget")
local sort_widget
sort_widget = SortWidget:new{
title = _("Sort favorites"),
item_table = item_table,
callback = function()
local new_order_table = {}
for i, v in ipairs(sort_widget.item_table) do
table.insert(new_order_table, { file = v.label, order = i })
end
ReadCollection:writeCollection(new_order_table, self.coll_menu.collection)
self:updateItemTable()
end
}
UIManager:show(sort_widget)
end
2023-09-06 06:41:10 +00:00
function FileManagerCollection:onBookMetadataChanged()
if self.coll_menu then
self.coll_menu:updateItems()
end
end
return FileManagerCollection