mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
add a Dispatcher module that allows for dispatching multiple events at once. This will allow for profiles & for gestures that do multiple things. it has 2 methods: Execute which is given a kv table of settings to change and fires an event for each of them. addSubMenu adds a menu item to a menu to allow for modifying which events are called it also has settingsList which is a master table of all allowed setting and their corresponding info (it is mostly from ReaderGesture and needs a lot of work) to allow for a new setting all one has to do is add a entry to settingsList with a corresponding event and it will work out of the box. the profile plugin is right now still a stub, just to test Dispatcher. the plan is to finish it and eventually refactor ReaderGesture to rely on this. This also needs effort to move many functions out of reader gesture into events where they belong.
134 lines
4.1 KiB
Lua
134 lines
4.1 KiB
Lua
local ConfirmBox = require("ui/widget/confirmbox")
|
|
local DataStorage = require("datastorage")
|
|
local Dispatcher = require("dispatcher")
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
local LuaSettings = require("luasettings")
|
|
local UIManager = require("ui/uimanager")
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
local _ = require("gettext")
|
|
local T = require("ffi/util").template
|
|
|
|
local Profiles = WidgetContainer:new{
|
|
name = "profiles",
|
|
is_doc_only = true,
|
|
profiles_file = DataStorage:getSettingsDir() .. "/profiles.lua",
|
|
profiles = nil,
|
|
data = nil,
|
|
}
|
|
|
|
function Profiles:init()
|
|
self.ui.menu:registerToMainMenu(self)
|
|
end
|
|
|
|
function Profiles:loadProfiles()
|
|
if self.profiles then
|
|
return
|
|
end
|
|
self.profiles = LuaSettings:open(self.profiles_file)
|
|
self.data = self.profiles.data
|
|
end
|
|
|
|
function Profiles:onFlushSettings()
|
|
if self.profiles then
|
|
self.profiles:flush()
|
|
end
|
|
end
|
|
|
|
function Profiles:addToMainMenu(menu_items)
|
|
menu_items.profiles = {
|
|
text = _("Profiles"),
|
|
sub_item_table_func = function()
|
|
return self:getSubMenuItems()
|
|
end,
|
|
}
|
|
end
|
|
|
|
function Profiles:getSubMenuItems()
|
|
self:loadProfiles()
|
|
local sub_item_table = {
|
|
{
|
|
text = _("New"),
|
|
keep_menu_open = true,
|
|
callback = function(touchmenu_instance)
|
|
local name_input
|
|
name_input = InputDialog:new{
|
|
title = _("Enter profile name"),
|
|
input = "",
|
|
buttons = {{
|
|
{
|
|
text = _("Cancel"),
|
|
callback = function()
|
|
UIManager:close(name_input)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Save"),
|
|
callback = function()
|
|
local name = name_input:getInputText()
|
|
if not self:newProfile(name) then
|
|
UIManager:show(InfoMessage:new{
|
|
text = T(_("There is already a profile called: %1"), name),
|
|
})
|
|
return
|
|
end
|
|
UIManager:close(name_input)
|
|
touchmenu_instance.item_table = self:getSubMenuItems()
|
|
touchmenu_instance.page = 1
|
|
touchmenu_instance:updateItems()
|
|
end,
|
|
},
|
|
}},
|
|
}
|
|
UIManager:show(name_input)
|
|
name_input:onShowKeyboard()
|
|
end,
|
|
separator = true,
|
|
}
|
|
}
|
|
for k,v in pairs(self.data) do
|
|
local sub_items = {
|
|
{
|
|
text = _("Delete profile"),
|
|
keep_menu_open = false,
|
|
separator = true,
|
|
callback = function()
|
|
UIManager:show(ConfirmBox:new{
|
|
text = _("Do you want to delete this profile?"),
|
|
ok_text = _("Yes"),
|
|
cancel_text = _("No"),
|
|
ok_callback = function()
|
|
self:deleteProfile(k)
|
|
end,
|
|
})
|
|
end,
|
|
}
|
|
}
|
|
Dispatcher.addSubMenu(self, sub_items, "data", k)
|
|
table.insert(sub_item_table, {
|
|
text = k,
|
|
hold_keep_menu_open = false,
|
|
sub_item_table = sub_items,
|
|
hold_callback = function()
|
|
Dispatcher.execute(self, v)
|
|
end,
|
|
})
|
|
end
|
|
return sub_item_table
|
|
end
|
|
|
|
function Profiles:newProfile(name)
|
|
if self.data[name] == nil then
|
|
self.data[name] = {}
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
function Profiles:deleteProfile(name)
|
|
self.data[name] = nil
|
|
end
|
|
|
|
return Profiles
|