Nahrungstracker/app/pages/home.py

138 lines
5 KiB
Python
Raw Permalink Normal View History

2024-06-23 14:02:09 +02:00
import flet as ft
from random import choice
2024-06-23 19:41:00 +02:00
from datetime import datetime
2024-06-23 14:02:09 +02:00
from pages import file_handler
2024-06-23 19:41:00 +02:00
from .history import History
2024-06-23 14:02:09 +02:00
def get_time():
t = datetime.now()
return [t.year, t.month, t.day, t.hour, t.minute, t.second]
def random_emoji():
return choice("🍎🍐🍊🍋🍌🍉🍇🍓🍈🍒🍑🍍🥝🥭🥑🍅🍆🥒🥕🥬🌽🥔🍠🌰🥜🍯🥐🍞🥖🥨🥯🧀🥚🍳🥓🧄🧅🥞🧇🍤🍗🍖🍕🌭🍔🍟🥙🌮🌯🥗🥘🍝🍜🦪🍲🍥🍣🍱🍛🍚🧆🍙🍘🍢🍡🍧🍨🍦🍰🎂🍮🍭🍬🍫🍿🍩🍪🥮🧁🥛🧈🍼☕🍵🍶🍺🍻🥂🍷🥃🍸🍹🍾🧉🧃🧊🧂🥄🍴")
2024-07-02 18:44:27 +02:00
class Home(ft.Control):
2024-06-23 19:41:00 +02:00
def __init__(self, history_instance: History) -> None:
2024-06-23 14:02:09 +02:00
super().__init__()
2024-06-23 19:41:00 +02:00
self.history_instance = history_instance
2024-06-23 14:02:09 +02:00
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)
2024-06-23 19:41:00 +02:00
self.snack_bar = ft.SnackBar(ft.Text("Hinzugefügt"))
2024-06-23 14:02:09 +02:00
self.main_container = ft.Container(
content=ft.Column(
[
ft.Row(
[
ft.Text("kjoule:", size=20),
self.kjtext,
2024-06-23 19:41:00 +02:00
ft.Text(f"/{round(self.history_instance.settings['kcal_max'] * 4.184, 0)}", size=10)
2024-06-23 14:02:09 +02:00
]
),
ft.Row(
[
ft.Text("kcal:", size=25),
self.kcaltext,
2024-06-23 19:41:00 +02:00
ft.Text(f"/{self.history_instance.settings['kcal_max']}", size=10)
2024-06-23 14:02:09 +02:00
]
),
ft.Row(
[
self.kcaltextfield,
ft.FloatingActionButton(icon="add", on_click=self.editkcal)
]
),
]
),
bgcolor=ft.colors.INDIGO_400,
border_radius=10,
padding=15,
)
2024-06-23 19:41:00 +02:00
self.controls = [self.main_container, self.snack_bar]
2024-06-23 14:02:09 +02:00
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()
2024-06-23 19:41:00 +02:00
print(emoji)
2024-06-23 14:02:09 +02:00
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))
2024-06-23 19:41:00 +02:00
self.history_instance.addkcal(emoji, self.kcaltextfield.value)
2024-06-23 14:02:09 +02:00
self.reseterrorinkcaltextfield()
self.kcaltextfield.value = ""
2024-06-23 19:41:00 +02:00
self.snack_bar.open = True
self.page.snack_bar = self.snack_bar
self.page.update()
self.snack_bar.open = False
self.page.snack_bar = None
self.history_instance.update_history_view()
2024-06-23 14:02:09 +02:00
except ValueError:
self.kcaltextfield.error_text = "\\(*O*)/ nicht verschreiben!!"
2024-06-23 19:41:00 +02:00
self.page.update()
2024-06-23 14:02:09 +02:00
def create_appbar(self):
return ft.AppBar(
2024-06-23 19:41:00 +02:00
title=ft.Text("Home"),
2024-06-23 14:02:09 +02:00
actions=[
ft.PopupMenuButton(
items=[ft.PopupMenuItem(text="Reset", on_click=self.resethistory)]
)
],
)
2024-06-23 19:41:00 +02:00
def resethistory(self,e):
print("reset button")
2024-06-23 14:02:09 +02:00
def reset(e):
self.kcaltext.value = "0"
self.kjtext.value = "0"
2024-06-23 19:41:00 +02:00
self.history_instance.history = []
file_handler.save(self.history_instance.settings, self.history_instance.history)
self.history_instance.update_history_view() # Aktualisieren der History-Ansicht
2024-06-23 14:02:09 +02:00
self.dialog.open = False
2024-06-23 19:41:00 +02:00
self.page.update()
2024-06-23 14:02:09 +02:00
def close(e):
self.dialog.open = False
2024-06-23 19:41:00 +02:00
self.page.update()
2024-06-23 14:02:09 +02:00
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!"),
)
2024-06-23 19:41:00 +02:00
self.page.dialog = self.dialog
2024-06-23 14:02:09 +02:00
self.dialog.open = True
self.page.update()
def load_todays_kcal(self):
today_date = get_time()[:3]
todays_kcal = 0
2024-06-23 19:41:00 +02:00
for entry in self.history_instance.history:
entry_date = entry[2][:3]
2024-06-23 14:02:09 +02:00
if entry_date == today_date:
todays_kcal += int(entry[1])
self.kcaltext.value = str(todays_kcal)