From ba3e29d9557a25119ca29dd173a7453c9725cc0e Mon Sep 17 00:00:00 2001 From: megamichi Date: Mon, 17 Jun 2024 17:13:32 +0200 Subject: [PATCH] init --- .gitignore | 160 ++++++++++++++++++++++++++++++++++++++ README.md | 9 +++ assets/icon.png | Bin 0 -> 5693 bytes file_handler.py | 16 ++++ main.py | 182 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + saves/history.json | 1 + saves/settings.json | 3 + 8 files changed, 372 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 assets/icon.png create mode 100644 file_handler.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 saves/history.json create mode 100644 saves/settings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68bc17f --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e609991 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Nahrung Analysierung + +An example of a minimal Flet app. + +To run the app: + +``` +flet run [app_directory] +``` \ No newline at end of file diff --git a/assets/icon.png b/assets/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..6c1142e0cebb6dace3281dd6bb3c3d37f237aef5 GIT binary patch literal 5693 zcmeHKYgAKL7EXMjXceW7idJp|mSV{}0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|1%(jgdiATjl*>-5bnXqoR4-MDe>YBOStG(KK zf`>)Ngv*YT|d3PEMvz<~ajeX6pPnOL<4)Aq9 zX?s1Jtp|O$4I{2br`1r}sQ<8^fBGV=4YnTDVH7{^X~?b)mj)?Iys2l0&+fs5Mm1!m zpsuH{{RsN$_VWjOj%q&`(KEx?5Eaxx9CLQ0BWn=MFS?n*K 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/main.py b/main.py new file mode 100644 index 0000000..4a5015c --- /dev/null +++ b/main.py @@ -0,0 +1,182 @@ +import flet as ft +import file_handler +from datetime import datetime +from random import choice + +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,autofocus=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): + 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() + print(emoji,ord(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) # Save the updated kcal value + self.update_history() + self.reseterrorinkcaltextfield() + self.kcaltextfield.value = "" + self.update() # Update the controls to reflect changes + except ValueError: + print("error") + self.kcaltextfield.error_text = "\\(*O*)/ nicht verschreiben!!" + self.update() + #pass # Handle invalid input gracefully + + 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) + + +def main(page: ft.Page): + page.title = "Nahrungstracker" + page.scroll = True + + nahrung = Nahrung() + nahrung.page = page # Store reference to the page in Nahrung instance + 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/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d54c7ec --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flet==0.22.* \ No newline at end of file diff --git a/saves/history.json b/saves/history.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/saves/history.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/saves/settings.json b/saves/settings.json new file mode 100644 index 0000000..36310da --- /dev/null +++ b/saves/settings.json @@ -0,0 +1,3 @@ +{ + "kcal_max": 2000 +} \ No newline at end of file