diff --git a/src/justgetit/app.py b/src/justgetit/app.py index a8fd998..073e35e 100644 --- a/src/justgetit/app.py +++ b/src/justgetit/app.py @@ -7,6 +7,7 @@ from justgetit.register import Register from justgetit.configuration import Configuration from justgetit.providekey import ProvideKey from justgetit.lists import Lists +from justgetit.items import Items from justgetit.backend import BackEnd from justgetit.localdata import LocalData @@ -15,6 +16,7 @@ class JustEatIt(toga.App): register_screen = toga.Box() providekey_screen = toga.Box() lists_screen = toga.Box() + items_screen = toga.Box() backend = None configuration = None last_screen_width = -1 @@ -32,6 +34,8 @@ class JustEatIt(toga.App): self.set_page(self.login_screen) elif destination == PAGE_LISTS: self.set_page(self.lists_screen) + elif destination == PAGE_ITEMS: + self.set_page(self.items_screen) def startup(self): self.configuration = Configuration(self) @@ -42,9 +46,11 @@ class JustEatIt(toga.App): self.register_screen = Register() self.providekey_screen = ProvideKey() self.lists_screen = Lists(self.localdata.lists) + self.items_screen = Items(self.localdata.items) self.backend = BackEnd(self) self.localdata.lists.data_widget = self.lists_screen.data_widget.source + self.localdata.items.data_widget = self.items_screen.data_widget.source self.main_window = toga.MainWindow(title=self.formal_name) diff --git a/src/justgetit/configuration.py b/src/justgetit/configuration.py index f08479d..0d0b279 100644 --- a/src/justgetit/configuration.py +++ b/src/justgetit/configuration.py @@ -11,6 +11,7 @@ class Configuration(): - user: the user as a dict - userlang: the users language - lastlogin_date: date of last sucessful login + - current_listid; the id of the last viewed list """ loaded = False data = {} @@ -81,4 +82,14 @@ class Configuration(): if 'user' in self.data: print() result = self.data['user']['id'] - return result \ No newline at end of file + return result + + def set_current_listid(self, lid): + self.data['current_listid'] = lid + + def get_current_listid(self): + if 'current_listid' in self.data: + return self.data['current_listid'] + return 0 + + \ No newline at end of file diff --git a/src/justgetit/items.py b/src/justgetit/items.py new file mode 100644 index 0000000..634337a --- /dev/null +++ b/src/justgetit/items.py @@ -0,0 +1,164 @@ +import toga +from toga.style import Pack +from toga.style.pack import COLUMN, ROW, END, START, CENTER, HIDDEN, JUSTIFY +from toga.sources import ListSource, Listener +import datetime +from justgetit.constants import * +from justgetit.basepage import BasePage + +class DatabaseList(toga.Box): + def __init__(self, id=None, style=None, children=None, **kwargs): + style.direction = COLUMN + super().__init__(id, style, children, **kwargs) + + self.source = ListSource( + accessors=["id", "listofitems_id", "label", "quantity", "unit", 'is_checked', 'is_suggestion', 'category'] + ) + self.source.add_listener(DatabaseList.DataListener(self)) + + + def on_row_pressed(self, w): + # self.app.configuration.set_current_listid(w.id) + # self.app.goto_next_page_by_name(PAGE_ITEMS) + print("item buttn prtesse") + + + class DataListener(Listener): + # listen to the database + def __init__(self, parent_widget): + print("widget: data-item init") + self.parent_widget = parent_widget + super().__init__() + + def change(self, item): + print("widget: data-item change") + return super().change(item) + + def clear(self): + print("widget: data-item clear") + return super().clear() + + def insert(self, index, item): + print("widget: data-item insert") + self.parent_widget.add(self.create_row(item.id, item.label, item.quantity, item.unit)) + return super().insert(index, item) + + def remove(self, index, item): + print("widget: data-item remove") + return super().remove(index, item) + + def create_row(self, listid, text, openitems, totalitems): + label = toga.Button( + text=text, + id=listid, + margin_bottom=2, + style = Pack( + flex=12, + background_color="white", #white + font_size=15, + ), + on_press=self.parent_widget.on_row_pressed + ) + indicator_label = toga.Label( + text = f"{openitems} / {totalitems}", + style = Pack( + color="blue", + margin=8, + + ) + ) + indicator_box = toga.Box( + children=[indicator_label], + style = Pack( + flex=2, + justify_content=END + ) + ) + listrow_inner = toga.Box( + style = Pack( + margin_bottom=1, + flex=1, + background_color="white", #white + ) + ) + listrow_inner.add(label) + listrow_inner.add(indicator_box) + + listrow_outer = toga.Box( + style = Pack( + # flex=1, + background_color="#ccc", # ccc + ) + ) + listrow_outer.add(listrow_inner) + + return(listrow_outer) + +class Items(BasePage): + + 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 + + :param self: register-screen + :param navigate_to: function to call if we want to switch screens + :param args: args for super + :param kwargs: kwars for super + """ + + # set button and image + self.first_menu_button_icon = 'edit3' + 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.data_widget = DatabaseList( + style=Pack( + margin_bottom=10, + # background_color="white", + ), + ) + + scrollarea = toga.ScrollContainer( + content=self.data_widget, + style=Pack( + flex=1, + margin=10, + ), + horizontal=False + ) + + # at the bottom of the list: edit-button + save_button = toga.Button( + icon=toga.Icon("images/edit"), + style=Pack( + width=50, + margin=10, + ), + on_press=self.edit_button_pressed) + + save_button_box= toga.Column( + children=[save_button], + style=Pack( + flex=1, + align_items=START, + margin=20 + ) + ) + + # wrapping up the windows elements + self.content_box.add(scrollarea) + # self.content_box.add(save_button_box) + + def on_show(self): + super().on_show() + self.datasource.reload() + + def edit_button_pressed(self): + self.app.goto_next_page_by_name(PAGE_ADDITEMS) + diff --git a/src/justgetit/lists.py b/src/justgetit/lists.py index 8d1b7cd..b98d20b 100644 --- a/src/justgetit/lists.py +++ b/src/justgetit/lists.py @@ -12,10 +12,14 @@ class DatabaseList(toga.Box): super().__init__(id, style, children, **kwargs) self.source = ListSource( - accessors=["id", "owner_user_id", "name", "is_active"] + accessors=["id", "owner_user_id", "name", "is_active", "pending", 'total'] ) self.source.add_listener(DatabaseList.DataListener(self)) + def on_row_pressed(self, w): + print("goto list") + self.app.configuration.set_current_listid(int(w.id)) + self.app.goto_next_page_by_name(PAGE_ITEMS) class DataListener(Listener): # listen to the database @@ -34,36 +38,38 @@ class DatabaseList(toga.Box): def insert(self, index, item): print("widget: data-item insert") - self.parent_widget.add(self.create_row(item.name,"3", "20")) + self.parent_widget.add(self.create_row(item.id, item.name, item.pending, item.total)) return super().insert(index, item) def remove(self, index, item): print("widget: data-item remove") return super().remove(index, item) - def create_row(self, text, openitems, totalitems): + def create_row(self, listid, text, openitems, totalitems): label = toga.Button( text=text, + id=listid, margin_bottom=2, style = Pack( - # margin_bottom=2, # scrollig doesnt work, bug?, replaced by margin_bottom in Button - # width=250, - flex=15, + flex=12, background_color="white", #white - font_size=15 - ) + font_size=15, + ), + on_press=self.parent_widget.on_row_pressed ) - spacer = toga.Box( - style = Pack( - flex=1 - ) - ) - indicator = toga.Label( + indicator_label = toga.Label( text = f"{openitems} / {totalitems}", style = Pack( color="blue", margin=8, - flex=2 + + ) + ) + indicator_box = toga.Box( + children=[indicator_label], + style = Pack( + flex=2, + justify_content=END ) ) listrow_inner = toga.Box( @@ -74,8 +80,7 @@ class DatabaseList(toga.Box): ) ) listrow_inner.add(label) - listrow_inner.add(spacer) - listrow_inner.add(indicator) + listrow_inner.add(indicator_box) listrow_outer = toga.Box( style = Pack( @@ -86,6 +91,8 @@ class DatabaseList(toga.Box): listrow_outer.add(listrow_inner) return(listrow_outer) + + class Lists(BasePage): @@ -153,9 +160,6 @@ class Lists(BasePage): super().on_show() self.datasource.reload() - - - - def edit_button_pressed(self): self.app.goto_next_page_by_name(PAGE_EDITLISTS) + diff --git a/src/justgetit/localdata.py b/src/justgetit/localdata.py index 08633cd..a08b031 100644 --- a/src/justgetit/localdata.py +++ b/src/justgetit/localdata.py @@ -17,36 +17,31 @@ class ListsData(): for item in result['data']: self.data_widget.append(item) +class ItemsData(): + def __init__(self, app): + self.app = app + self.data_widget = None + + def reload(self): + print("reload items") + current_userid = self.app.configuration.get_userid() + current_listid = self.app.configuration.get_current_listid() + if current_listid > 0: + parameters = {} + parameters['listid'] = current_listid + result = self.app.backend.postwait('load_items', parameters) + for item in result['data']: + self.data_widget.append(item) -class List(): - id = 0 - updated_at = None - owner_user_id = 0 - name = '' - is_active = True - - -class Item(): - id = 0 - updated_at = None - listofitems_id = 0 - label = '' - quantity = 0 - unit = '' - label_alt1 = '' - label_alt2 = '' - is_checked = False - is_suggestion = False - category = '' - class LocalData(): def __init__(self, app): self.app = app self.curent_userid = 0 self.current_listid = 0 self.lists = ListsData(app) + self.items = ItemsData(app) def set_user(self, userid): if userid != self.curent_userid: