start implementing edit_recipe

This commit is contained in:
Jannes Magnusson
2025-10-18 23:28:55 +02:00
parent 06df2e0e9b
commit eb32f089ad
12 changed files with 241 additions and 70 deletions

View File

@@ -0,0 +1,4 @@
from .recipe_selection import RecipeSelection
from .recipe_manager import RecipeManager
from .edit_recipe import EditRecipe
from .recipe import Recipe

View File

@@ -1,39 +0,0 @@
from typing import Tuple
from PIL import ImageDraw, Image
from ..base import View
from ..button_interface import ButtonInterface
class AddRecipe(View, ButtonInterface):
def __init__(self, parent, im_size, center,
save_command=None, cancel_command=None):
self.save_command = save_command
self.cancel_command = cancel_command
super().__init__(parent, im_size, center)
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
draw.text((40, 30), "Add Recipe", fill='black')
draw.text((40, 60), "Save", fill='black')
draw.text((40, 90), "Cancel", fill='black')
return im
def left_press(self):
if self.save_command:
self.save_command()
def right_press(self):
if self.cancel_command:
self.cancel_command()
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, False, True, False
def render_left_press(self, draw, x, y):
draw.text((x, y), 'Save', fill='black')
def render_right_press(self, draw, x, y):
draw.text((x, y), 'Cancel', fill='black')

View File

View File

@@ -0,0 +1,104 @@
from typing import Tuple
from PIL import ImageDraw, Image
from ..base import View
from ..button_interface import ButtonInterface
from .recipe_manager import RecipeManager
from .recipe import Recipe
class EditRecipe(View, ButtonInterface):
def __init__(self, parent, im_size, center,
recipe_manager: RecipeManager,
deactivate_command,
recipe: Recipe = None):
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.recipe = recipe
if recipe is None:
self.recipe = Recipe("New", [])
self.confirm_view = False
self.selected_field = 0 # 0: name, 1+: steps
super().__init__(parent, im_size, center)
def _get_visual_steps(self):
steps = self.recipe.steps + ['+']
if len(steps) < 4:
return steps, 0
start = max(0, self.selected_field - 2)
end = min(len(steps), start + 4)
steps = steps[start:end]
return steps, start
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
x = 40
draw.text((x, 10), "Name:", fill='black')
if self.selected_field == 0:
r = 10
offset = 35
for i in range(0, 90, r // 2):
draw.circle((x + i, offset), r, fill='black')
draw.text((x, 30), self.recipe.name, fill='white')
else:
draw.text((x, 30), self.recipe.name, fill='black')
visual_steps, start = self._get_visual_steps()
for idx, step in enumerate(visual_steps):
y_pos = 60 + idx * 20
if start + idx + 1 == self.selected_field:
r = 10
offset = 15
for i in range(0, 90, r // 2):
draw.circle((x + i, y_pos), r, fill='black')
if str(step) != '+':
step.step_type.render(draw, (x, y_pos - 5), fill='white')
draw.text((x + 30, y_pos - 5), step.value_str, fill='white')
else:
draw.text((x, y_pos - 5), '+', fill='white')
elif str(step) == '+':
draw.text((x, y_pos - 5), '+', fill='black')
else:
step.step_type.render(draw, (x, y_pos - 5), fill='black')
draw.text((x + 30, y_pos - 5), step.value_str, fill='black')
return im
def left_press(self):
# edit entry
pass
def left_long_press(self):
# save
self.recipe_manager.add_recipe(self.recipe)
self.deactivate_command()
def right_press(self):
self.selected_field += 1
if self.selected_field > len(self.recipe.steps) + 1:
self.selected_field = 0
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, True
def render_left_press(self, draw, x, y):
draw.regular_polygon((x, y, 5), 3, fill='black', rotation=270)
def render_left_long_press(self, draw, x, y):
draw.text((x - 3, y - 5), 'Save', fill='black')
def render_right_press(self, draw, x, y):
draw.regular_polygon((x, y + 6, 5), 3, fill='black', rotation=180)
def render_right_long_press(self, draw, x, y):
draw.text((x - 30, y - 5), 'Cancel', fill='black')

View File

@@ -3,6 +3,8 @@ from typing import Union
from enum import Enum
from ..draw_utils import draw_clock
class Recipe:
def __init__(self, name: str, steps: list[Step]):
self.name = name
@@ -19,6 +21,16 @@ class StepType(Enum):
WAIT_TIME_FINISHED = 3
TARE = 4
def render(self, draw, position, fill='black') -> str:
if self == StepType.SECTION:
draw.text(position, "T", fill=fill)
elif self == StepType.WEIGH:
draw.text(position, "W", fill=fill)
elif self == StepType.START_TIME or self == StepType.WAIT_TIME_FINISHED:
draw_clock(draw, (position[0] + 3, position[1] + 5), radius=3, color=fill)
elif self == StepType.TARE:
draw.text(position, "0.0g", fill=fill)
class Step:
def __init__(self,
@@ -26,6 +38,29 @@ class Step:
value: float | str = None):
self.step_type = step_type
self.value = value
@property
def value_str(self) -> str:
if self.step_type in [StepType.WEIGH]:
return f"{self.value}g"
elif self.step_type == StepType.START_TIME:
if self.value == -1:
return "Start"
else:
minutes = self.value // 60
seconds = self.value % 60
if minutes == 0:
return f"{seconds}s"
else:
return f"{minutes}:{seconds:02d}"
elif self.step_type == StepType.SECTION:
return str(self.value)
elif self.step_type == StepType.TARE:
return "Tare"
elif self.step_type == StepType.WAIT_TIME_FINISHED:
return "Wait"
else:
return ""
###### example recipes #########

View File

@@ -0,0 +1,14 @@
from .recipe import V60, ESPRESSO
class RecipeManager:
def __init__(self):
self.recipes = [
V60,
ESPRESSO
]
def add_recipe(self, recipe):
self.recipes.append(recipe)
def remove_recipe(self, recipe):
self.recipes.remove(recipe)

View File

@@ -3,20 +3,25 @@ from typing import Tuple
from ..base import View
from ..button_interface import ButtonInterface
from .recipe_manager import RecipeManager
from .recipe import V60, ESPRESSO
from PIL import ImageDraw, Image
class RecipeSelection(View, ButtonInterface):
recipes = [
V60,
ESPRESSO
]
@property
def recipes(self):
return self.recipe_manager.recipes
def __init__(self, parent, im_size, center, deactivate_command=None):
def __init__(self, parent, im_size, center,
recipe_manager: RecipeManager = None,
edit_recipe_command=None,
deactivate_command=None):
self.selected_index = 0
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.edit_recipe_command = edit_recipe_command
super().__init__(parent, im_size, center)
def _get_visual_recipes(self):
@@ -56,6 +61,10 @@ class RecipeSelection(View, ButtonInterface):
def left_press(self):
self.selected_index = (self.selected_index - 1) % (len(self.recipes) + 2)
def left_long_press(self):
if self.selected_index < len(self.recipes):
self.edit_recipe_command(self.recipes[self.selected_index])
def right_press(self):
self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2)
@@ -65,8 +74,7 @@ class RecipeSelection(View, ButtonInterface):
# activate selected recipe
print(f"Activating recipe: {self.recipes[self.selected_index]}")
elif self.selected_index == len(self.recipes):
# add new recipe
print("Adding new recipe")
self.edit_recipe_command()
else:
self.selected_index = 0
self.deactivate_command()