update calendar widget

This commit is contained in:
Evgeny
2021-09-14 12:10:56 +03:00
parent d941be94f4
commit e9904277c7

View File

@@ -2,7 +2,7 @@
-- description = "Monthly calendar with system calendar events" -- description = "Monthly calendar with system calendar events"
-- type = "widget" -- type = "widget"
-- author = "Andrey Gavrilov" -- author = "Andrey Gavrilov"
-- version = "3.0" -- version = "3.1"
local tab = {} local tab = {}
local line = " " local line = " "
@@ -15,17 +15,17 @@ local month = os.date("%m"):gsub("^0","")
local day = os.date("%d"):gsub("^0","") local day = os.date("%d"):gsub("^0","")
function on_resume() function on_resume()
--ui:set_folding_flag(true) --ui:set_folding_flag(true)
ui:show_table(table_to_tables(tab,8),0, true, line) ui:show_table(table_to_tables(tab,8),0, true, line)
widget_type = "table" widget_type = "table"
end end
function on_alarm() function on_alarm()
if next(settings:get()) == nil then if next(settings:get()) == nil then
settings:set(get_all_cals()[2]) settings:set(get_all_cals()[2])
end end
tab = get_cal(year,month) tab = get_cal(year,month)
line = get_line() line = get_line()
ui:show_table(table_to_tables(tab,8),0, true, line) ui:show_table(table_to_tables(tab,8),0, true, line)
ui:set_title(ui:get_default_title().." ("..get_date(month,year)..")") ui:set_title(ui:get_default_title().." ("..get_date(month,year)..")")
widget_type = "table" widget_type = "table"
@@ -37,38 +37,41 @@ function on_settings()
end end
function on_click(i) function on_click(i)
if widget_type == "table" then if widget_type == "table" then
if i == 1 then if i == 1 then
local time = os.time{year=year,month=month,day=1}-24*60*60 system:vibrate(10)
year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)") local time = os.time{year=year,month=month,day=1}-24*60*60
year,month = year:gsub("^0",""),month:gsub("^0","") year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)")
on_alarm() year,month = year:gsub("^0",""),month:gsub("^0","")
elseif i == 8 then on_alarm()
local time = os.time{year=year,month=month,day=1}+31*24*60*60 elseif i == 8 then
year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)") system:vibrate(10)
year,month = year:gsub("^0",""),month:gsub("^0","") local time = os.time{year=year,month=month,day=1}+31*24*60*60
on_alarm() year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)")
elseif i > 1 and i < 8 then year,month = year:gsub("^0",""),month:gsub("^0","")
dialog_id = "date" on_alarm()
ui:show_edit_dialog("Enter month and year", "Format - 12.2020. Empty value - current month", string.format("%02d.%04d", month, year)) elseif i > 1 and i < 8 then
return dialog_id = "date"
elseif (i-1)%8 ~= 0 and tab[i] ~= " " then ui:show_edit_dialog("Enter month and year", "Format - 12.2020. Empty value - current month", string.format("%02d.%04d", month, year))
day = tab[i]:match(">(%d+)<"):gsub("^0","") return
events = get_day_tab(get_my_events(year,month,day)) elseif (i-1)%8 ~= 0 and tab[i] ~= " " then
if next(events) ~= nil then day = tab[i]:match(">(%d+)<"):gsub("^0","")
widget_type = "lines" events = get_day_tab(get_my_events(year,month,day))
if next(events) ~= nil then
widget_type = "lines"
ui:show_table(get_lines(events),2, false, line) ui:show_table(get_lines(events),2, false, line)
end end
return else
else return
return end
end elseif widget_type == "lines" then
else if math.ceil(i/3) <= #events then
if math.ceil(i/3) <= #events then dialog_id = "event"
calendar:show_event_dialog(events[math.ceil(i/3)][1]) calendar:show_event_dialog(events[math.ceil(i/3)][1])
else else
on_resume() widget_type = "table"
end ui:show_table(table_to_tables(tab,8),0, true, line)
end
end end
end end
@@ -98,18 +101,18 @@ function on_dialog_action(data)
return return
end end
end end
local month,year = data:match("(%d+)%.(%d+)") month,year = data:match("(%d+)%.(%d+)")
month,year = month:gsub("^0",""),year:gsub("^0","") month,year = month:gsub("^0",""),year:gsub("^0","")
on_alarm() on_alarm()
elseif dialog_id == "settings" then elseif dialog_id == "settings" then
settings:set(id_to_cal_id(data)) settings:set(id_to_cal_id(data))
on_alarm() on_alarm()
end end
end end
function get_cal(y,m,days) function get_cal(y,m)
local color = ui:get_colors() local color = ui:get_colors()
local events = get_my_events(y,m,0) local events = get_my_events(y,m,0)
local from = os.time{year=y,month=m,day=1} local from = os.time{year=y,month=m,day=1}
local tab = { local tab = {
"ᐊ <font color=\""..color.secondary_text.."\">#</font>", "ᐊ <font color=\""..color.secondary_text.."\">#</font>",
@@ -143,32 +146,31 @@ function get_cal(y,m,days)
end end
function format_day(y,m,d,events) function format_day(y,m,d,events)
local color = ui:get_colors() local color = ui:get_colors()
local from = os.time{year=y,month=m,day=d,hour=0,min=0,sec=0} local from = os.time{year=y,month=m,day=d,hour=0,min=0,sec=0}
local to = os.time{year=y,month=m,day=d,hour=23,min=59,sec=59} local to = os.time{year=y,month=m,day=d,hour=23,min=59,sec=59}
local yes = false local yes = false
for i=1,#events do for i=1,#events do
local v = events[i] local v = events[i]
if v.begin >= from and v["end"] <= to then if v.begin >= from and v["end"] <= to then
yes = true yes = true
break break
end end
end end
local dd = d local dd = d
if yes then if yes then
dd = "<b>"..dd.."</b>" dd = "<b>"..dd.."</b>"
end end
if year == os.date("%Y"):gsub("^0","") and month == os.date("%m"):gsub("^0","") and d == os.date("%d"):gsub("^0","") then if year == os.date("%Y"):gsub("^0","") and month == os.date("%m"):gsub("^0","") and d == os.date("%d"):gsub("^0","") then
dd = "<font color=\""..color.progress_good.."\">"..dd.."</font>" dd = "<font color=\""..color.progress_good.."\">"..dd.."</font>"
elseif os.date("%w",from):gsub("0","7")-5 > 0 then elseif os.date("%w",from):gsub("0","7")-5 > 0 then
dd = "<font color=\""..color.progress_bad.."\">"..dd.."</font>" dd = "<font color=\""..color.progress_bad.."\">"..dd.."</font>"
else else
dd = "<font color=\""..color.primary_text.."\">"..dd.."</font>" dd = "<font color=\""..color.primary_text.."\">"..dd.."</font>"
end end
return dd return dd
end end
function check_date(date) function check_date(date)
local m, Y = date:match("(%d+).(%d+)") local m, Y = date:match("(%d+).(%d+)")
local time = os.time{day=1, month=m or 0, year=Y or 0} local time = os.time{day=1, month=m or 0, year=Y or 0}
@@ -177,102 +179,103 @@ function check_date(date)
end end
function table_to_tables(tab, num) function table_to_tables(tab, num)
local out_tab = {} local out_tab = {}
local row = {} local row = {}
for k,v in ipairs(tab) do for k,v in ipairs(tab) do
table.insert(row, v) table.insert(row, v)
if k % num == 0 then if k % num == 0 then
table.insert(out_tab, row) table.insert(out_tab, row)
row = {} row = {}
end end
end end
return out_tab return out_tab
end end
function get_date(m,y) function get_date(m,y)
local time = os.time{year=y,month=m,day=1} local time = os.time{year=y,month=m,day=1}
return os.date("%B",time).." "..string.format("%04d", y) return os.date("%B",time).." "..string.format("%04d", y)
end end
function get_my_events(y,m,d) function get_my_events(y,m,d)
local tab = {} local tab = {}
local from = os.time{year=y,month=m,day=1,hour=0,min=0,sec=0} local from = os.time{year=y,month=m,day=1,hour=0,min=0,sec=0}
local to = os.date("*t",from + 31*24*60*60) local to = os.date("*t",from + 31*24*60*60)
to = os.time{year=to.year,month=to.month,day=1,hour=0,min=0,sec=0}-1 to = os.time{year=to.year,month=to.month,day=1,hour=0,min=0,sec=0}-1
if d ~= 0 then if d ~= 0 then
from =os.time{year=y,month=m,day=d,hour=0,min=0,sec=0} from =os.time{year=y,month=m,day=d,hour=0,min=0,sec=0}
to = os.time{year=y,month=m,day=d,hour=23,min=59,sec=59} to = os.time{year=y,month=m,day=d,hour=23,min=59,sec=59}
end end
local events = calendar:get_events(from,to,settings:get()) local events = {}
for i=1,#events do if next(settings:get()) then
local v = events[i] events = calendar:get_events(from,to,settings:get())
if v.begin >= from and v["end"] <= to then end
v["calendar_name"],v["calendar_color"]=get_my_calendar(v.calendar_id) for i=1,#events do
table.insert(tab,v) local v = events[i]
end if v.begin >= from and v["end"] <= to then
v["calendar_name"],v["calendar_color"]=get_my_calendar(v.calendar_id)
table.insert(tab,v)
end
if v.begin > to then if v.begin > to then
break break
end end
end end
return tab return tab
end end
function get_line() function get_line()
local line = "" local line = ""
local date = os.date("*t") local date = os.date("*t")
local from = os.time{year=date.year,month=date.month,day=date.day,hour=0,min=0,sec=0} local from = os.time{year=date.year,month=date.month,day=date.day,hour=0,min=0,sec=0}
local to = os.time{year=date.year,month=date.month,day=date.day,hour=23,min=59,sec=59} local to = os.time{year=date.year,month=date.month,day=date.day,hour=23,min=59,sec=59}
local events = calendar:get_events(from,to,settings:get()) local events = calendar:get_events(from,to,settings:get())
if next(events) == nil then if next(events) == nil then
line = "No events today" line = "No events today"
else else
line = "There're events today" line = "There're events today"
end end
return line return line
end end
function get_my_calendar(id) function get_my_calendar(id)
local cals = calendar:get_calendars() local cals = calendar:get_calendars()
for i=1,#cals do for i=1,#cals do
local v = cals[i] local v = cals[i]
if id == v.id then if id == v.id then
return v.name,v.color return v.name,v.color
end end
end end
end end
function get_day_tab(events) function get_day_tab(events)
local tab = {} local tab = {}
for i,v in ipairs(events) do for i,v in ipairs(events) do
local t = {v.id, v.all_day, os.date("%H:%M",v.begin), os.date("%H:%M",v["end"]), v.title, v.description, v.location, v.calendar_name, v.calendar_color} local t = {v.id, v.all_day, os.date("%H:%M",v.begin), os.date("%H:%M",v["end"]), v.title, v.description, v.location, v.calendar_name, v.calendar_color}
table.insert(tab,t) table.insert(tab,t)
end end
return tab return tab
end end
function get_lines(events) function get_lines(events)
local color = ui:get_colors() local color = ui:get_colors()
local lines = {} local lines = {}
for i,v in ipairs(events) do for i,v in ipairs(events) do
table.insert(lines,"<font color = \""..v[9].."\">•</font>") table.insert(lines,"<font color = \""..v[9].."\">•</font>")
table.insert(lines,v[5]) table.insert(lines,v[5])
if v[2] == false then if v[2] == false then
table.insert(lines,"<font color = \""..color.secondary_text.."\">"..v[3].." - "..v[4].."</font>") table.insert(lines,"<font color = \""..color.secondary_text.."\">"..v[3].." - "..v[4].."</font>")
else else
table.insert(lines,"<font color = \""..color.secondary_text.."\">All day</font>") table.insert(lines,"<font color = \""..color.secondary_text.."\">All day</font>")
end end
end end
local t = table_to_tables(tab,8) local t = table_to_tables(tab,8)
for i = #lines/3+1,#t-1 do for i = #lines/3+1,#t-1 do
table.insert(lines," ") table.insert(lines," ")
table.insert(lines," ") table.insert(lines," ")
table.insert(lines," ") table.insert(lines," ")
end end
if #lines/3 < #t then table.insert(lines," ")
table.insert(lines," ") table.insert(lines,"☚ Back")
table.insert(lines,"☚ Back") table.insert(lines," ")
table.insert(lines," ")
end
return table_to_tables(lines,3) return table_to_tables(lines,3)
end end