Update counter widget to 2.0
This commit is contained in:
@@ -2,12 +2,14 @@
|
|||||||
-- description = "Time counting widget to fight bad habits"
|
-- description = "Time counting widget to fight bad habits"
|
||||||
-- type = "widget"
|
-- type = "widget"
|
||||||
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
|
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
|
||||||
-- arguments_help = "Enter the date of the start of counting in the format DD.MM.YYYY"
|
-- version = "2.0"
|
||||||
-- version = "1.0"
|
-- on_resume_when_folding = "true"
|
||||||
|
|
||||||
|
local prefs = require "prefs"
|
||||||
local date = require "date"
|
local date = require "date"
|
||||||
|
|
||||||
-- constants
|
-- constants
|
||||||
|
local max_counters = 5
|
||||||
local year = 365.25
|
local year = 365.25
|
||||||
local month = 30.43
|
local month = 30.43
|
||||||
|
|
||||||
@@ -18,48 +20,124 @@ local milestones = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local milestones_formatted = {
|
local milestones_formatted = {
|
||||||
"1 day", "3 days", "1 week", "2 weeks",
|
"1+ days", "3+ days", "1+ weeks", "2+ weeks",
|
||||||
"1 months", "3 months", "6 months",
|
"1+ months", "3+ months", "6+ months",
|
||||||
"1 year", "3 years", "5 years", "10 years", "20 years", "100 years"
|
"1+ years", "3+ years", "5+ years", "10+ years", "20+ years", "100+ years"
|
||||||
}
|
}
|
||||||
|
|
||||||
function on_resume()
|
function on_load()
|
||||||
local args = settings:get()
|
init_default_settings()
|
||||||
|
end
|
||||||
|
|
||||||
if next(args) == nil then
|
function on_resume()
|
||||||
ui:show_text("Tap to enter date")
|
local gui_inst = {}
|
||||||
return
|
local lines_num = max_counters
|
||||||
|
|
||||||
|
if ui:folding_flag() then
|
||||||
|
lines_num = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, lines_num do
|
||||||
|
local title, start_date = parse_settings_string(i)
|
||||||
|
if title == nil then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
local curr_date = date()
|
||||||
|
local passed = date.diff(curr_date, start_date)
|
||||||
|
local passed_days = math.floor(passed:spandays())
|
||||||
|
|
||||||
|
local idx = get_milestone_idx(passed)
|
||||||
|
local passed_str = passed_days.." days"
|
||||||
|
if prefs.show_milestones then
|
||||||
|
passed_str = passed_str.." / "..milestones_formatted[idx]
|
||||||
|
end
|
||||||
|
local next_milestone_percent = passed_days / milestones[idx+1] * 100
|
||||||
|
|
||||||
|
table.insert(gui_inst, {"progress", title..": "..passed_str, {progress = next_milestone_percent}})
|
||||||
|
end
|
||||||
|
|
||||||
|
gui_inst = insert_between_elements(gui_inst, {"new_line", 1})
|
||||||
|
gui(gui_inst):render()
|
||||||
|
end
|
||||||
|
|
||||||
|
function init_default_settings()
|
||||||
|
if prefs.counter_1 == nil then
|
||||||
|
prefs["counter_1"] = "Sample / 31.01.2024"
|
||||||
|
for i = 2,max_counters do
|
||||||
|
prefs["counter_"..i] = ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if prefs.show_milestones == nil then
|
||||||
|
prefs.show_milestones = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function parse_settings_string(i)
|
||||||
|
local title_and_date = prefs["counter_"..i]
|
||||||
|
if title_and_date == nil or #title_and_date == 0 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local splitted = title_and_date:split("/")
|
||||||
|
|
||||||
|
local title = trim(splitted[1])
|
||||||
|
if title == nil or #title == 0 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local date_str = trim(splitted[2])
|
||||||
|
if date_str == nil or #date_str == 0 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local arr = date_str:split("%.")
|
||||||
|
if arr == nil or #arr < 3 then
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local arr = args[1]:split("%.")
|
|
||||||
local start_date = date(arr[3], arr[2], arr[1])
|
local start_date = date(arr[3], arr[2], arr[1])
|
||||||
|
|
||||||
local curr_date = date()
|
return title, start_date
|
||||||
local passed = date.diff(curr_date, start_date)
|
|
||||||
local passed_days = math.floor(passed:spandays())
|
|
||||||
local idx = get_milestone_idx(passed)
|
|
||||||
|
|
||||||
ui:show_progress_bar(passed_days.." days / "..milestones_formatted[idx],
|
|
||||||
passed_days, milestones[idx])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_click()
|
function on_click()
|
||||||
settings:show_dialog()
|
prefs:show_dialog()
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_settings()
|
function on_settings()
|
||||||
settings:show_dialog()
|
prefs:show_dialog()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- utils
|
-- utils
|
||||||
|
|
||||||
|
function trim(s)
|
||||||
|
if s == nil or #s == 0 then return s end
|
||||||
|
return s:match("^%s*(.-)%s*$")
|
||||||
|
end
|
||||||
|
|
||||||
|
function insert_between_elements(t, element)
|
||||||
|
local new_table = {}
|
||||||
|
for i = 1, #t do
|
||||||
|
table.insert(new_table, t[i])
|
||||||
|
if i < #t then
|
||||||
|
table.insert(new_table, element)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return new_table
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function get_milestone_idx(passed)
|
function get_milestone_idx(passed)
|
||||||
local days_passed = passed:spandays()
|
local days_passed = passed:spandays()
|
||||||
local idx = 1
|
local idx = 0
|
||||||
|
|
||||||
for k,v in ipairs(milestones) do
|
for k,v in ipairs(milestones) do
|
||||||
if days_passed > v then
|
if days_passed > v then
|
||||||
idx = idx + 1
|
idx = idx + 1
|
||||||
|
else
|
||||||
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user