import flet as ft from random import choice from datetime import datetime from pages import file_handler from .history import History 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 Home(ft.Control): def __init__(self, history_instance: History) -> None: super().__init__() self.history_instance = history_instance 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) self.snack_bar = ft.SnackBar(ft.Text("Hinzugefรผgt")) self.main_container = ft.Container( content=ft.Column( [ ft.Row( [ ft.Text("kjoule:", size=20), self.kjtext, ft.Text(f"/{round(self.history_instance.settings['kcal_max'] * 4.184, 0)}", size=10) ] ), ft.Row( [ ft.Text("kcal:", size=25), self.kcaltext, ft.Text(f"/{self.history_instance.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.controls = [self.main_container, self.snack_bar] 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() print(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_instance.addkcal(emoji, self.kcaltextfield.value) self.reseterrorinkcaltextfield() self.kcaltextfield.value = "" 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() except ValueError: self.kcaltextfield.error_text = "\\(*O*)/ nicht verschreiben!!" self.page.update() def create_appbar(self): return ft.AppBar( title=ft.Text("Home"), actions=[ ft.PopupMenuButton( items=[ft.PopupMenuItem(text="Reset", on_click=self.resethistory)] ) ], ) def resethistory(self,e): print("reset button") def reset(e): self.kcaltext.value = "0" self.kjtext.value = "0" 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 self.dialog.open = False self.page.update() def close(e): self.dialog.open = False self.page.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] todays_kcal = 0 for entry in self.history_instance.history: entry_date = entry[2][:3] if entry_date == today_date: todays_kcal += int(entry[1]) self.kcaltext.value = str(todays_kcal)