-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
149 lines (134 loc) · 3.85 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
-- gotobed is a koreader plugin to shut down the device at bedtime
--
-- TODO(edoput) this will always shut down the device after betime
-- even when the device is on standby. Listen for standby events
-- and cancel the poweroff.
-- ui
local DateTimeWidget = require("ui/widget/datetimewidget")
local InfoMessage = require("ui/widget/infomessage")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
-- persistence
local LuaSettings = require("luasettings")
local DataStorage = require("datastorage")
-- i18n
local _ = require("gettext")
local T = require("ffi/util").template
-- debug
local logger = require("logger")
local GoToBed = WidgetContainer:new{
name = "gotobed",
settings = nil, -- user settings { hour = h, min = m }
timeout = 60, -- callback timeout in seconds
callback = nil,
}
function GoToBed:init()
if not self.settings then
self.settings = LuaSettings:open(DataStorage:getSettingsDir() .. "/gotobed.lua")
end
self:setup()
-- previously scheduled callbacks must be cleared
self.ui.menu:registerToMainMenu(self)
end
function GoToBed:enabled()
return self.settings:readSetting("bedtime", 0) ~= 0
end
function GoToBed:schedule()
return self.settings:readSetting("bedtime")
end
function GoToBed:setup()
self.callback = function () self:run() end
UIManager:scheduleIn(self.timeout, self.callback)
end
function GoToBed:teardown()
if self.callback ~= nil then
UIManager:unschedule(self.callback)
end
self.callback = nil
end
-- callback
function GoToBed:run()
if not self:enabled() then
return
end
local schedule = self:schedule()
local now = os.date("*t")
if schedule.hour >= now.hour and schedule.min >= now.min then
-- TODO(edoput) send a message
UIManager.poweroff_action()
return
end
UIManager:scheduleIn(self.timeout, self.callback)
end
function GoToBed:addToMainMenu(menu_items)
menu_items.gotobed = {
-- main menu item
text_func = function ()
if self:enabled() then
local bedtime = self.settings:readSetting("bedtime", { hour = 0, min = 0})
return T(
_("Bedtime is at %1:%2"),
string.format("%02d", bedtime.hour),
string.format("%02d", bedtime.min)
)
else
return _("Set up bedtime")
end
end,
-- interaction with menu item
sub_item_table = {
-- set bedtime
{
text = _("Set bedtime"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local now_t = os.date("*t")
local time_picker = DateTimeWidget:new{
is_date = false,
hour = now_t.hour,
min = now_t.min,
-- ok_text = _("Confirm"),
title_text = _("Set bedtime"),
info_text = _("Enter a time in hours and minutes."),
callback = function(time)
-- UI
touchmenu_instance:closeMenu()
local confirmation = InfoMessage:new{
text = T(_("Bedtime is at %1:%2"),
string.format("%02d", time.hour),
string.format("%02d", time.min)
),
timeout = 5,
}
UIManager:show(confirmation)
-- actually save new value
self.settings:saveSetting("bedtime", {hour = time.hour, min = time.min})
-- and persist to disk
self.settings:flush()
-- remove previously scheduled callbacks
self:teardown()
-- setup callback
self:setup()
-- show current bedtime in menu
touchmenu_instance:updateItems()
end
}
UIManager:show(time_picker)
end
},
-- disable bedtime
{
text = _("Disable bedtime"),
keep_menu_open = true,
enabled_func = function() return self:enabled() end,
callback = function(touchmenu_instance)
self.settings:delSetting("bedtime")
self.settings:flush()
self:teardown()
touchmenu_instance:updateItems()
end
},
}
}
end
return GoToBed