Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add book to favorites #5527

Merged
merged 7 commits into from Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 49 additions & 16 deletions frontend/apps/filemanager/filemanager.lua
Expand Up @@ -9,6 +9,7 @@ local DocumentRegistry = require("document/documentregistry")
local Event = require("ui/event")
local FileChooser = require("ui/widget/filechooser")
local FileManagerBookInfo = require("apps/filemanager/filemanagerbookinfo")
local FileManagerCollection = require("apps/filemanager/filemanagercollection")
local FileManagerConverter = require("apps/filemanager/filemanagerconverter")
local FileManagerFileSearcher = require("apps/filemanager/filemanagerfilesearcher")
local FileManagerHistory = require("apps/filemanager/filemanagerhistory")
Expand All @@ -23,6 +24,7 @@ local InputContainer = require("ui/widget/container/inputcontainer")
local InputDialog = require("ui/widget/inputdialog")
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
local PluginLoader = require("pluginloader")
local ReadCollection = require("readcollection")
local ReaderDeviceStatus = require("apps/reader/modules/readerdevicestatus")
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
local ReaderGesture = require("apps/reader/modules/readergesture")
Expand Down Expand Up @@ -288,35 +290,59 @@ function FileManager:init()
},
-- a little hack to get visual functionality grouping
{},
{
}
if lfs.attributes(file, "mode") == "file" then
table.insert(buttons, {
{
text = _("Open with…"),
enabled = lfs.attributes(file, "mode") == "file" and (DocumentRegistry:getProviders(file) == nil
or #(DocumentRegistry:getProviders(file)) > 1),
enabled = DocumentRegistry:getProviders(file) == nil or #(DocumentRegistry:getProviders(file)) > 1,
callback = function()
UIManager:close(self.file_dialog)
self:showSetProviderButtons(file, FileManager.instance, ReaderUI)
end,
},
{
text = _("Convert"),
enabled = lfs.attributes(file, "mode") == "file"
and FileManagerConverter:isSupported(file),
text = _("Book information"),
enabled = FileManagerBookInfo:isSupported(file),
callback = function()
FileManagerBookInfo:show(file)
UIManager:close(self.file_dialog)
FileManagerConverter:showConvertButtons(file, self)
end,
},
}
})
table.insert(buttons, {
{
text = _("Book information"),
enabled = FileManagerBookInfo:isSupported(file),
text_func = function()
if ReadCollection:checkItemExist(file) then
return _("Remove from favorites")
else
return _("Add to favorites")
end
end,
enabled = DocumentRegistry:getProviders(file) ~= nil,
callback = function()
FileManagerBookInfo:show(file)
if ReadCollection:checkItemExist(file) then
ReadCollection:removeItem(file)
else
ReadCollection:addItem(file)
end
UIManager:close(self.file_dialog)
end,
},
},
}
})
if FileManagerConverter:isSupported(file) then
table.insert(buttons, {
{
text = _("Convert"),
enabled = true,
callback = function()
UIManager:close(self.file_dialog)
FileManagerConverter:showConvertButtons(file, self)
end,
}
})
end
end
if lfs.attributes(file, "mode") == "directory" then
local realpath = util.realpath(file)
table.insert(buttons, {
Expand Down Expand Up @@ -361,6 +387,9 @@ function FileManager:init()
table.insert(self, FileManagerHistory:new{
ui = self,
})
table.insert(self, FileManagerCollection:new{
ui = self,
})
table.insert(self, FileManagerFileSearcher:new{ ui = self })
table.insert(self, FileManagerShortcuts:new{ ui = self })
table.insert(self, ReaderDictionary:new{ ui = self })
Expand Down Expand Up @@ -695,10 +724,11 @@ function FileManager:pasteHere(file)
self:moveFile(DocSettings:getSidecarDir(orig), dest) -- dest is always a directory
end
if self:moveFile(orig, dest) then
--update history
-- Update history and collections.
local dest_file = string.format("%s/%s", dest, util.basename(orig))
require("readhistory"):updateItemByPath(orig, dest_file)
--update last open file
ReadCollection:updateItemByPath(orig, dest_file)
-- Update last open file.
if G_reader_settings:readSetting("lastfile") == orig then
G_reader_settings:saveSetting("lastfile", dest_file)
end
Expand Down Expand Up @@ -764,7 +794,7 @@ function FileManager:createFolder(curr_folder, new_folder)
end

function FileManager:deleteFile(file)
local ok, err
local ok, err, is_dir
local file_abs_path = util.realpath(file)
if file_abs_path == nil then
UIManager:show(InfoMessage:new{
Expand All @@ -778,6 +808,7 @@ function FileManager:deleteFile(file)
ok, err = os.remove(file_abs_path)
else
ok, err = util.purgeDir(file_abs_path)
is_dir = true
end
if ok and not err then
if is_doc then
Expand All @@ -789,6 +820,7 @@ function FileManager:deleteFile(file)
end
doc_settings:purge()
end
ReadCollection:removeItemByPath(file, is_dir)
UIManager:show(InfoMessage:new{
text = util.template(_("Deleted %1"), file),
timeout = 2,
Expand All @@ -804,6 +836,7 @@ function FileManager:renameFile(file)
if util.basename(file) ~= self.rename_dialog:getInputText() then
local dest = util.joinPath(util.dirname(file), self.rename_dialog:getInputText())
if self:moveFile(file, dest) then
ReadCollection:updateItemByPath(file, dest)
if lfs.attributes(dest, "mode") == "file" then
local doc = require("docsettings")
local move_history = true;
Expand Down
124 changes: 124 additions & 0 deletions frontend/apps/filemanager/filemanagercollection.lua
@@ -0,0 +1,124 @@
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local FileManagerBookInfo = require("apps/filemanager/filemanagerbookinfo")
local InputContainer = require("ui/widget/container/inputcontainer")
local Menu = require("ui/widget/menu")
local ReadCollection = require("readcollection")
local UIManager = require("ui/uimanager")
local Screen = require("device").screen
local _ = require("gettext")

local FileManagerCollection = InputContainer: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

function FileManagerCollection:onMenuHold(item)
self.collfile_dialog = nil
local buttons = {
{
{
text = _("Sort"),
callback = function()
UIManager:close(self.collfile_dialog)
local item_table = {}
for i=1, #self._manager.coll_menu.item_table do
table.insert(item_table, {text = self._manager.coll_menu.item_table[i].text, label = self._manager.coll_menu.item_table[i].file})
end
local SortWidget = require("ui/widget/sortwidget")
local sort_item
sort_item = SortWidget:new{
title = _("Sort favorites"),
item_table = item_table,
callback = function()
local new_order_table = {}
for i=1, #sort_item.item_table do
table.insert(new_order_table, {
file = sort_item.item_table[i].label,
order = i
})
end
ReadCollection:writeCollection(new_order_table, self._manager.coll_menu.collection)
self._manager:updateItemTable()
end
}
UIManager:show(sort_item)

end,
},
{
text = _("Remove from collection"),
callback = function()
ReadCollection:removeItem(item.file, self._manager.coll_menu.collection)
self._manager:updateItemTable()
UIManager:close(self.collfile_dialog)
end,
},
},
{
{
text = _("Book information"),
enabled = FileManagerBookInfo:isSupported(item.file),
callback = function()
FileManagerBookInfo:show(item.file)
Comment on lines +81 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later, you might want to tweak the onHold dialog when the user uses a CoverBrowser mode, like we did for History in:

-- Similar to onFileHold setup just above, but for History,
-- which is plugged in main.lua _FileManagerHistory_updateItemTable()
function CoverMenu:onHistoryMenuHold(item)

so we have the other buttons like View book description, View full size cover...

UIManager:close(self.collfile_dialog)
end,
},
},
}
self.collfile_dialog = ButtonDialogTitle:new{
title = item.text:match("([^/]+)$"),
title_align = "center",
buttons = buttons,
}
UIManager:show(self.collfile_dialog)
return true
end

function FileManagerCollection:onShowColl(collection)
self.coll_menu = Menu:new{
ui = self.ui,
width = Screen:getWidth(),
height = Screen:getHeight(),
covers_fullscreen = true, -- hint for UIManager:_repaint()
is_borderless = true,
is_popout = false,
onMenuHold = self.onMenuHold,
_manager = self,
collection = collection,
}
self:updateItemTable()
self.coll_menu.close_callback = function()
-- Close it at next tick so it stays displayed
-- while a book is opening (avoids a transient
-- display of the underlying File Browser)
UIManager:nextTick(function()
UIManager:close(self.coll_menu)
end)
end
UIManager:show(self.coll_menu)
return true
end

return FileManagerCollection
6 changes: 6 additions & 0 deletions frontend/apps/reader/readerui.lua
Expand Up @@ -11,6 +11,7 @@ local DocSettings = require("docsettings")
local DocumentRegistry = require("document/documentregistry")
local Event = require("ui/event")
local FileManagerBookInfo = require("apps/filemanager/filemanagerbookinfo")
local FileManagerCollection = require("apps/filemanager/filemanagercollection")
local FileManagerHistory = require("apps/filemanager/filemanagerhistory")
local FileManagerFileSearcher = require("apps/filemanager/filemanagerfilesearcher")
local FileManagerShortcuts = require("apps/filemanager/filemanagershortcuts")
Expand Down Expand Up @@ -351,6 +352,11 @@ function ReaderUI:init()
dialog = self.dialog,
ui = self,
})
-- collections/favorites view
self:registerModule("collections", FileManagerCollection:new{
dialog = self.dialog,
ui = self,
})
-- book info
self:registerModule("bookinfo", FileManagerBookInfo:new{
dialog = self.dialog,
Expand Down