From e9904277c75e9c9893f968524d37a0c63a26da69 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 14 Sep 2021 12:10:56 +0300 Subject: [PATCH] update calendar widget --- main/calendar.lua | 243 +++++++++++++++++++++++----------------------- 1 file changed, 123 insertions(+), 120 deletions(-) diff --git a/main/calendar.lua b/main/calendar.lua index 374eccf..6755c74 100644 --- a/main/calendar.lua +++ b/main/calendar.lua @@ -2,7 +2,7 @@ -- description = "Monthly calendar with system calendar events" -- type = "widget" -- author = "Andrey Gavrilov" --- version = "3.0" +-- version = "3.1" local tab = {} local line = " " @@ -15,17 +15,17 @@ local month = os.date("%m"):gsub("^0","") local day = os.date("%d"):gsub("^0","") function on_resume() - --ui:set_folding_flag(true) - ui:show_table(table_to_tables(tab,8),0, true, line) - widget_type = "table" + --ui:set_folding_flag(true) + ui:show_table(table_to_tables(tab,8),0, true, line) + widget_type = "table" end function on_alarm() if next(settings:get()) == nil then settings:set(get_all_cals()[2]) end - tab = get_cal(year,month) - line = get_line() + tab = get_cal(year,month) + line = get_line() ui:show_table(table_to_tables(tab,8),0, true, line) ui:set_title(ui:get_default_title().." ("..get_date(month,year)..")") widget_type = "table" @@ -37,38 +37,41 @@ function on_settings() end function on_click(i) - if widget_type == "table" then - if i == 1 then - local time = os.time{year=year,month=month,day=1}-24*60*60 - year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)") - year,month = year:gsub("^0",""),month:gsub("^0","") - on_alarm() - elseif i == 8 then - local time = os.time{year=year,month=month,day=1}+31*24*60*60 - year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)") - year,month = year:gsub("^0",""),month:gsub("^0","") - on_alarm() - elseif i > 1 and i < 8 then - dialog_id = "date" - ui:show_edit_dialog("Enter month and year", "Format - 12.2020. Empty value - current month", string.format("%02d.%04d", month, year)) - return - elseif (i-1)%8 ~= 0 and tab[i] ~= " " then - day = tab[i]:match(">(%d+)<"):gsub("^0","") - events = get_day_tab(get_my_events(year,month,day)) - if next(events) ~= nil then - widget_type = "lines" + if widget_type == "table" then + if i == 1 then + system:vibrate(10) + local time = os.time{year=year,month=month,day=1}-24*60*60 + year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)") + year,month = year:gsub("^0",""),month:gsub("^0","") + on_alarm() + elseif i == 8 then + system:vibrate(10) + local time = os.time{year=year,month=month,day=1}+31*24*60*60 + year,month = os.date("%Y-%m",time):match("(%d+)-(%d+)") + year,month = year:gsub("^0",""),month:gsub("^0","") + on_alarm() + elseif i > 1 and i < 8 then + dialog_id = "date" + ui:show_edit_dialog("Enter month and year", "Format - 12.2020. Empty value - current month", string.format("%02d.%04d", month, year)) + return + elseif (i-1)%8 ~= 0 and tab[i] ~= " " then + day = tab[i]:match(">(%d+)<"):gsub("^0","") + 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) - end - return - else - return - end - else - if math.ceil(i/3) <= #events then - calendar:show_event_dialog(events[math.ceil(i/3)][1]) - else - on_resume() - end + end + else + return + end + elseif widget_type == "lines" then + if math.ceil(i/3) <= #events then + dialog_id = "event" + calendar:show_event_dialog(events[math.ceil(i/3)][1]) + else + widget_type = "table" + ui:show_table(table_to_tables(tab,8),0, true, line) + end end end @@ -98,18 +101,18 @@ function on_dialog_action(data) return end end - local month,year = data:match("(%d+)%.(%d+)") + month,year = data:match("(%d+)%.(%d+)") month,year = month:gsub("^0",""),year:gsub("^0","") on_alarm() elseif dialog_id == "settings" then - settings:set(id_to_cal_id(data)) + settings:set(id_to_cal_id(data)) on_alarm() end end -function get_cal(y,m,days) - local color = ui:get_colors() - local events = get_my_events(y,m,0) +function get_cal(y,m) + local color = ui:get_colors() + local events = get_my_events(y,m,0) local from = os.time{year=y,month=m,day=1} local tab = { "ᐊ #", @@ -143,32 +146,31 @@ function get_cal(y,m,days) end function format_day(y,m,d,events) - local color = ui:get_colors() - 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 yes = false - for i=1,#events do - local v = events[i] - if v.begin >= from and v["end"] <= to then - yes = true + local color = ui:get_colors() + 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 yes = false + for i=1,#events do + local v = events[i] + if v.begin >= from and v["end"] <= to then + yes = true break - end - end + end + end local dd = d if yes then - dd = ""..dd.."" + dd = ""..dd.."" end if year == os.date("%Y"):gsub("^0","") and month == os.date("%m"):gsub("^0","") and d == os.date("%d"):gsub("^0","") then - dd = ""..dd.."" + dd = ""..dd.."" elseif os.date("%w",from):gsub("0","7")-5 > 0 then - dd = ""..dd.."" - else - dd = ""..dd.."" + dd = ""..dd.."" + else + dd = ""..dd.."" end - return dd end - + function check_date(date) local m, Y = date:match("(%d+).(%d+)") local time = os.time{day=1, month=m or 0, year=Y or 0} @@ -177,102 +179,103 @@ function check_date(date) end function table_to_tables(tab, num) - local out_tab = {} - local row = {} - for k,v in ipairs(tab) do - table.insert(row, v) - if k % num == 0 then - table.insert(out_tab, row) - row = {} - end - end - return out_tab + local out_tab = {} + local row = {} + for k,v in ipairs(tab) do + table.insert(row, v) + if k % num == 0 then + table.insert(out_tab, row) + row = {} + end + end + return out_tab end 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) end function get_my_events(y,m,d) - local tab = {} - 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) - to = os.time{year=to.year,month=to.month,day=1,hour=0,min=0,sec=0}-1 - if d ~= 0 then - 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} - end - local events = calendar:get_events(from,to,settings:get()) - for i=1,#events do - local v = events[i] - 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 + local tab = {} + 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) + to = os.time{year=to.year,month=to.month,day=1,hour=0,min=0,sec=0}-1 + if d ~= 0 then + 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} + end + local events = {} + if next(settings:get()) then + events = calendar:get_events(from,to,settings:get()) + end + for i=1,#events do + local v = events[i] + 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 break end - end - return tab + end + return tab end function get_line() - local line = "" - 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 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()) - if next(events) == nil then - line = "No events today" - else - line = "There're events today" - end - return line + local line = "" + 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 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()) + if next(events) == nil then + line = "No events today" + else + line = "There're events today" + end + return line end function get_my_calendar(id) local cals = calendar:get_calendars() - for i=1,#cals do - local v = cals[i] - if id == v.id then - return v.name,v.color - end - end + for i=1,#cals do + local v = cals[i] + if id == v.id then + return v.name,v.color + end + end end function get_day_tab(events) - local tab = {} - 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} - table.insert(tab,t) - end - return tab + local tab = {} + 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} + table.insert(tab,t) + end + return tab end function get_lines(events) local color = ui:get_colors() local lines = {} for i,v in ipairs(events) do - table.insert(lines,"") + table.insert(lines,"") table.insert(lines,v[5]) if v[2] == false then table.insert(lines,""..v[3].." - "..v[4].."") else - table.insert(lines,"All day") + table.insert(lines,"All day") end end local t = table_to_tables(tab,8) 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 - if #lines/3 < #t then - table.insert(lines," ") - table.insert(lines,"☚ Back") - table.insert(lines," ") - end + table.insert(lines," ") + table.insert(lines,"☚ Back") + table.insert(lines," ") return table_to_tables(lines,3) end