lists with datasource- proper separation of duties

This commit is contained in:
2026-01-12 17:24:42 +01:00
parent 7a8b3928a6
commit 65bb3c7b2d
3 changed files with 31 additions and 88 deletions
+5 -2
View File
@@ -35,14 +35,17 @@ class JustEatIt(toga.App):
def startup(self): def startup(self):
self.configuration = Configuration(self) self.configuration = Configuration(self)
self.data = LocalData(self)
self.localdata = LocalData(self)
self.login_screen = Login() self.login_screen = Login()
self.register_screen = Register() self.register_screen = Register()
self.providekey_screen = ProvideKey() self.providekey_screen = ProvideKey()
self.lists_screen = Lists() self.lists_screen = Lists(self.localdata.lists)
self.backend = BackEnd(self) self.backend = BackEnd(self)
self.localdata.lists.data_widget = self.lists_screen.data_widget.source
self.main_window = toga.MainWindow(title=self.formal_name) self.main_window = toga.MainWindow(title=self.formal_name)
# here comes the startup logic. # here comes the startup logic.
+13 -67
View File
@@ -10,10 +10,12 @@ class DatabaseList(toga.Box):
def __init__(self, id=None, style=None, children=None, **kwargs): def __init__(self, id=None, style=None, children=None, **kwargs):
style.direction = COLUMN style.direction = COLUMN
super().__init__(id, style, children, **kwargs) super().__init__(id, style, children, **kwargs)
self.data = ListSource(
self.source = ListSource(
accessors=["id", "owner_user_id", "name", "is_active"] 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): class DataListener(Listener):
# listen to the database # listen to the database
@@ -88,7 +90,7 @@ class DatabaseList(toga.Box):
class Lists(BasePage): class Lists(BasePage):
def __init__(self, *args, **kwargs): def __init__(self, datasource, *args, **kwargs):
""" """
This page allows you to enter the aplication key This page allows you to enter the aplication key
The aplication-key is kept until a month after the app is last used 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.first_menu_button_callback = self.edit_button_pressed
self.second_menu_button_icon = 'logout' self.second_menu_button_icon = 'logout'
self.second_menu_button_callback = self.logout_button_pressed self.second_menu_button_callback = self.logout_button_pressed
self.datasource = datasource
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# here the list of lists # here the list of lists
self.listholder = DatabaseList( self.data_widget = DatabaseList(
style=Pack( style=Pack(
margin_bottom=10, margin_bottom=10,
# background_color="white", # background_color="white",
) ),
) )
scrollarea = toga.ScrollContainer( scrollarea = toga.ScrollContainer(
content=self.listholder, content=self.data_widget,
style=Pack( style=Pack(
flex=1, flex=1,
margin=10, margin=10,
@@ -126,16 +127,6 @@ class Lists(BasePage):
horizontal=False horizontal=False
) )
# for i in range(30):
# listholder.add(self.create_row("Supermarkt",f"{i}","30"))
# at the bottom of the list: edit-button # at the bottom of the list: edit-button
save_button = toga.Button( save_button = toga.Button(
icon=toga.Icon("images/edit"), icon=toga.Icon("images/edit"),
@@ -160,56 +151,11 @@ class Lists(BasePage):
def on_show(self): def on_show(self):
super().on_show() super().on_show()
self.listholder.data.clear() self.datasource.reload()
for l in self.app.data.reload_list():
print(type(l))
self.listholder.data.append(l)
# self.listholder.data.data = self.app.data.reload_list()
def edit_button_pressed(self): def edit_button_pressed(self):
self.app.goto_next_page_by_name(PAGE_EDITLISTS) 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)
+13 -19
View File
@@ -1,28 +1,22 @@
import datetime import datetime
import asyncio import asyncio
import toga
from toga.sources import ListSource
class ListOfLists(): class ListsData():
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
# self.listento = widget_to_listen_to self.data_widget = None
self.data = []
# self.widget_data = list_source_object
# self.reload()
# 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.app = app
self.curent_userid = 0 self.curent_userid = 0
self.current_listid = 0 self.current_listid = 0
self.lists = ListOfLists(app) self.lists = ListsData(app)
def set_user(self, userid): def set_user(self, userid):
if userid != self.curent_userid: if userid != self.curent_userid: