ReadTimer + Profiles auto-exec (#14309)

This commit is contained in:
hius07
2025-09-14 10:32:06 +03:00
committed by GitHub
parent d1e2f586ce
commit 99c7d00b58
3 changed files with 150 additions and 46 deletions

View File

@@ -12,7 +12,7 @@ local util = require("util")
local _ = require("gettext")
-- Date at which the last migration snippet was added
local CURRENT_MIGRATION_DATE = 20250601
local CURRENT_MIGRATION_DATE = 20250914
-- Retrieve the date of the previous migration, if any
local last_migration_date = G_reader_settings:readSetting("last_migration_date", 0)
@@ -888,6 +888,28 @@ if last_migration_date < 20250601 then
SettingsMigration:migrateSettings(G_reader_settings)
end
-- 20250914, Move all ReadTimer plugin settings into a single table.
-- https://github.com/koreader/koreader/pull/14309
if last_migration_date < 20250914 then
logger.info("Performing one-time migration for 20250914")
local remain_time_hours, remain_time_minutes
local remain_time = G_reader_settings:readSetting("reader_timer_remain_time")
if remain_time then
remain_time_hours = remain_time[1]
remain_time_minutes = remain_time[2]
G_reader_settings:delSetting("reader_timer_remain_time")
end
local settings = {
remain_time_hours = remain_time_hours,
remain_time_minutes = remain_time_minutes,
show_value_in_header = G_reader_settings:readSetting("readtimer_show_value_in_header"),
show_value_in_footer = G_reader_settings:readSetting("readtimer_show_value_in_footer"),
}
G_reader_settings:saveSetting("readtimer", settings)
G_reader_settings:delSetting("readtimer_show_value_in_header")
G_reader_settings:delSetting("readtimer_show_value_in_footer")
end
-- We're done, store the current migration date
G_reader_settings:saveSetting("last_migration_date", CURRENT_MIGRATION_DATE)

View File

@@ -226,11 +226,13 @@ For example, with a trigger "on document closing" the profile will be executed b
self:genAutoExecMenuItem(_("on KOReader start"), "Start", k),
self:genAutoExecMenuItem(_("on wake-up"), "Resume", k),
self:genAutoExecMenuItem(_("on exiting sleep screen"), "OutOfScreenSaver", k),
self:genAutoExecMenuItem(_("on read timer expiry"), "ReadTimerExpired", k),
self:genAutoExecMenuItem(_("on rotation"), "SetRotationMode", k),
self:genAutoExecMenuItem(_("on showing folder"), "PathChanged", k, true),
-- separator
self:genAutoExecMenuItem(_("on book opening"), "ReaderReadyAll", k),
self:genAutoExecMenuItem(_("on book closing"), "CloseDocumentAll", k),
max_per_page = 11,
}
end,
hold_callback = function(touchmenu_instance)
@@ -978,6 +980,10 @@ function Profiles:onOutOfScreenSaver() -- global
self:executeAutoExecEvent("OutOfScreenSaver")
end
function Profiles:onReadTimerExpired() -- global by ReadTimer plugin
self:executeAutoExecEvent("ReadTimerExpired")
end
function Profiles:onSetRotationMode(mode) -- global
local event = "SetRotationMode"
if self.autoexec[event] == nil then return end

View File

@@ -4,6 +4,7 @@ local DateTimeWidget = require("ui/widget/datetimewidget")
local Dispatcher = require("dispatcher")
local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local Notification = require("ui/widget/notification")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local logger = require("logger")
@@ -24,26 +25,47 @@ local ReadTimer = WidgetContainer:extend{
function ReadTimer:onDispatcherRegisterActions()
Dispatcher:registerAction("show_alarm",
{category="none", event="ShowAlarm", title=_("Set reader alarm"), general=true})
{category="none", event="ShowAlarm", title=_("Set read timer alarm"), general=true})
Dispatcher:registerAction("show_timer",
{category="none", event="ShowTimer", title=_("Set reader timer"), general=true})
{category="none", event="ShowTimer", title=_("Set read timer interval"), general=true})
Dispatcher:registerAction("stop_timer",
{category="none", event="StopTimer", title=_("Stop reader timer"), general=true, separator=true})
{category="none", event="StopTimer", title=_("Stop read timer"), general=true, separator=true})
end
function ReadTimer:init()
self.timer_symbol = "\u{23F2}" -- ⏲ timer symbol
self.timer_letter = "T"
self.settings = G_reader_settings:readSetting("readtimer", {})
self.alarm_callback = function()
-- Don't do anything if we were unscheduled
if self.time == 0 then return end
local function maybeRescheduleInterval()
if self.last_interval_time > 0 and self.settings.auto_reschedule_interval then
logger.dbg("Schedule a new time:", self.last_interval_time)
self:rescheduleIn(self.last_interval_time)
end
end
self.time = 0
UIManager:broadcastEvent(Event:new("ReadTimerExpired"))
if self.settings.show_on_expiry == "nothing" then
maybeRescheduleInterval()
return
end
local tip_text = _("Time is up")
if self.settings.show_on_expiry == "notification" then
UIManager:show(Notification:new{
text = tip_text,
timeout = 5,
})
maybeRescheduleInterval()
return
end
local confirm_box
-- only interval support repeat
if self.last_interval_time > 0 then
if self.last_interval_time > 0 and not self.settings.auto_reschedule_interval then
logger.dbg("can_repeat, show confirm_box")
confirm_box = ConfirmBox:new{
text = tip_text,
@@ -64,6 +86,7 @@ function ReadTimer:init()
UIManager:show(InfoMessage:new{
text = tip_text,
})
maybeRescheduleInterval()
end
end
@@ -103,14 +126,11 @@ function ReadTimer:init()
return
end
self.show_value_in_header = G_reader_settings:readSetting("readtimer_show_value_in_header")
self.show_value_in_footer = G_reader_settings:readSetting("readtimer_show_value_in_footer")
if self.show_value_in_header then
if self.settings.show_value_in_header then
self:addAdditionalHeaderContent()
end
if self.show_value_in_footer then
if self.settings.show_value_in_footer then
self:addAdditionalFooterContent()
end
@@ -119,10 +139,10 @@ function ReadTimer:init()
end
function ReadTimer:update_status_bars(seconds)
if self.show_value_in_header then
if self.settings.show_value_in_header then
UIManager:broadcastEvent(Event:new("UpdateHeader"))
end
if self.show_value_in_footer then
if self.settings.show_value_in_footer then
UIManager:broadcastEvent(Event:new("RefreshAdditionalContent"))
end
-- if seconds schedule 1ms later
@@ -216,45 +236,57 @@ function ReadTimer:rescheduleIn(seconds)
-- Resolution: time.now() subsecond, os.time() two seconds
self.time = time.now() + time.s(seconds)
UIManager:scheduleIn(seconds, self.alarm_callback)
if self.show_value_in_header or self.show_value_in_footer then
if self.settings.show_value_in_header or self.settings.show_value_in_footer then
self:update_status_bars(seconds)
end
end
function ReadTimer:addCheckboxes(widget)
local checkbox_header = CheckButton:new{
function ReadTimer:addCheckboxes(widget, is_interval)
if is_interval then
widget:addWidget(CheckButton:new{
text = _("Auto-reschedule"),
checked = self.settings.auto_reschedule_interval,
parent = widget,
callback = function()
self.settings.auto_reschedule_interval = not self.settings.auto_reschedule_interval or nil
end,
})
end
widget:addWidget(CheckButton:new{
text = _("Show timer in alt status bar"),
checked = self.show_value_in_header,
checked = self.settings.show_value_in_header,
parent = widget,
callback = function()
self.show_value_in_header = not self.show_value_in_header
G_reader_settings:saveSetting("readtimer_show_value_in_header", self.show_value_in_header)
if self.show_value_in_header then
self.settings.show_value_in_header = not self.settings.show_value_in_header or nil
if self.settings.show_value_in_header then
self:addAdditionalHeaderContent()
else
self:removeAdditionalHeaderContent()
end
end,
}
local checkbox_footer = CheckButton:new{
})
widget:addWidget(CheckButton:new{
text = _("Show timer in status bar"),
checked = self.show_value_in_footer,
checked = self.settings.show_value_in_footer,
parent = widget,
callback = function()
self.show_value_in_footer = not self.show_value_in_footer
G_reader_settings:saveSetting("readtimer_show_value_in_footer", self.show_value_in_footer)
if self.show_value_in_footer then
self.settings.show_value_in_footer = not self.settings.show_value_in_footer or nil
if self.settings.show_value_in_footer then
self:addAdditionalFooterContent()
else
self:removeAdditionalFooterContent()
end
end,
}
widget:addWidget(checkbox_header)
widget:addWidget(checkbox_footer)
})
end
function ReadTimer:addToMainMenu(menu_items)
local show_on_expiry_info = {
message = _("message"),
notification = _("notification"),
nothing = _("nothing"),
}
menu_items.read_timer = {
text_func = function()
if self:scheduled() then
@@ -268,9 +300,14 @@ function ReadTimer:addToMainMenu(menu_items)
checked_func = function()
return self:scheduled()
end,
hold_callback = function(touchmenu_instance)
if self:scheduled() then
self:onStopTimer(touchmenu_instance)
end
end,
sub_item_table = {
{
text = _("Set time"),
text = _("Set alarm"),
keep_menu_open = true,
callback = function(touchmenu_instance)
self:onShowAlarm(touchmenu_instance)
@@ -292,6 +329,44 @@ function ReadTimer:addToMainMenu(menu_items)
callback = function(touchmenu_instance)
self:onStopTimer(touchmenu_instance)
end,
separator = true,
},
{
text_func = function()
return T(_("Show on timer expiry: %1"), show_on_expiry_info[self.settings.show_on_expiry or "message"])
end,
sub_item_table = {
{
text = show_on_expiry_info["message"],
checked_func = function()
return self.settings.show_on_expiry == nil
end,
radio = true,
callback = function()
self.settings.show_on_expiry = nil
end,
},
{
text = show_on_expiry_info["notification"],
checked_func = function()
return self.settings.show_on_expiry == "notification"
end,
radio = true,
callback = function()
self.settings.show_on_expiry = "notification"
end,
},
{
text = show_on_expiry_info["nothing"],
checked_func = function()
return self.settings.show_on_expiry == "nothing"
end,
radio = true,
callback = function()
self.settings.show_on_expiry = "nothing"
end,
},
},
},
},
}
@@ -325,10 +400,13 @@ function ReadTimer:onShowAlarm(touchmenu_instance)
hour = curr_hour,
min = curr_min,
ok_text = _("Set alarm"),
title_text = _("New alarm"),
title_text = _("Alarm"),
info_text = _("Enter a time in hours and minutes."),
callback = function(alarm_time)
self:setAlarm(alarm_time, now_t, touchmenu_instance)
if touchmenu_instance then
touchmenu_instance:closeMenu()
end
self:setAlarm(alarm_time, now_t)
end
}
self:addCheckboxes(time_widget)
@@ -336,7 +414,7 @@ function ReadTimer:onShowAlarm(touchmenu_instance)
return true
end
function ReadTimer:setAlarm(alarm_time, then_t, touchmenu_instance)
function ReadTimer:setAlarm(alarm_time, then_t)
then_t.hour = alarm_time.hour
then_t.min = alarm_time.min
then_t.sec = 0
@@ -357,36 +435,35 @@ function ReadTimer:setAlarm(alarm_time, then_t, touchmenu_instance)
datetime.secondsToClockDuration(user_duration_format, seconds, false)),
timeout = 5,
})
if touchmenu_instance then touchmenu_instance:updateItems() end
end
function ReadTimer:onShowTimer(touchmenu_instance)
local remain_hours, remain_minutes = self:remainingTime()
if not remain_hours and not remain_minutes then
local remain_time = G_reader_settings:readSetting("reader_timer_remain_time")
if remain_time then
remain_hours = remain_time[1]
remain_minutes = remain_time[2]
end
remain_hours = self.settings.remain_time_hours
remain_minutes = self.settings.remain_time_minutes
end
local time_widget = DateTimeWidget:new{
hour = remain_hours or 0,
min = remain_minutes or 0,
hour_max = 17,
ok_text = _("Set timer"),
title_text = _("Set reader timer"),
ok_text = _("Start timer"),
title_text = _("Interval"),
info_text = _("Enter a time in hours and minutes."),
callback = function(timer_time)
self:setInterval(timer_time, touchmenu_instance)
if touchmenu_instance then
touchmenu_instance:closeMenu()
end
self:setInterval(timer_time)
end
}
self:addCheckboxes(time_widget)
self:addCheckboxes(time_widget, true)
UIManager:show(time_widget)
return true
end
function ReadTimer:setInterval(timer_time, touchmenu_instance)
function ReadTimer:setInterval(timer_time)
local seconds = timer_time.hour * 3600 + timer_time.min * 60
if seconds > 0 then
self:unschedule()
@@ -400,11 +477,10 @@ function ReadTimer:setInterval(timer_time, touchmenu_instance)
datetime.secondsToClockDuration(user_duration_format, seconds, true)),
timeout = 5,
})
if touchmenu_instance then touchmenu_instance:updateItems() end
-- Save settings
local remain_time = {timer_time.hour, timer_time.min}
G_reader_settings:saveSetting("reader_timer_remain_time", remain_time)
self.settings.remain_time_hours = timer_time.hour
self.settings.remain_time_minutes = timer_time.min
end
end