From 65bb3c7b2d68b882abbdedbf7684a54665bfcd37 Mon Sep 17 00:00:00 2001 From: Ignace Date: Mon, 12 Jan 2026 17:24:42 +0100 Subject: [PATCH] lists with datasource- proper separation of duties --- src/justgetit/app.py | 7 +++- src/justgetit/lists.py | 80 +++++++------------------------------- src/justgetit/localdata.py | 32 +++++++-------- 3 files changed, 31 insertions(+), 88 deletions(-) diff --git a/src/justgetit/app.py b/src/justgetit/app.py index da60c91..a8fd998 100644 --- a/src/justgetit/app.py +++ b/src/justgetit/app.py @@ -35,14 +35,17 @@ class JustEatIt(toga.App): def startup(self): self.configuration = Configuration(self) - self.data = LocalData(self) + + self.localdata = LocalData(self) self.login_screen = Login() self.register_screen = Register() self.providekey_screen = ProvideKey() - self.lists_screen = Lists() + self.lists_screen = Lists(self.localdata.lists) self.backend = BackEnd(self) + self.localdata.lists.data_widget = self.lists_screen.data_widget.source + self.main_window = toga.MainWindow(title=self.formal_name) # here comes the startup logic. diff --git a/src/justgetit/lists.py b/src/justgetit/lists.py index 4e9e486..8d1b7cd 100644 --- a/src/justgetit/lists.py +++ b/src/justgetit/lists.py @@ -10,10 +10,12 @@ class DatabaseList(toga.Box): def __init__(self, id=None, style=None, children=None, **kwargs): style.direction = COLUMN super().__init__(id, style, children, **kwargs) - self.data = ListSource( + + self.source = ListSource( accessors=["id", "owner_user_id", "name", "is_active"] ) - self.data.add_listener(DatabaseList.DataListener(self)) + self.source.add_listener(DatabaseList.DataListener(self)) + class DataListener(Listener): # listen to the database @@ -88,7 +90,7 @@ class DatabaseList(toga.Box): class Lists(BasePage): - def __init__(self, *args, **kwargs): + def __init__(self, datasource, *args, **kwargs): """ This page allows you to enter the aplication key The aplication-key is kept until a month after the app is last used @@ -104,21 +106,20 @@ class Lists(BasePage): self.first_menu_button_callback = self.edit_button_pressed self.second_menu_button_icon = 'logout' self.second_menu_button_callback = self.logout_button_pressed + self.datasource = datasource super().__init__(*args, **kwargs) # here the list of lists - self.listholder = DatabaseList( + self.data_widget = DatabaseList( style=Pack( margin_bottom=10, # background_color="white", - ) + ), ) - - scrollarea = toga.ScrollContainer( - content=self.listholder, + content=self.data_widget, style=Pack( flex=1, margin=10, @@ -126,16 +127,6 @@ class Lists(BasePage): horizontal=False ) - - - # for i in range(30): - # listholder.add(self.create_row("Supermarkt",f"{i}","30")) - - - - - - # at the bottom of the list: edit-button save_button = toga.Button( icon=toga.Icon("images/edit"), @@ -160,56 +151,11 @@ class Lists(BasePage): def on_show(self): super().on_show() - self.listholder.data.clear() - for l in self.app.data.reload_list(): - print(type(l)) - self.listholder.data.append(l) + self.datasource.reload() + + + - # self.listholder.data.data = self.app.data.reload_list() def edit_button_pressed(self): self.app.goto_next_page_by_name(PAGE_EDITLISTS) - - def create_row(self, text, open, total): - label = toga.Button( - text=text, - margin_bottom=2, - style = Pack( - # margin_bottom=2, # scrollig doesnt work, bug?, replaced by margin_bottom in Button - width=250, - background_color="white", - font_size=15 - ) - ) - spacer = toga.Box( - style = Pack( - flex=1 - ) - ) - indicator = toga.Label( - text = f"{open} / {total}", - style = Pack( - color="blue", - margin=8 - ) - ) - listrow_inner = toga.Box( - style = Pack( - margin_bottom=1, - flex=1, - background_color="white", - ) - ) - listrow_inner.add(label) - listrow_inner.add(spacer) - listrow_inner.add(indicator) - - listrow_outer = toga.Box( - style = Pack( - flex=1, - background_color="#ccc", - ) - ) - listrow_outer.add(listrow_inner) - - return(listrow_outer) \ No newline at end of file diff --git a/src/justgetit/localdata.py b/src/justgetit/localdata.py index bc544f2..08633cd 100644 --- a/src/justgetit/localdata.py +++ b/src/justgetit/localdata.py @@ -1,28 +1,22 @@ import datetime import asyncio +import toga +from toga.sources import ListSource -class ListOfLists(): +class ListsData(): def __init__(self, app): self.app = app - # self.listento = widget_to_listen_to - self.data = [] - # self.widget_data = list_source_object - # self.reload() + self.data_widget = None - # self.widget_data.append({"name": "ToDo", "id": 3, "owner_user_id": 3, "is_active": True, "updated_at": datetime.datetime.now() }) + def reload(self): + curent_userid = self.app.configuration.get_userid() + if curent_userid > 0: + parameters = {} + parameters['userid'] = curent_userid + result = self.app.backend.postwait('load_lists', parameters) + for item in result['data']: + self.data_widget.append(item) - def reload(self, userid): - parameters = {} - parameters['userid'] = userid - result = self.app.backend.postwait('load_lists', parameters) - return result['data'] - - - def on_reloaded(self, bla): - print("reloaded") - - def clear(self): - self.data = [] @@ -52,7 +46,7 @@ class LocalData(): self.app = app self.curent_userid = 0 self.current_listid = 0 - self.lists = ListOfLists(app) + self.lists = ListsData(app) def set_user(self, userid): if userid != self.curent_userid: