diff --git a/README.md b/README.md index 96e2c91..0c97759 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ First line
Second line Dialog button clicks should be processed in the `on_dialog_action(number)` callback, where 1 is the first button, 2 is the second, and -1 is the "closed" button if no buttons were specified. `ui:show_edit_dialog()` returns the text in the `on_dialog_action(text)` callback. +If the first argument of the dialog contains two lines separated by `\n`, the second line becomes a subheading. + # Context menu * `ui:prepare_context_menu(table)` - prepares the context menu, which will be automatically shown when the item is held on the screen for a long time. diff --git a/README_ru.md b/README_ru.md index dc52f07..e7c2a9f 100644 --- a/README_ru.md +++ b/README_ru.md @@ -55,6 +55,8 @@ First line
Second line Нажатия на кнопки диалога должны обрабатываться в колбеке `on_dialog_action(number)`, где 1 - это первая кнопка, 2 - вторая, а -1 - нажатие кнопки "закрыть", если никакие кнопки не были указаны. `ui:show_edit_dialog()` возвращает текст в колбек `on_dialog_action(text)`. +Если первый аргумент диалога содержит две строки, разделенных знаком `\n`, вторая строка станет подзаголовком. + # Контекстное меню * `ui:prepare_context_menu(table)` - функция подготавливает контекстное меню, которое будет автоматически показано при долгом удержании элемента на экране. diff --git a/main/shell-widget.lua b/main/shell-widget.lua index d4cd858..e71c5c3 100644 --- a/main/shell-widget.lua +++ b/main/shell-widget.lua @@ -15,7 +15,7 @@ function redraw() end function on_click(idx) - ui:show_edit_dialog("Enter command") + ui:show_edit_dialog("Enter command\nAAAA") end function on_dialog_action(text) diff --git a/ru/calendar-ru-widget.lua b/ru/calendar-ru-widget.lua new file mode 100644 index 0000000..4d58802 --- /dev/null +++ b/ru/calendar-ru-widget.lua @@ -0,0 +1,170 @@ +-- name = "Календарь" +-- description = "Производственный календарь" +-- data_source = "https://isdayoff.ru/" +-- type = "widget" +-- author = "Andrey Gavrilov" +-- version = "1.0" + +local color = require "md_colors" + +local days = {} +local tab = {} + +local pr_text_color = ui:get_primary_text_color() +local sec_text_color = ui:get_secondary_text_color() +local year = os.date("*t").year +local month = os.date("*t").month + +function on_alarm() + is_days_off(year,month) +end + +function is_days_off(y,m) + http:get("https://isdayoff.ru/api/getdata?year="..y.."&month="..m.."&pre=1&delimeter=-&covid=1") +end + +function on_network_result(result) + days = result:split("-") + tab = get_cal(year,month) + ui:show_table(tab,8, true) + ui:set_title(ui:get_default_title().." ("..string.format("%02d.%04d",month,year)..")") +end + +function on_click(i) + if i == 1 then + local time = get_time(begin_month(year,month))-24*60*60 + local y,m,d = get_day(time) + year = y + month = m + elseif i == 8 then + local time = get_time(end_month(year,month))+24*60*60 + local y,m,d = get_day(time) + year = y + month = m + elseif i > 1 and i < 8 then + ui:show_edit_dialog("Введите месяц и год", "Формат - 12.2020. Пустое значение - текущий месяц", string.format("%02d.%04d", month, year)) + elseif (i-1)%8 ~= 0 and tab[i] ~= " " then + ui:show_toast(os.date("*t", get_time(year,month,tab[i]:match(">(%d+)<"))).yday) + return + else + return + end + is_days_off(year,month) +end + +function on_dialog_action(data) + if data == -1 then + return + elseif data == "" then + local date = os.date("*t") + if year == date.year and month == date.month then + return + end + year = date.year + month = date.month + is_days_off(year,month) + return + elseif not check_date(data) then + return + else + local m,y = data:match("(%d+)%.(%d+)") + if year == tonumber(y) and month == tonumber(m) then + return + end + end + local m,y = data:match("(%d+)%.(%d+)") + year = tonumber(y) + month = tonumber(m) + is_days_off(year,month) +end + +function get_cal(y,m) + local from = get_time(begin_month(y,m)) + local tab = { + "< #", + "Пн", + "Вт", + "Ср", + "Чт", + "Пт", + "Сб", + "Вс >" + } + table.insert(tab,""..weeknumber(get_day(from)).."") + for i =1, weekday(get_day(from))-1,1 do + table.insert(tab," ") + end + local to = get_time(end_month(y,m)) + local k=0 + for time = from,to,24*60*60 do + k=k+1 + local y,m,d = get_day(time) + table.insert(tab, format_day(d,k)) + if weekday(get_day(time))==7 and time ~= to then + table.insert(tab, ""..weeknumber(get_day(time+24*60*60)).."") + end + end + for i = weekday(get_day(to))+1,7,1 do + table.insert(tab," ") + end + return tab +end + +function weeknumber(y,m,d) + local yday = os.date("*t",os.time{day=d,month=m,year=y}).yday + local wday = weekday(y,m,1) + return math.floor((yday+10-wday)/7) +end + +function weekday(y,m,d) + local wday = os.date("*t",os.time{day=d,month=m,year=y}).wday-1 + if wday == 0 then wday = 7 end + return wday +end + +function begin_month(y,m) + local time = os.time{day=1,month=m,year=y} + local date = os.date("*t",time) + return date.year,date.month,date.day +end + +function end_month(y,m) + local time = os.time{day=1,month=m,year=y}+31*24*60*60 + local date = os.date("*t",time) + local time = os.time{day=1,month=date.month,year=date.year}-24*60*60 + local date = os.date("*t",time) + return date.year,date.month,date.day +end + +function get_day(t) + local date = os.date("*t",t) + return date.year,date.month,date.day +end + +function get_time(y,m,d) + return os.time{day=d,month=m,year=y} +end + +function format_day(d,k) + local day = d + if days[k] == "2" then + day = ""..day.."" + elseif days[k] == "1" then + day = ""..day.."" + elseif days[k] == "4" then + day = ""..day.."" + else + day = ""..day.."" + end + if year == os.date("*t").year and month == os.date("*t").month and d == os.date("*t").day then + day = ""..day.."" + end + return day + 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} + local str = string.format("%02d.%04d", m or 0, Y or 0) + return str == os.date("%m.%Y", time) +end diff --git a/samples/dialog_sample.lua b/samples/dialog_sample.lua index 944b724..5f51ce9 100644 --- a/samples/dialog_sample.lua +++ b/samples/dialog_sample.lua @@ -8,7 +8,7 @@ end function on_click(idx) if idx == 1 then - ui:show_dialog("Dialog title", "This is dialog") + ui:show_dialog("Dialog title\nSubtitle", "This is dialog") elseif idx == 2 then ui:show_dialog("Dialog title", "This is dialog", "Button 1", "Button 2") elseif idx == 3 then