From a1b6f732218202ed8055f0fcbb60bea309452a6b Mon Sep 17 00:00:00 2001 From: Evgeny Date: Wed, 1 Sep 2021 07:50:17 +0300 Subject: [PATCH] add get_colors function --- README.md | 3 +-- README_ru.md | 3 +-- community/btc-widget.lua | 4 ++-- community/currency-widget-ru.lua | 2 +- community/icndb-translate-widget.lua | 14 +++++++------- main/covid-widget.lua | 4 ++-- main/currencies-widget.lua | 2 +- ru/calendar-ru-widget.lua | 6 +++--- samples/colors-sample.lua | 17 +++++++++++++++++ 9 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 samples/colors-sample.lua diff --git a/README.md b/README.md index 7299337..9e63817 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,8 @@ For most network scripts `on_alarm()` should be used. * `ui:show_toast(string)` - shows informational message in Android style; * `ui:get_default_title()` - returns the standard widget title (set in the `name` metadata); * `ui:set_title()` - changes the title of the widget, should be called before the data display function (empty line - reset to the standard title); -* `ui:get_primary_text_color()` - returns the color of the theme text in #XXXXXX format; -* `ui:get_secondary_text_color()` - returns the secondary text color (usually gray) in #XXXXXX format; * `ui:set_folding_flag(boolean)` - sets or clears the flag of the folded mode of the widget, the function should be called before the data display functions; +* `ui:get_colors()` - returns table with current theme colors; When you click on any element of the interface, the `on_click(number)` callback will be executed, where number is the ordinal number of the element. A long click calls `on_long_click(number)`. For example, if you use `ui:show_buttons` to show three buttons, then clicking the first button will call `on_click` with argument 1, the second with arguments 2, and so on. If there is only one element on the screen, the argument will always be equal to one and can be omitted. diff --git a/README_ru.md b/README_ru.md index 1a1f9c1..a773279 100644 --- a/README_ru.md +++ b/README_ru.md @@ -25,9 +25,8 @@ * `ui:show_toast(string)` - показывает информационное сообщение в стиле Android; * `ui:get_default_title()` - возвращает стандартный заголовок виджета (задается в метаданных `name`); * `ui:set_title()` - изменяет заголовок виджета, функцию следует вызывать до функции отображения данных (пустая строка - сброс до стандартного заголовка); -* `ui:get_primary_text_color()` - возвращает цвет текста темы в формате #XXXXXX; -* `ui:get_secondary_text_color()` - возвращает цвет вторичного текста (обычно серый) в формате #XXXXXX; * `ui:set_folding_flag(boolean)` - устанавливает или снимает флаг свернутого режима виджета, функцию следует вызывать до функций отображения данных; +* `ui:get_colors()` - возвращает таблицу текущих цветов темы; При нажатии на любой элемент интерфейса будет выполнен колбек `on_click(number)`, где number - это порядковый номер элемента. Долгое нажатие вызывает `on_long_click(number)`. Например, если вы используете `ui:show_buttons` для показа трех кнопок, то при нажатии первой кнопки будет вызван `on_click` с аргументом 1, второй - с аргументов 2, и так далее. Если элемент на экране всего один - аргумент всегда будет равен единице и его можно будет опустить. diff --git a/community/btc-widget.lua b/community/btc-widget.lua index 009ecb4..8164827 100644 --- a/community/btc-widget.lua +++ b/community/btc-widget.lua @@ -4,10 +4,10 @@ -- author = "Evgeny Zobnin (zobnin@gmail.com)" -- version = "1.0" -equals = " = " +equals = " = " function on_alarm() - http:get("https://api.blockchain.info/ticker") + http:get("https://api.blockchain.info/ticker") end function on_network_result(result) diff --git a/community/currency-widget-ru.lua b/community/currency-widget-ru.lua index c8477f0..3414967 100644 --- a/community/currency-widget-ru.lua +++ b/community/currency-widget-ru.lua @@ -8,7 +8,7 @@ local json = require "json" local color = require "md_colors" -local text_color = ui:get_secondary_text_color() +local text_color = ui:get_colors().secondary_text local equals = " = " -- константы -- diff --git a/community/icndb-translate-widget.lua b/community/icndb-translate-widget.lua index 2b6bc8b..5384e18 100644 --- a/community/icndb-translate-widget.lua +++ b/community/icndb-translate-widget.lua @@ -8,13 +8,13 @@ local joke = "" local txt = "" -function urlencode(str) - if (str) then +function urlencode(str) + if (str) then str = string.gsub(str, "\n", "\r\n") str = string.gsub(str, "([^%w _ %- . ~])", function (c) return string.format ("%%%02X", string.byte(c)) end) - str = string.gsub(str, " ", "+") - end - return str + str = string.gsub(str, " ", "+") + end + return str end function urldecode(str) @@ -24,12 +24,12 @@ function urldecode(str) end function on_alarm() - http:get("http://api.icndb.com/jokes/random") + http:get("http://api.icndb.com/jokes/random") end function on_network_result(result) local first_letter = string.sub(result, 1, 1) - local textColor = ui:get_secondary_text_color() + local textColor = ui:get_colors().secondary_text joke = ajson:get_value(result, "object object:value string:joke") local id = ajson:get_value(result, "object object:value string:id") diff --git a/main/covid-widget.lua b/main/covid-widget.lua index c0f934c..f9321bf 100644 --- a/main/covid-widget.lua +++ b/main/covid-widget.lua @@ -5,7 +5,7 @@ -- author = "Evgeny Zobnin (zobnin@gmail.com)" -- version = "1.0" -equals = " = " +equals = " = " function on_alarm() http:get("https://api.covid19api.com/summary") @@ -16,7 +16,7 @@ function on_network_result(result) local total = ajson:get_value(result, "object object:Global int:TotalConfirmed") local newDeaths = ajson:get_value(result, "object object:Global int:NewDeaths") local totalDeaths = ajson:get_value(result, "object object:Global int:TotalDeaths") - + ui:show_lines({ "Disease | total"..equals..comma_value(total).." | new"..equals..comma_value(new), "Deaths | total"..equals..comma_value(totalDeaths).." | new"..equals..comma_value(newDeaths) diff --git a/main/currencies-widget.lua b/main/currencies-widget.lua index b620a92..5963897 100644 --- a/main/currencies-widget.lua +++ b/main/currencies-widget.lua @@ -12,7 +12,7 @@ json = require "json" -- constants local red_color = "#f44336" local green_color = "#48ad47" -local text_color = ui:get_secondary_text_color() +local text_color = ui:get_colors().secondary_text local equals = " = " -- global vars diff --git a/ru/calendar-ru-widget.lua b/ru/calendar-ru-widget.lua index f21d5af..0e87661 100644 --- a/ru/calendar-ru-widget.lua +++ b/ru/calendar-ru-widget.lua @@ -14,8 +14,8 @@ local tab = {} local line = "" local text = "" -local pr_text_color = ui:get_primary_text_color() -local sec_text_color = ui:get_secondary_text_color() +local pr_text_color = ui:get_colors().primary_text +local sec_text_color = ui:get_colors().secondary_text local year = os.date("*t").year local month = os.date("*t").month local day = os.date("*t").day @@ -206,7 +206,7 @@ function format_day(d,k) 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} diff --git a/samples/colors-sample.lua b/samples/colors-sample.lua new file mode 100644 index 0000000..328be81 --- /dev/null +++ b/samples/colors-sample.lua @@ -0,0 +1,17 @@ +function on_resume() + local colors = ui:get_colors() + local colors_strings = stringify_table(colors) + + ui:show_lines(colors_strings) +end + +function stringify_table(tab) + local new_tab = {} + + for k,v in pairs(tab) do + table.insert(new_tab, k..": "..tostring(v)) + end + + return new_tab +end +