From 7aa368dacae1da9e1f0120a8f4e90662e75737ae Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 15 Jul 2023 13:51:26 +0400 Subject: [PATCH] add permission checks to main widgets --- main/calendar-menu.lua | 12 ++++++++++++ main/calendar.lua | 14 ++++++++++++++ main/contacts-menu.lua | 13 +++++++++++++ 3 files changed, 39 insertions(+) diff --git a/main/calendar-menu.lua b/main/calendar-menu.lua index 4fb20da..af4f3b8 100644 --- a/main/calendar-menu.lua +++ b/main/calendar-menu.lua @@ -8,11 +8,19 @@ local fmt = require "fmt" +local have_permission = false local events = {} function on_drawer_open() events = calendar:events() + if events == "permission_error" then + calendar:request_permission() + return + end + + have_permission = true + lines = map(events, function(it) local date = fmt.colored(os.date("%d.%m", it.begin), it.color) return date..fmt.space(4)..it.title @@ -22,10 +30,14 @@ function on_drawer_open() end function on_click(idx) + if not have_permission then return end + calendar:show_event_dialog(events[idx].id) end function on_long_click(idx) + if not have_permission then return end + calendar:open_event(events[idx].id) end diff --git a/main/calendar.lua b/main/calendar.lua index 59aaa3e..e0a6efa 100644 --- a/main/calendar.lua +++ b/main/calendar.lua @@ -15,12 +15,20 @@ local month = os.date("%m"):gsub("^0","") local day = os.date("%d"):gsub("^0","") function on_resume() + if widget_type == "text" then + return + end --ui:set_folding_flag(true) ui:show_table(table_to_tables(tab,8),0, true, line) widget_type = "table" end function on_alarm() + if calendar:events(0,0) == "permission_error" then + widget_type = "text" + ui:show_text("Click to grant permission") + return + end if next(settings:get()) == nil then settings:set(get_all_cals()[2]) end @@ -72,9 +80,15 @@ function on_click(i) widget_type = "table" ui:show_table(table_to_tables(tab,8),0, true, line) end + elseif widget_type == "text" then + calendar:request_permission() end end +function on_permission_granted() + on_alarm() +end + function on_dialog_action(data) if data == -1 then events = {} diff --git a/main/contacts-menu.lua b/main/contacts-menu.lua index 37e44cd..36013e6 100644 --- a/main/contacts-menu.lua +++ b/main/contacts-menu.lua @@ -6,7 +6,18 @@ -- author = "Evgeny Zobnin" -- version = "1.0" +local have_permission = false + function on_drawer_open() + local raw_contacts = phone:contacts() + + if raw_contacts == "permission_error" then + phone:request_permission() + return + end + + have_permission = true + contacts = distinct_by_name( sort_by_name(phone:contacts()) ) @@ -22,6 +33,8 @@ function on_icons_ready(icons) end function on_click(idx) + if not have_permission then return end + phone:show_contact_dialog(contacts[idx].lookup_key) end