Refactor MedTrackerApp and UI components for improved structure and readability

- Simplified initialization logic in init.py
- Consolidated testing_mode assignment
- Removed unnecessary else statements
- Created UIManager class to handle UI-related tasks
- Modularized input frame creation, table frame creation, and graph frame creation
- Enhanced edit window creation with better organization and error handling
- Updated data management methods to improve clarity and maintainability
- Improved logging for better debugging and tracking of application flow
This commit is contained in:
William Valentin
2025-07-23 16:10:22 -07:00
parent 4ba4b1b7c5
commit 2142db7093
15 changed files with 1063 additions and 578 deletions

81
src/graph_manager.py Normal file
View File

@@ -0,0 +1,81 @@
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.axes import Axes
from tkinter import ttk
class GraphManager:
"""Handle all graph-related operations for the application."""
def __init__(self, parent_frame: ttk.LabelFrame) -> None:
self.parent_frame: ttk.LabelFrame = parent_frame
# Configure graph frame to expand
self.parent_frame.grid_rowconfigure(0, weight=1)
self.parent_frame.grid_columnconfigure(0, weight=1)
# Initialize matplotlib figure and canvas
self.fig: matplotlib.figure.Figure
self.ax: Axes
self.fig, self.ax = plt.subplots()
self.canvas: FigureCanvasTkAgg = FigureCanvasTkAgg(
figure=self.fig, master=self.parent_frame
)
self.canvas.get_tk_widget().pack(fill="both", expand=True)
def update_graph(self, df: pd.DataFrame) -> None:
"""Update the graph with new data."""
self.ax.clear()
if not df.empty:
# Convert dates and sort
df = df.copy() # Create a copy to avoid modifying the original
df["date"] = pd.to_datetime(df["date"])
df = df.sort_values(by="date")
df.set_index(keys="date", inplace=True)
# Plot data series
self._plot_series(
df, "depression", "Depression (0:good, 10:bad)", "o", "-"
)
self._plot_series(
df, "anxiety", "Anxiety (0:good, 10:bad)", "o", "-"
)
self._plot_series(
df, "sleep", "Sleep (0:bad, 10:good)", "o", "dashed"
)
self._plot_series(
df, "appetite", "Appetite (0:bad, 10:good)", "o", "dashed"
)
# Configure graph appearance
self.ax.legend()
self.ax.set_title("Medication Effects Over Time")
self.ax.set_xlabel("Date")
self.ax.set_ylabel("Rating (0-10)")
self.fig.autofmt_xdate()
# Redraw the canvas
self.canvas.draw()
def _plot_series(
self,
df: pd.DataFrame,
column: str,
label: str,
marker: str,
linestyle: str,
) -> None:
"""Helper method to plot a data series."""
self.ax.plot(
df.index,
df[column],
marker=marker,
linestyle=linestyle,
label=label,
)
def close(self) -> None:
"""Clean up resources."""
plt.close(self.fig)