DateTimeWidget: replaces DateWidget and TimeWidget (#8240)

Deduplicate code by combining DateWidget and TimeWidget.
ReadTimer plugin: some rewording.
reviewable/pr8264/r1
zwim 3 years ago committed by GitHub
parent 84baf00416
commit 9ed22df03f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,11 +1,10 @@
local DateWidget = require("ui/widget/datewidget")
local DateTimeWidget = require("ui/widget/datetimewidget")
local Device = require("device")
local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local Language = require("ui/language")
local NetworkMgr = require("ui/network/manager")
local UIManager = require("ui/uimanager")
local TimeWidget = require("ui/widget/timewidget")
local _ = require("gettext")
local N_ = _.ngettext
local Screen = Device.screen
@ -131,7 +130,8 @@ if Device:setDateTime() then
local now_t = os.date("*t")
local curr_hour = now_t.hour
local curr_min = now_t.min
local time_widget = TimeWidget:new{
local time_widget = DateTimeWidget:new{
is_date = false,
hour = curr_hour,
min = curr_min,
ok_text = _("Set time"),
@ -161,7 +161,7 @@ if Device:setDateTime() then
local curr_year = now_t.year
local curr_month = now_t.month
local curr_day = now_t.day
local date_widget = DateWidget:new{
local date_widget = DateTimeWidget:new{
year = curr_year,
month = curr_month,
day = curr_day,

@ -15,25 +15,35 @@ local TextBoxWidget = require("ui/widget/textboxwidget")
local TextWidget = require("ui/widget/textwidget")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local VerticalSpan = require("ui/widget/verticalspan")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext")
local Screen = Device.screen
local DateWidget = InputContainer:new{
local DateTimeWidget = InputContainer:new{
title_face = Font:getFace("x_smalltfont"),
info_text = nil,
width = nil,
height = nil,
is_date = true,
day = 1,
month = 1,
year = 2021,
hour = 12,
hour_max = 23,
min = 0,
ok_text = _("Apply"),
cancel_text = _("Close"),
-- Optional extra button on bottom
extra_text = nil,
extra_callback = nil,
}
function DateWidget:init()
function DateTimeWidget:init()
self.screen_width = Screen:getWidth()
self.screen_height = Screen:getHeight()
self.width = self.width or math.floor(math.min(self.screen_width, self.screen_height) * 0.8)
self.width = self.width or math.floor(math.min(self.screen_width, self.screen_height) *
(self.is_date and 0.8 or 0.6))
if Device:hasKeys() then
self.key_events = {
Close = { {"Back"}, doc = "close date widget" }
@ -57,7 +67,7 @@ function DateWidget:init()
self:update()
end
function DateWidget:update()
function DateTimeWidget:update()
local year_widget = NumberPickerWidget:new{
show_parent = self,
value = self.year,
@ -66,39 +76,44 @@ function DateWidget:update()
value_step = 1,
value_hold_step = 4,
}
local month_widget = NumberPickerWidget:new{
local month_hour_widget = NumberPickerWidget:new{
show_parent = self,
value = self.month,
value_min = 1,
value_max = 12,
value = self.is_date and self.month or self.hour,
value_min = self.is_date and 1 or 0,
value_max = self.is_date and 12 or self.hour_max,
value_step = 1,
value_hold_step = 3,
}
local day_widget = NumberPickerWidget:new{
local day_min_widget = NumberPickerWidget:new{
show_parent = self,
value = self.day,
value_min = 1,
value_max = 31,
value = self.is_date and self.day or self.min,
value_min = self.is_date and 1 or 0,
value_max = self.is_date and 31 or 59,
value_step = 1,
value_hold_step = 5,
date_month = month_widget,
value_hold_step = self.is_date and 5 or 10,
date_month_hour = month_hour_widget,
date_year = year_widget,
}
local dash_space = TextBoxWidget:new{
text = "",
local separator_space = TextBoxWidget:new{
text = self.is_date and "" or ":",
alignment = "center",
face = self.title_face,
bold = true,
width = math.floor(math.min(self.screen_width, self.screen_height) * 0.02),
width = math.floor(math.min(self.screen_width, self.screen_height) *
(self.is_date and 0.02 or 0.05)),
}
local date_group = HorizontalGroup:new{
align = "center",
year_widget,
dash_space,
month_widget,
dash_space,
day_widget,
}
align = "center",
year_widget,
separator_space,
month_hour_widget,
separator_space,
day_min_widget,
}
if not self.is_date then
table.remove(date_group, 2)
table.remove(date_group, 1)
end
local date_title = FrameContainer:new{
padding = Size.padding.default,
@ -116,6 +131,21 @@ function DateWidget:update()
h = Size.line.thick,
}
}
local date_info
if self.info_text then
date_info = FrameContainer:new{
padding = Size.padding.default,
margin = Size.margin.small,
bordersize = 0,
TextBoxWidget:new{
text = self.info_text,
face = Font:getFace("x_smallinfofont"),
width = math.floor(self.width * 0.9),
}
}
else
date_info = VerticalSpan:new{ width = 0 }
end
local buttons = {
{
{
@ -129,8 +159,13 @@ function DateWidget:update()
callback = function()
if self.callback then
self.year = year_widget:getValue()
self.month = month_widget:getValue()
self.day = day_widget:getValue()
if self.is_date then
self.month = month_hour_widget:getValue()
self.day = day_min_widget:getValue()
else
self.hour = month_hour_widget:getValue()
self.min = day_min_widget:getValue()
end
self:callback(self)
end
self:onClose()
@ -138,6 +173,22 @@ function DateWidget:update()
},
}
}
if self.extra_text then
table.insert(buttons,{
{
text = self.extra_text,
callback = function()
if self.extra_callback then
self.extra_callback(year_widget:getValue(), month_hour_widget:getValue(),
day_min_widget:getValue())
end
if not self.keep_shown_on_apply then -- assume extra wants it same as ok
self:onClose()
end
end,
},
})
end
local ok_cancel_buttons = ButtonTable:new{
width = self.width - 2*Size.padding.default,
@ -156,6 +207,7 @@ function DateWidget:update()
align = "left",
date_title,
date_line,
date_info,
CenterContainer:new{
dimen = Geom:new{
w = self.width,
@ -190,34 +242,34 @@ function DateWidget:update()
end)
end
function DateWidget:onCloseWidget()
function DateTimeWidget:onCloseWidget()
UIManager:setDirty(nil, function()
return "ui", self.date_frame.dimen
end)
end
function DateWidget:onShow()
function DateTimeWidget:onShow()
UIManager:setDirty(self, function()
return "ui", self.date_frame.dimen
end)
return true
end
function DateWidget:onAnyKeyPressed()
function DateTimeWidget:onAnyKeyPressed()
UIManager:close(self)
return true
end
function DateWidget:onTapClose(arg, ges_ev)
function DateTimeWidget:onTapClose(arg, ges_ev)
if ges_ev.pos:notIntersectWith(self.date_frame.dimen) then
self:onClose()
end
return true
end
function DateWidget:onClose()
function DateTimeWidget:onClose()
UIManager:close(self)
return true
end
return DateWidget
return DateTimeWidget

@ -1,208 +0,0 @@
local Blitbuffer = require("ffi/blitbuffer")
local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
local Device = require("device")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local Font = require("ui/font")
local HorizontalGroup = require("ui/widget/horizontalgroup")
local InputContainer = require("ui/widget/container/inputcontainer")
local LineWidget = require("ui/widget/linewidget")
local NumberPickerWidget = require("ui/widget/numberpickerwidget")
local Size = require("ui/size")
local TextBoxWidget = require("ui/widget/textboxwidget")
local TextWidget = require("ui/widget/textwidget")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext")
local Screen = Device.screen
local TimeWidget = InputContainer:new{
title_face = Font:getFace("x_smalltfont"),
width = nil,
height = nil,
hour = 0,
hour_max = 23,
min = 0,
ok_text = _("Apply"),
cancel_text = _("Close"),
}
function TimeWidget:init()
self.screen_width = Screen:getWidth()
self.screen_height = Screen:getHeight()
self.width = self.width or math.floor(math.min(self.screen_width, self.screen_height) * 0.6)
if Device:hasKeys() then
self.key_events = {
Close = { {"Back"}, doc = "close time widget" }
}
end
if Device:isTouchDevice() then
self.ges_events = {
TapClose = {
GestureRange:new{
ges = "tap",
range = Geom:new{
w = self.screen_width,
h = self.screen_height,
}
},
},
}
end
-- Actually the widget layout
self:update()
end
function TimeWidget:update()
local hour_widget = NumberPickerWidget:new{
show_parent = self,
value = self.hour,
value_min = 0,
value_max = self.hour_max,
value_step = 1,
value_hold_step = 4,
}
local min_widget = NumberPickerWidget:new{
show_parent = self,
value = self.min,
value_min = 0,
value_max = 59,
value_step = 1,
value_hold_step = 10,
}
local colon_space = TextBoxWidget:new{
text = ":",
alignment = "center",
face = self.title_face,
bold = true,
width = math.floor(math.min(self.screen_width, self.screen_height) * 0.05),
}
local time_group = HorizontalGroup:new{
align = "center",
hour_widget,
colon_space,
min_widget,
}
local time_title = FrameContainer:new{
padding = Size.padding.default,
margin = Size.margin.title,
bordersize = 0,
TextWidget:new{
text = self.title_text,
face = self.title_face,
max_width = self.width - 2 * (Size.padding.default + Size.margin.title),
},
}
local time_line = LineWidget:new{
dimen = Geom:new{
w = self.width,
h = Size.line.thick,
}
}
local buttons = {
{
{
text = self.cancel_text,
callback = function()
self:onClose()
end,
},
{
text = self.ok_text,
callback = function()
if self.callback then
self.hour = hour_widget:getValue()
self.min = min_widget:getValue()
self:callback(self)
end
self:onClose()
end,
},
}
}
local ok_cancel_buttons = ButtonTable:new{
width = self.width - 2*Size.padding.default,
buttons = buttons,
zero_sep = true,
show_parent = self,
}
self.time_frame = FrameContainer:new{
radius = Size.radius.window,
padding = 0,
margin = 0,
background = Blitbuffer.COLOR_WHITE,
VerticalGroup:new{
align = "left",
time_title,
time_line,
CenterContainer:new{
dimen = Geom:new{
w = self.width,
h = math.floor(time_group:getSize().h * 1.2),
},
time_group
},
CenterContainer:new{
dimen = Geom:new{
w = self.width,
h = ok_cancel_buttons:getSize().h,
},
ok_cancel_buttons
}
}
}
self[1] = WidgetContainer:new{
align = "center",
dimen = Geom:new{
x = 0, y = 0,
w = self.screen_width,
h = self.screen_height,
},
FrameContainer:new{
bordersize = 0,
self.time_frame,
}
}
UIManager:setDirty(self, function()
return "ui", self.time_frame.dimen
end)
end
function TimeWidget:onCloseWidget()
UIManager:setDirty(nil, function()
return "ui", self.time_frame.dimen
end)
end
function TimeWidget:onShow()
UIManager:setDirty(self, function()
return "ui", self.time_frame.dimen
end)
return true
end
function TimeWidget:onAnyKeyPressed()
UIManager:close(self)
return true
end
function TimeWidget:onTapClose(arg, ges_ev)
if ges_ev.pos:notIntersectWith(self.time_frame.dimen) then
self:onClose()
end
return true
end
function TimeWidget:onClose()
UIManager:close(self)
return true
end
return TimeWidget

@ -1,5 +1,5 @@
local DateTimeWidget = require("ui/widget/datetimewidget")
local InfoMessage = require("ui/widget/infomessage")
local TimeWidget = require("ui/widget/timewidget")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local logger = require("logger")
@ -80,17 +80,19 @@ function ReadTimer:addToMainMenu(menu_items)
end,
sub_item_table = {
{
text = _("Time"),
text = _("Set time"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local now_t = os.date("*t")
local curr_hour = now_t.hour
local curr_min = now_t.min
local time_widget = TimeWidget:new{
local time_widget = DateTimeWidget:new{
is_date = false,
hour = curr_hour,
min = curr_min,
ok_text = _("Set timer"),
title_text = _("Set reader timer"),
ok_text = _("Set alarm"),
title_text = _("New alarm"),
info_text = _("Enter a time in hours and minutes."),
callback = function(time)
touchmenu_instance:closeMenu()
self:unschedule()
@ -121,7 +123,7 @@ function ReadTimer:addToMainMenu(menu_items)
end,
},
{
text = _("Minutes from now"),
text = _("Set interval"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local remain_time = {}
@ -133,12 +135,14 @@ function ReadTimer:addToMainMenu(menu_items)
remain_minutes = remain_time[2]
end
end
local time_widget = TimeWidget:new{
local time_widget = DateTimeWidget:new{
is_date = false,
hour = remain_hours or 0,
min = remain_minutes or 0,
hour_max = 17,
ok_text = _("Set timer"),
title_text = _("Set reader timer from now (hours:minutes)"),
title_text = _("Set reader timer"),
info_text = _("Enter a time in hours and minutes."),
callback = function(time)
touchmenu_instance:closeMenu()
self:unschedule()

Loading…
Cancel
Save