feat: Enhance dose tracking functionality in edit window and add punch button support
This commit is contained in:
+32
-109
@@ -81,15 +81,9 @@ class MedTrackerApp:
|
||||
self.input_frame: ttk.Frame = input_ui["frame"]
|
||||
self.symptom_vars: dict[str, tk.IntVar] = input_ui["symptom_vars"]
|
||||
self.medicine_vars: dict[str, tuple[tk.IntVar, str]] = input_ui["medicine_vars"]
|
||||
self.dose_buttons: dict[str, ttk.Button] = input_ui["dose_buttons"]
|
||||
self.dose_entries: dict[str, ttk.Entry] = input_ui["dose_entries"]
|
||||
self.dose_displays: dict[str, tk.Text] = input_ui["dose_displays"]
|
||||
self.note_var: tk.StringVar = input_ui["note_var"]
|
||||
self.date_var: tk.StringVar = input_ui["date_var"]
|
||||
|
||||
# Set up dose button callbacks
|
||||
self._setup_dose_button_callbacks()
|
||||
|
||||
# Add buttons to input frame
|
||||
self.ui_manager.add_buttons(
|
||||
self.input_frame,
|
||||
@@ -112,75 +106,6 @@ class MedTrackerApp:
|
||||
# Load data
|
||||
self.load_data()
|
||||
|
||||
def _setup_dose_button_callbacks(self) -> None:
|
||||
"""Set up callbacks for dose tracking buttons."""
|
||||
for medicine_name, button in self.dose_buttons.items():
|
||||
button.config(
|
||||
command=lambda med=medicine_name: self._take_medicine_dose(med)
|
||||
)
|
||||
|
||||
# Update dose displays for today
|
||||
self._update_dose_displays()
|
||||
|
||||
def _take_medicine_dose(self, medicine_name: str) -> None:
|
||||
"""Record a dose of medicine taken right now."""
|
||||
dose_entry = self.dose_entries[medicine_name]
|
||||
dose = dose_entry.get().strip()
|
||||
|
||||
if not dose:
|
||||
messagebox.showerror(
|
||||
"Error",
|
||||
f"Please enter a dose amount for {medicine_name}",
|
||||
parent=self.root,
|
||||
)
|
||||
return
|
||||
|
||||
# Use today's date
|
||||
today = self.date_var.get()
|
||||
if not today:
|
||||
from datetime import datetime
|
||||
|
||||
today = datetime.now().strftime("%m/%d/%Y")
|
||||
self.date_var.set(today)
|
||||
|
||||
if self.data_manager.add_medicine_dose(today, medicine_name, dose):
|
||||
messagebox.showinfo(
|
||||
"Success",
|
||||
f"{medicine_name.title()} dose recorded: {dose}",
|
||||
parent=self.root,
|
||||
)
|
||||
# Clear dose entry
|
||||
dose_entry.delete(0, tk.END)
|
||||
# Update displays and reload data
|
||||
self._update_dose_displays()
|
||||
self.load_data()
|
||||
else:
|
||||
messagebox.showerror(
|
||||
"Error", f"Failed to record {medicine_name} dose", parent=self.root
|
||||
)
|
||||
|
||||
def _update_dose_displays(self) -> None:
|
||||
"""Update the dose display areas with today's doses."""
|
||||
today = self.date_var.get()
|
||||
if not today:
|
||||
return
|
||||
|
||||
for medicine_name, display in self.dose_displays.items():
|
||||
doses = self.data_manager.get_today_medicine_doses(today, medicine_name)
|
||||
|
||||
display.config(state=tk.NORMAL)
|
||||
display.delete(1.0, tk.END)
|
||||
|
||||
if doses:
|
||||
dose_text = "\n".join(
|
||||
[f"{timestamp}: {dose}" for timestamp, dose in doses]
|
||||
)
|
||||
display.insert(1.0, dose_text)
|
||||
else:
|
||||
display.insert(1.0, "No doses recorded today")
|
||||
|
||||
display.config(state=tk.DISABLED)
|
||||
|
||||
def on_double_click(self, event: tk.Event) -> None:
|
||||
"""Handle double-click event to edit an entry."""
|
||||
logger.debug("Double-click event triggered on treeview.")
|
||||
@@ -194,14 +119,39 @@ class MedTrackerApp:
|
||||
"""Create a new Toplevel window for editing an entry."""
|
||||
original_date = values[0] # Store the original date
|
||||
|
||||
# Get the full row data from the CSV (including dose columns)
|
||||
df = self.data_manager.load_data()
|
||||
if not df.empty and original_date in df["date"].values:
|
||||
full_row = df[df["date"] == original_date].iloc[0]
|
||||
# Convert to tuple in the expected order for the edit window
|
||||
full_values = (
|
||||
full_row["date"],
|
||||
full_row["depression"],
|
||||
full_row["anxiety"],
|
||||
full_row["sleep"],
|
||||
full_row["appetite"],
|
||||
full_row["bupropion"],
|
||||
full_row["bupropion_doses"],
|
||||
full_row["hydroxyzine"],
|
||||
full_row["hydroxyzine_doses"],
|
||||
full_row["gabapentin"],
|
||||
full_row["gabapentin_doses"],
|
||||
full_row["propranolol"],
|
||||
full_row["propranolol_doses"],
|
||||
full_row["note"],
|
||||
)
|
||||
else:
|
||||
# Fallback to the table values if full data not found
|
||||
full_values = values
|
||||
|
||||
# Define callbacks for edit window buttons
|
||||
callbacks: dict[str, Callable] = {
|
||||
"save": lambda win, *args: self._save_edit(win, original_date, *args),
|
||||
"delete": lambda win: self._delete_entry(win, item_id),
|
||||
}
|
||||
|
||||
# Create edit window using UI manager
|
||||
_: tk.Toplevel = self.ui_manager.create_edit_window(values, callbacks)
|
||||
# Create edit window using UI manager with full data
|
||||
_: tk.Toplevel = self.ui_manager.create_edit_window(full_values, callbacks)
|
||||
|
||||
def _save_edit(
|
||||
self,
|
||||
@@ -217,36 +167,9 @@ class MedTrackerApp:
|
||||
gaba: int,
|
||||
prop: int,
|
||||
note: str,
|
||||
dose_data: dict[str, str],
|
||||
) -> None:
|
||||
"""Save the edited data to the CSV file."""
|
||||
# Get existing dose data for this date to preserve it
|
||||
bup_doses = ""
|
||||
hydro_doses = ""
|
||||
gaba_doses = ""
|
||||
prop_doses = ""
|
||||
|
||||
# Try to get existing dose data
|
||||
try:
|
||||
existing_bup = self.data_manager.get_today_medicine_doses(
|
||||
original_date, "bupropion"
|
||||
)
|
||||
existing_hydro = self.data_manager.get_today_medicine_doses(
|
||||
original_date, "hydroxyzine"
|
||||
)
|
||||
existing_gaba = self.data_manager.get_today_medicine_doses(
|
||||
original_date, "gabapentin"
|
||||
)
|
||||
existing_prop = self.data_manager.get_today_medicine_doses(
|
||||
original_date, "propranolol"
|
||||
)
|
||||
|
||||
bup_doses = "|".join([f"{ts}:{dose}" for ts, dose in existing_bup])
|
||||
hydro_doses = "|".join([f"{ts}:{dose}" for ts, dose in existing_hydro])
|
||||
gaba_doses = "|".join([f"{ts}:{dose}" for ts, dose in existing_gaba])
|
||||
prop_doses = "|".join([f"{ts}:{dose}" for ts, dose in existing_prop])
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not retrieve existing dose data: {e}")
|
||||
|
||||
values: list[str | int] = [
|
||||
date,
|
||||
dep,
|
||||
@@ -254,13 +177,13 @@ class MedTrackerApp:
|
||||
slp,
|
||||
app,
|
||||
bup,
|
||||
bup_doses,
|
||||
dose_data.get("bupropion", ""),
|
||||
hydro,
|
||||
hydro_doses,
|
||||
dose_data.get("hydroxyzine", ""),
|
||||
gaba,
|
||||
gaba_doses,
|
||||
dose_data.get("gabapentin", ""),
|
||||
prop,
|
||||
prop_doses,
|
||||
dose_data.get("propranolol", ""),
|
||||
note,
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user