-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbirthdays-widget.lua
More file actions
116 lines (105 loc) · 2.6 KB
/
birthdays-widget.lua
File metadata and controls
116 lines (105 loc) · 2.6 KB
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
-- name = "Birthdays"
-- name_id = "birthday"
-- description = "Shows upcoming birthdays from the contacts"
-- type = "widget"
-- author = "Andrey Gavrilov"
-- version = "1.3"
local prefs = require "prefs"
local fmt = require "fmt"
local months = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
}
function on_resume()
if not prefs.count then
prefs.count = 10
end
phone:request_permission()
end
function on_permission_granted()
contacts = calendar:contacts_events()
redraw()
end
function on_contacts_loaded()
redraw()
end
function redraw()
table.sort(contacts,function(a,b) return a.begin < b.begin end)
events = {}
local lines = {}
local lines_exp = {}
prev_begin = contacts[1].begin
for i,v in ipairs(contacts) do
local fmt_out = fmt_line(v)
local insert = 0
if #lines == 0 then
insert = 1
elseif not (fmt_out == lines[#lines]) then
insert = 1
end
if insert == 1 then
if #lines_exp >= prefs.count and prev_begin ~= v.begin then
break
end
table.insert(events, v)
if #lines < prefs.count then
table.insert(lines, fmt_out)
end
table.insert(lines_exp, fmt_out)
prev_begin = v.begin
end
end
if ui.set_expandable then
if #lines_exp == #lines then
ui:set_expandable(false)
else
ui:set_expandable(true)
end
end
if ui.is_expanded and ui:is_expanded() then
ui:show_lines(lines_exp)
else
ui:show_lines(lines)
end
end
function fmt_line(event)
local line = event.title
if os.date("%y%m%d",event.begin) == os.date("%y%m%d") then
line = fmt.bold(fmt.colored(line, aio:colors().accent))
end
return line .. fmt.secondary(" - ") .. fmt_date(event.begin)
end
function fmt_date(date)
local d = ""
if os.date("%y%m%d",date) == os.date("%y%m%d") then
d = "Today"
elseif os.date("%y%m%d",date-86400) == os.date("%y%m%d") then
d = "Tomorrow"
else
d = months[tonumber(os.date("%m", date))] .. ", " .. tostring(tonumber(os.date("%d", date)))
end
return fmt.secondary(d)
end
function on_click(idx)
calendar:show_event_dialog(events[idx])
end
function on_settings()
dialogs:show_radio_dialog("Number of events", {1,2,3,4,5,6,7,8,9,10}, prefs.count)
end
function on_dialog_action(idx)
if idx == -1 then
return
end
prefs.count = idx
redraw()
end