diff --git a/app/assets/icon.png b/app/assets/icon.png new file mode 100644 index 0000000..4e91dfe Binary files /dev/null and b/app/assets/icon.png differ diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..a85d5bb --- /dev/null +++ b/app/main.py @@ -0,0 +1,41 @@ +import flet as ft +from pages.root import Nahrung +from pages.settings import Settings + +def main(page: ft.Page): + page.title = "Nahrungstracker" + page.scroll = True + + # Initialize views + nahrung = Nahrung() + settings = Settings() + + # Store reference to the page in instances + nahrung.page = page + settings.page = page + + # Function to switch views + def on_navigation_change(e): + page.controls.clear() + if e.control.selected_index == 0: + page.appbar = nahrung.create_appbar() + page.add(nahrung) + elif e.control.selected_index == 1: + page.appbar = ft.AppBar(title=ft.Text("Settings")) + page.add(settings) + page.update() + + page.navigation_bar = ft.NavigationBar( + destinations=[ + ft.NavigationDestination(icon=ft.icons.HOME, label="Home"), + ft.NavigationDestination(icon=ft.icons.SETTINGS, label="Settings") + ], + on_change=on_navigation_change, + ) + + page.appbar = nahrung.create_appbar() + page.add(nahrung) + page.update() # Ensure the initial UI is updated + +if __name__ == "__main__": + ft.app(target=main) diff --git a/app/pages/file_handler.py b/app/pages/file_handler.py new file mode 100644 index 0000000..6eb2a2d --- /dev/null +++ b/app/pages/file_handler.py @@ -0,0 +1,14 @@ +import json + +def save(jsettings,jhisory): + with open("saves/settings.json", "w") as data: + data.write(json.dumps(jsettings,indent=4)) + with open("saves/history.json", "w") as data: + data.write(json.dumps(jhisory,indent=4)) + +def load(self) -> list: + with open("saves/settings.json", "r") as data: + jsettings = json.load(data) + with open("saves/history.json", "r") as data: + jhistory = json.load(data) + return jsettings,jhistory diff --git a/app/pages/history.py b/app/pages/history.py new file mode 100644 index 0000000..e69de29 diff --git a/app/pages/root.py b/app/pages/root.py new file mode 100644 index 0000000..a8676aa --- /dev/null +++ b/app/pages/root.py @@ -0,0 +1,168 @@ +import flet as ft +from datetime import datetime +from random import choice +from pages import file_handler + +def get_time(): + t = datetime.now() + return [t.year, t.month, t.day, t.hour, t.minute, t.second] + +def random_emoji(): + return choice("๐ŸŽ๐Ÿ๐ŸŠ๐Ÿ‹๐ŸŒ๐Ÿ‰๐Ÿ‡๐Ÿ“๐Ÿˆ๐Ÿ’๐Ÿ‘๐Ÿ๐Ÿฅ๐Ÿฅญ๐Ÿฅ‘๐Ÿ…๐Ÿ†๐Ÿฅ’๐Ÿฅ•๐Ÿฅฌ๐ŸŒฝ๐Ÿฅ”๐Ÿ ๐ŸŒฐ๐Ÿฅœ๐Ÿฏ๐Ÿฅ๐Ÿž๐Ÿฅ–๐Ÿฅจ๐Ÿฅฏ๐Ÿง€๐Ÿฅš๐Ÿณ๐Ÿฅ“๐Ÿง„๐Ÿง…๐Ÿฅž๐Ÿง‡๐Ÿค๐Ÿ—๐Ÿ–๐Ÿ•๐ŸŒญ๐Ÿ”๐ŸŸ๐Ÿฅ™๐ŸŒฎ๐ŸŒฏ๐Ÿฅ—๐Ÿฅ˜๐Ÿ๐Ÿœ๐Ÿฆช๐Ÿฒ๐Ÿฅ๐Ÿฃ๐Ÿฑ๐Ÿ›๐Ÿš๐Ÿง†๐Ÿ™๐Ÿ˜๐Ÿข๐Ÿก๐Ÿง๐Ÿจ๐Ÿฆ๐Ÿฐ๐ŸŽ‚๐Ÿฎ๐Ÿญ๐Ÿฌ๐Ÿซ๐Ÿฟ๐Ÿฉ๐Ÿช๐Ÿฅฎ๐Ÿง๐Ÿฅ›๐Ÿงˆ๐Ÿผโ˜•๐Ÿต๐Ÿถ๐Ÿบ๐Ÿป๐Ÿฅ‚๐Ÿท๐Ÿฅƒ๐Ÿธ๐Ÿน๐Ÿพ๐Ÿง‰๐Ÿงƒ๐ŸงŠ๐Ÿง‚๐Ÿฅ„๐Ÿด") + + +class Nahrung(ft.UserControl): + def __init__(self) -> None: + super().__init__() + self.settings, self.history = file_handler.load(self) + + self.kjtext = ft.Text("0", size=25) + self.kcaltext = ft.Text("0", size=30) + self.kcaltextfield = ft.TextField(keyboard_type="NUMBER", expand=True, on_change=self.reseterrorinkcaltextfield, on_submit=self.editkcal) + + # Initialize history list + self.historylist = [] + self.update_history() + + # Initialize controls + self.main_container = ft.Container( + content=ft.Column( + [ + ft.Row( + [ + ft.Text("kjoule:", size=20), + self.kjtext, + ft.Text(f"/{round(self.settings['kcal_max'] * 4.184, 0)}", size=10) + ] + ), + ft.Row( + [ + ft.Text("kcal:", size=25), + self.kcaltext, + ft.Text(f"/{self.settings['kcal_max']}", size=10) + ] + ), + ft.Row( + [ + self.kcaltextfield, + ft.FloatingActionButton(icon="add", on_click=self.editkcal) + ] + ), + ] + ), + bgcolor=ft.colors.INDIGO_400, + border_radius=10, + padding=15, + ) + + self.history_container = ft.Container( + content=ft.Column([ft.Text("Verlauf:"), + ft.Column(controls=self.historylist) + ]), + ) + + self.controls = [self.main_container, self.history_container] + + self.load_todays_kcal() + + def reseterrorinkcaltextfield(self, e=None): + self.kcaltextfield.error_text = "" + + def build(self): + self.kjtext.value = str(round(int(self.kcaltext.value) * 4.184, 0)) + return ft.Column(self.controls) + + def editkcal(self, e): + try: + emoji = random_emoji() + new_kcal = int(self.kcaltext.value) + int(self.kcaltextfield.value) + self.kcaltext.value = str(new_kcal) + self.kjtext.value = str(round(new_kcal * 4.184, 0)) + self.history.append([emoji, self.kcaltextfield.value, get_time()]) + file_handler.save(self.settings, self.history) + self.update_history() + self.reseterrorinkcaltextfield() + self.kcaltextfield.value = "" + self.update() + except ValueError: + self.kcaltextfield.error_text = "\\(*O*)/ nicht verschreiben!!" + self.update() + + def create_appbar(self): + return ft.AppBar( + title=ft.Text("Nahrungstracker"), + actions=[ + ft.PopupMenuButton( + items=[ft.PopupMenuItem(text="Reset", on_click=self.resethistory)] + ) + ], + ) + + def update_history(self): + self.historylist.clear() + for h in self.history: + self.historylist.insert(0, + ft.Container(content= + ft.Column(controls=[ + ft.Row( + [ft.Text(str(f"{h[2][3]}:{h[2][4]}:{h[2][5]} {h[2][2]}.{h[2][1]}.{h[2][0]}"))], + alignment=ft.MainAxisAlignment.END + ), + ft.Row( + [ft.Text(str(f"({h[1]} kcal) {h[0]}"), size=20)], + alignment=ft.MainAxisAlignment.START, + )]), + bgcolor=ft.colors.INDIGO_600, # "#226666", + border_radius=5, + padding=6 + ) + ) + + def resethistory(self, e): + print("pressed") + def reset(e): + self.kcaltext.value = "0" + self.kjtext.value = "0" + self.history = [] + file_handler.save(self.settings, self.history) + self.update_history() + self.history_container.content = ft.Column(self.historylist) + self.dialog.open = False + # beide updaten + self.page.update() + self.update() + + def close(e): + self.dialog.open = False + # beide updaten + self.page.update() + self.update() + + self.dialog = ft.AlertDialog( + modal=True, + title=ft.Text("Wirklich?"), + content=ft.Text("Alle Daten werden aus deinem Verlauf gelรถscht!!!"), + actions=[ + ft.TextButton("ja", on_click=reset), + ft.TextButton("nein", on_click=close), + ], + actions_alignment=ft.MainAxisAlignment.END, + on_dismiss=lambda e: print("Modal dialog dismissed!"), + ) + self.page.dialog = self.dialog + + self.dialog.open = True + self.page.update() + + def load_todays_kcal(self): + today_date = get_time()[:3] + print(today_date) + + todays_kcal = 0 + for entry in self.history: + entry_date = entry[2][:3] # Year, Month, Day part of the entry's timestamp + if entry_date == today_date: + todays_kcal += int(entry[1]) + + self.kcaltext.value = str(todays_kcal) + diff --git a/app/pages/settings.py b/app/pages/settings.py new file mode 100644 index 0000000..34703c7 --- /dev/null +++ b/app/pages/settings.py @@ -0,0 +1,21 @@ +import flet as ft +class Settings(ft.UserControl): + def __init__(self) -> None: + super().__init__() + + + # Initialize controls + self.main_container = ft.Container( + content=ft.Column([ft.Text("huhu")]), + border_radius=10, + padding=15 + ) + + def build(self): + return ft.Column(self.controls) + + def create_appbar(self): + return ft.AppBar( + title=ft.Text("Settings") + ) + diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..7a82fda --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1 @@ +flet==0.21.* \ No newline at end of file diff --git a/app/saves/history.json b/app/saves/history.json new file mode 100644 index 0000000..d154340 --- /dev/null +++ b/app/saves/history.json @@ -0,0 +1,26 @@ +[ + [ + "\ud83c\udf4b", + "8", + [ + 2024, + 6, + 23, + 13, + 37, + 23 + ] + ], + [ + "\ud83c\udf47", + "5", + [ + 2024, + 6, + 23, + 13, + 37, + 40 + ] + ] +] \ No newline at end of file diff --git a/app/saves/settings.json b/app/saves/settings.json new file mode 100644 index 0000000..36310da --- /dev/null +++ b/app/saves/settings.json @@ -0,0 +1,3 @@ +{ + "kcal_max": 2000 +} \ No newline at end of file diff --git a/app/views.py b/app/views.py new file mode 100644 index 0000000..b03bf87 --- /dev/null +++ b/app/views.py @@ -0,0 +1,4 @@ +import flet as ft + +def get_views(page): + return {} \ No newline at end of file