Sorry if this has been covered before.
But I tried to use the Today Widget, today (I have tried before, but long time ago). Anyway, I was suprised about what I could do.
I was able to load a CustomView using @JonB PYUILoader (loading a pyui file into a Custom Class) , I was able to use the update method in the custom class to update a timer value. Interact with a database (a single call for now).
Anyway, it all just works. Well execpt for the bg_color. The way I am doing it, I end up with a white bg_color. I tried setting the bg_color = None, that does not work. I will have to find out how to do that.
but the below code is not runable on anothers device, I just wanted to start something and see if things like update method would work.
I am not sure if different devices get more memory to run today widgets or not.
So the below code is not meant to be smart/polished. Just wanted to see if it would work.
#!python3
import appex
import ui
import arrow
from tinydb import TinyDB, where
class PYUILoader(ui.View):
# this acts as a normal Custom ui.View class
# the root view of the class is the pyui file read in
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
def __init__(self, pyui_fn, *args, **kwargs):
bindings = globals().copy()
bindings[self.__class__.__name__] = self.WrapInstance()
ui.load_view(pyui_fn, bindings)
# call after so our kwargs modify attrs
super().__init__(*args, **kwargs)
def GetTimerFinished(db_fn, timer_name):
with TinyDB(db_fn) as db:
rec = db.search(where('timer_name') == timer_name)
return rec[0]
def SetTimerFinished(db_fn, timer_name, days=0, hours=0, minutes=0):
utc = arrow.utcnow()
finish_time = utc.shift(days=+days, hours=+hours, minutes=+minutes)
rec = dict(timer_name=timer_name,
entered=utc.for_json(),
finish_time=finish_time.for_json(),
elapased=False,
)
with TinyDB(db_fn) as db:
db.upsert(rec, where('timer_name') == rec['timer_name'])
return rec
class AppexTestClass(PYUILoader):
def __init__(self, pyui_fn, db_fn, timer_name, *args, **kwargs):
super().__init__(pyui_fn, *args, **kwargs)
#self.bg_color = None
self.db_fn = db_fn
self.timer_name = timer_name
self.local_finish_time = 0
rec = GetTimerFinished(db_fn, self.timer_name)
self.update_interval = 1
self.count = 0
self.expired = False
self.calc_time()
self['f_timer_name'].text = rec['timer_name']
def calc_time(self):
timer_rec = GetTimerFinished(self.db_fn, self.timer_name)
self.timer_name = timer_rec['timer_name']
utc = arrow.get(timer_rec['finish_time'])
local_finish_time = utc.to('Asia/Bangkok')
self.local_finish_time = local_finish_time
if self.local_finish_time < arrow.now():
self.expired = True
def time_remaining(self):
return self.local_finish_time - arrow.now()
def update(self):
if self.expired:
self['f_time_left'].text = 'Expired'
return
self['f_time_left'].text =\
self.format_timedelta(self.local_finish_time - arrow.now())
def format_timedelta(self, td):
hours, remainder = divmod(td.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
hours, minutes, seconds = int(hours), int(minutes), int(seconds)
if hours < 10:
hours = '0%s' % int(hours)
if minutes < 10:
minutes = '0%s' % minutes
if seconds < 10:
seconds = '0%s' % seconds
return '%s:%s:%s' % (hours, minutes, seconds)
def main():
db_fn = 'my_timers.json'
pyui_fn = 'appex_test_ui.pyui'
timer_name = 'Wonder Wars'
#SetTimerFinished(db_fn, timer_name, hours=5, minutes=35 )
v = AppexTestClass(pyui_fn, db_fn, timer_name)
appex.set_widget_view(v)
if __name__ == '__main__':
main()