Skip to content

Commit 1c92b42

Browse files
authored
Merge pull request #351 from akshay0611/fix/issue342
Feat: Implement improved habit editing in Habit Builder (#342)
2 parents a4ae84f + bf3034d commit 1c92b42

File tree

1 file changed

+104
-56
lines changed

1 file changed

+104
-56
lines changed

pages/Habit_Builder.py

Lines changed: 104 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -212,70 +212,118 @@ def display_habit_management():
212212
if (h.get('name') if isinstance(h, dict) else h.name) == habit.name)
213213

214214
with st.expander(f"🎯 {habit.name}", expanded=False):
215-
col1, col2, col3 = st.columns(3)
216-
217-
with col1:
218-
st.metric("Current Streak", f"{habit.streak} days", f"+{habit.total_completions} total")
219-
with col2:
220-
difficulty_emoji = {"Easy": "🟢", "Medium": "🟡", "Hard": "🔴"}
221-
st.metric("Difficulty", f"{difficulty_emoji.get(habit.difficulty, '')} {habit.difficulty}")
222-
with col3:
223-
st.metric("Category", habit.category)
224-
225-
# Check completion status
226-
today = datetime.date.today()
227-
last_completed = None
228-
if habit.last_completed:
229-
if isinstance(habit.last_completed, str):
230-
last_completed = datetime.datetime.strptime(habit.last_completed, '%Y-%m-%d').date()
231-
else:
232-
last_completed = habit.last_completed
233-
234-
# Action buttons
235-
col1, col2, col3, col4 = st.columns(4)
236-
237-
with col1:
238-
if last_completed == today:
239-
st.success("✅ Completed Today!")
240-
else:
241-
if st.button("Mark Complete", key=f"complete_{original_index}"):
215+
# Check if this habit is currently being edited
216+
if st.session_state.get(f"editing_{original_index}", False):
217+
st.markdown("#### Edit Habit Details")
218+
with st.form(key=f"edit_habit_form_{original_index}"):
219+
col1_edit, col2_edit = st.columns(2)
220+
with col1_edit:
221+
edited_name = st.text_input("Habit Name", value=habit.name, key=f"edit_name_{original_index}")
222+
edited_category = st.selectbox("Category", list(get_habit_categories().keys()), index=list(get_habit_categories().keys()).index(habit.category), key=f"edit_category_{original_index}")
223+
with col2_edit:
224+
edited_difficulty = st.selectbox("Difficulty", ["Easy", "Medium", "Hard"], index=["Easy", "Medium", "Hard"].index(habit.difficulty), key=f"edit_difficulty_{original_index}")
225+
226+
# Handle reminder_time conversion
227+
current_reminder_time = None
228+
if habit.reminder_time:
229+
try:
230+
current_reminder_time = datetime.datetime.strptime(habit.reminder_time, '%H:%M:%S').time()
231+
except ValueError:
232+
# Handle cases where reminder_time might be in a different format or invalid
233+
pass
234+
edited_reminder_time = st.time_input("Reminder Time (Optional)", value=current_reminder_time, key=f"edit_reminder_{original_index}")
235+
236+
edited_notes = st.text_area("Notes (Optional)", value=habit.notes, key=f"edit_notes_{original_index}")
237+
238+
col_save, col_cancel = st.columns(2)
239+
with col_save:
240+
save_changes = st.form_submit_button("💾 Save Changes")
241+
with col_cancel:
242+
cancel_edit = st.form_submit_button("❌ Cancel")
243+
244+
if save_changes:
242245
# Update habit in session state
243246
habit_data = st.session_state.habits[original_index]
244247
if isinstance(habit_data, dict):
245-
if last_completed == today - datetime.timedelta(days=1):
246-
habit_data['streak'] += 1
247-
else:
248-
habit_data['streak'] = 1
249-
habit_data['total_completions'] += 1
250-
habit_data['last_completed'] = today.isoformat()
251-
252-
# Award points
253-
points = {"Easy": 5, "Medium": 10, "Hard": 15}
254-
st.session_state.user_points += points.get(habit.difficulty, 5)
248+
habit_data['name'] = edited_name
249+
habit_data['category'] = edited_category
250+
habit_data['difficulty'] = edited_difficulty
251+
habit_data['notes'] = edited_notes
252+
habit_data['reminder_time'] = edited_reminder_time.isoformat() if edited_reminder_time else None
255253

254+
st.session_state[f"editing_{original_index}"] = False
255+
st.success(f"Habit '{edited_name}' updated!")
256256
st.rerun()
257-
258-
with col2:
259-
if st.button("Edit", key=f"edit_{original_index}"):
260-
st.session_state[f"editing_{original_index}"] = True
261-
st.rerun()
262-
263-
with col3:
264-
if st.button("Archive", key=f"archive_{original_index}"):
265-
if st.session_state.get(f"confirm_archive_{original_index}", False):
266-
st.session_state.habits.pop(original_index)
257+
258+
if cancel_edit:
259+
st.session_state[f"editing_{original_index}"] = False
267260
st.rerun()
261+
else:
262+
# Normal display mode
263+
col1, col2, col3 = st.columns(3)
264+
265+
with col1:
266+
st.metric("Current Streak", f"{habit.streak} days", f"+{habit.total_completions} total")
267+
with col2:
268+
difficulty_emoji = {"Easy": "🟢", "Medium": "🟡", "Hard": "🔴"}
269+
st.metric("Difficulty", f"{difficulty_emoji.get(habit.difficulty, '')} {habit.difficulty}")
270+
with col3:
271+
st.metric("Category", habit.category)
272+
273+
# Check completion status
274+
today = datetime.date.today()
275+
last_completed = None
276+
if habit.last_completed:
277+
if isinstance(habit.last_completed, str):
278+
last_completed = datetime.datetime.strptime(habit.last_completed, '%Y-%m-%d').date()
279+
else:
280+
last_completed = habit.last_completed
281+
282+
# Action buttons
283+
col_complete, col_edit, col_archive, col_confirm = st.columns(4)
284+
285+
with col_complete:
286+
if last_completed == today:
287+
st.success("✅ Completed Today!")
268288
else:
269-
st.session_state[f"confirm_archive_{original_index}"] = True
289+
if st.button("Mark Complete", key=f"complete_{original_index}"):
290+
# Update habit in session state
291+
habit_data = st.session_state.habits[original_index]
292+
if isinstance(habit_data, dict):
293+
if last_completed == today - datetime.timedelta(days=1):
294+
habit_data['streak'] += 1
295+
else:
296+
habit_data['streak'] = 1
297+
habit_data['total_completions'] += 1
298+
habit_data['last_completed'] = today.isoformat()
299+
300+
# Award points
301+
points = {"Easy": 5, "Medium": 10, "Hard": 15}
302+
st.session_state.user_points += points.get(habit.difficulty, 5)
303+
304+
st.rerun()
305+
306+
with col_edit:
307+
if st.button("✏️ Edit", key=f"edit_btn_{original_index}"):
308+
st.session_state[f"editing_{original_index}"] = True
270309
st.rerun()
271-
272-
with col4:
273-
if st.session_state.get(f"confirm_archive_{original_index}", False):
274-
st.warning("Click Archive again to confirm")
275-
276-
# Show notes if any
277-
if habit.notes:
278-
st.write(f"📝 **Notes:** {habit.notes}")
310+
311+
with col_archive:
312+
if st.button("Archive", key=f"archive_{original_index}"):
313+
if st.session_state.get(f"confirm_archive_{original_index}", False):
314+
st.session_state.habits.pop(original_index)
315+
st.rerun()
316+
else:
317+
st.session_state[f"confirm_archive_{original_index}"] = True
318+
st.rerun()
319+
320+
with col_confirm:
321+
if st.session_state.get(f"confirm_archive_{original_index}", False):
322+
st.warning("Click Archive again to confirm")
323+
324+
# Show notes if any
325+
if habit.notes:
326+
st.write(f"📝 **Notes:** {habit.notes}")
279327

280328
def display_add_habit_form():
281329
"""Display form to add new habits"""

0 commit comments

Comments
 (0)