add edit name

This commit is contained in:
Jannes Magnusson
2025-10-23 20:27:50 +02:00
parent 1e62bd77d1
commit 90257a62a0
6 changed files with 75 additions and 40 deletions

View File

@@ -5,21 +5,25 @@ from ..base import View
from ..button_interface import ButtonInterface
from .recipe_manager import RecipeManager
from .recipe import Recipe, Step
from .recipe import Recipe
class EditRecipe(View, ButtonInterface):
def __init__(self, parent, im_size, center,
recipe_manager: RecipeManager,
edit_step_command,
deactivate_command,
recipe: Recipe = None):
recipe_id: int = None):
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.recipe = recipe
self.recipe_id = recipe_id
self.edit_step_command = edit_step_command
if recipe is None:
self.is_add_form = recipe_id is None
if recipe_id is None:
self.recipe = Recipe("New", [])
self.recipe_manager.tmp_recipe = self.recipe
else:
self.recipe = recipe_manager.get_recipe(recipe_id)
self.confirm_view = False
self.selected_field = 0 # 0: name, 1+: steps
@@ -94,14 +98,18 @@ class EditRecipe(View, ButtonInterface):
pass
else:
# edit name
self.edit_step_command(self.recipe, self.selected_field)
self.edit_step_command(self.recipe_id, self.selected_field)
def right_press(self):
self.selected_field = (self.selected_field + 1) % (len(self.recipe.steps) + 3)
def right_long_press(self):
# save
self.recipe_manager.add_recipe(self.recipe)
if self.is_add_form:
self.recipe_manager.add_recipe(self.recipe)
else:
self.recipe_manager.update_recipe(self.recipe_id, self.recipe)
# if view is in edit mode, the recipe is already updated (by reference)
self.deactivate_command()
def has_button(self) -> Tuple[bool, bool, bool, bool]:

View File

@@ -12,17 +12,17 @@ from .recipe import Recipe, Step, StepType
class EditStep(View, ButtonInterface):
def __init__(self, parent, im_size, center,
recipe: Recipe,
recipe_id: int,
step_index: int,
recipe_manager: RecipeManager,
deactivate_command):
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.recipe = recipe
self.recipe_id = recipe_id
self.step_index = step_index
if step_index == 0:
self.step = recipe.name
self.step = recipe_manager.get_recipe(recipe_id).name
self.confirm_view = False
self.edit_step = 0 # 0: type, 1: step/name value
self.new_type = ''
@@ -53,21 +53,37 @@ class EditStep(View, ButtonInterface):
self.new_value += decode(self.morse_buffer, language=self.morse_code_language).upper()
else:
self.value_cursor -= 1
self.value_cursor = max(0, self.value_cursor)
self.new_value = self.new_value[:-1]
# process morse buffer
self.last_input = None
self.morse_buffer = ''
if isinstance(self.step, str):
draw.text((x, 10), "Name:", fill='black')
draw.text((x, 30), self.new_value, fill='black')
font_size = 16
y_start = 30
draw.text((x, y_start), "Name:", fill='black')
draw.text((x, y_start + 20), self.new_value, fill='black', font_size=font_size)
if self.value_cursor_pulse > 1.0:
draw.rectangle((x + self.value_cursor * 8, 28, x + self.value_cursor * 8 + 8, 40), fill='black')
draw.rectangle((x + self.value_cursor * 10,
y_start + 20 - 2,
x + self.value_cursor * 10 + 8,
y_start + 34),
fill='black')
if self.value_cursor_pulse > 2.0:
self.value_cursor_pulse = 0.0
self.value_cursor_pulse += 0.1
draw.line((x, 45, x + 80, 45), fill='black')
draw.line((x, y_start + 37, x + 80, y_start + 37), fill='black')
if self.morse_buffer != '':
for i, suffix in enumerate(['']):#, '.', '-', '.' * 10]):
letter = 'del'
if len(suffix) != 10 and len(self.morse_buffer) + len(suffix) < 10:
letter = decode(self.morse_buffer + suffix, language=self.morse_code_language)
draw.text((x, y_start + 50 + i * 15),
f'{self.morse_buffer + suffix} {letter}',
fill='black', font_size=14)
elif self.edit_step == 0:
pass
else:
@@ -101,22 +117,18 @@ class EditStep(View, ButtonInterface):
# draw.text((x + 30, y_pos - 5), step.value_str, fill='black')
return im
def left_press(self):
self.selected_field = (self.selected_field - 1) % (len(self.recipe.steps) + 3)
self.deactivate_command()
def left_long_press(self):
if self.selected_field == len(self.recipe.steps) + 2:
# back
if self.step_index == 0:
# editing name
if self.new_value:
recipe = self.recipe_manager.get_recipe(self.recipe_id)
recipe.name = self.new_value
self.deactivate_command()
elif self.selected_field == len(self.recipe.steps) + 1:
# add step
pass
elif self.selected_field == 0:
# edit name
pass
else:
# edit entry
pass
def right_press(self):
@@ -128,11 +140,10 @@ class EditStep(View, ButtonInterface):
self.morse_buffer += '-'
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, False
return True, True, True, False
def render_left_press(self, draw, x, y):
draw.regular_polygon((x, y+2, 5), 3, fill='black')
draw.text((x, y-5), 'Cancel', fill='black')
def render_left_long_press(self, draw, x, y):
draw.text((x, y-5), 'Next', fill='black')

View File

@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Union
from copy import deepcopy
from enum import Enum
@@ -13,6 +13,8 @@ class Recipe:
def __str__(self):
return self.name
def copy(self) -> Recipe:
return deepcopy(self)
class StepType(Enum):
SECTION = 0

View File

@@ -1,4 +1,4 @@
from .recipe import V60, ESPRESSO
from .recipe import V60, ESPRESSO, Recipe
class RecipeManager:
def __init__(self):
@@ -6,9 +6,21 @@ class RecipeManager:
V60,
ESPRESSO
]
self.tmp_recipe = None
def add_recipe(self, recipe):
self.recipes.append(recipe)
def update_recipe(self, recipe_id, new_recipe):
self.recipes[recipe_id] = new_recipe
def remove_recipe(self, recipe):
self.recipes.remove(recipe)
self.recipes.remove(recipe)
def get_recipe_id(self, recipe):
return self.recipes.index(recipe)
def get_recipe(self, recipe_id) -> Recipe:
if recipe_id == None:
return self.tmp_recipe
return self.recipes[recipe_id]

View File

@@ -64,7 +64,7 @@ class RecipeSelection(View, ButtonInterface):
def left_long_press(self):
if self.selected_index < len(self.recipes):
self.edit_recipe_command(self.recipes[self.selected_index])
self.edit_recipe_command(self.selected_index)
def right_press(self):
self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2)
@@ -74,8 +74,10 @@ 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
self.edit_recipe_command()
else:
# back
self.selected_index = 0
self.deactivate_command()