mirror of
https://github.com/koreader/koreader
synced 2024-10-31 21:20:20 +00:00
1ffbd8760d
Use a table & table.concat instead of individual concats. And then use that same table for every hash-related operation. (Nothing else uses the configurable hash function, otherwise I'd have limited the table shenanigans to the function itself).
72 lines
2.0 KiB
Lua
72 lines
2.0 KiB
Lua
local ffiUtil = require("ffi/util")
|
|
|
|
local Configurable = {}
|
|
|
|
function Configurable:new(o)
|
|
o = o or {}
|
|
setmetatable(o, self)
|
|
self.__index = self
|
|
return o
|
|
end
|
|
|
|
function Configurable:reset()
|
|
for key, value in pairs(self) do
|
|
local value_type = type(value)
|
|
if value_type == "number" or value_type == "string" then
|
|
self[key] = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
function Configurable:hash(list)
|
|
for key, value in ffiUtil.orderedPairs(self) do
|
|
local value_type = type(value)
|
|
if value_type == "number" or value_type == "string" then
|
|
table.insert(list, value)
|
|
end
|
|
end
|
|
end
|
|
|
|
function Configurable:loadDefaults(config_options)
|
|
-- reset configurable before loading new options
|
|
self:reset()
|
|
local prefix = config_options.prefix.."_"
|
|
for i=1, #config_options do
|
|
local options = config_options[i].options
|
|
for j=1,#options do
|
|
local key = options[j].name
|
|
local settings_key = prefix..key
|
|
local default = G_reader_settings:readSetting(settings_key)
|
|
self[key] = default or options[j].default_value
|
|
if not self[key] then
|
|
self[key] = options[j].default_arg
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function Configurable:loadSettings(settings, prefix)
|
|
for key, value in pairs(self) do
|
|
local value_type = type(value)
|
|
if value_type == "number" or value_type == "string"
|
|
or value_type == "table" then
|
|
local saved_value = settings:readSetting(prefix..key)
|
|
if saved_value ~= nil then
|
|
self[key] = saved_value
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function Configurable:saveSettings(settings, prefix)
|
|
for key, value in pairs(self) do
|
|
local value_type = type(value)
|
|
if value_type == "number" or value_type == "string"
|
|
or value_type == "table" then
|
|
settings:saveSetting(prefix..key, value)
|
|
end
|
|
end
|
|
end
|
|
|
|
return Configurable
|