๐ Fix crashes when the "Now" indicator is visible
What we're fixing:
The app has a "Now" indicator that shows you the current time in your daily food log. When this indicator is visible, certain actions like deleting a meal item can cause the app to crash. The crash happens because the code doesn't properly account for the extra section that the "Now" indicator occupies in the table view. This task audits all the places where meal sections are referenced and ensures they correctly handle the offset created by the timing summary section.
Problem
We fixed a crash in deleteItem(_:) where the code was using sortedMeals.firstIndex directly as a table section index, but this doesn't account for the timing summary section that can be inserted at a dynamic position.
When the timing summary ("NOW" indicator) is shown on TODAY only, it occupies a section in the table view, which shifts all meal section indices. The code has helper methods:
- โ
mealIndex(forSection:)- converts table section to sortedMeals array index - โ
sectionIndex(forMealIndex:)- converts sortedMeals array index to table section
Note: The timing summary only shows on TODAY. On other days, timingSummarySectionIndex is nil, so the helper methods return indices unchanged. The fix is safe for both cases.
Locations to audit (DayViewController.swift)
| Line | Current Code | Issue |
|---|---|---|
| 460 | sortedMeals.firstIndex as section | May need sectionIndex(forMealIndex:) |
| 1680 | sortedMeals[indexPath.section] | Should use mealIndex(forSection:) |
| 1728 | sortedMeals[indexPath.section] | Should use mealIndex(forSection:) |
| 2464 | sortedMeals.firstIndex as sectionIndex | May need sectionIndex(forMealIndex:) |
| 4003 | sortedMeals.firstIndex as sectionIndex | Need to separate mealIndex/sectionIndex |
| 4093 | sortedMeals[indexPath.section] | Should use mealIndex(forSection:) |
| 4141 | sortedMeals[indexPath.section] | Should use mealIndex(forSection:) |
Pattern to fix
- โWhen accessing
sortedMealsarray โ use meal index (viamealIndex(forSection:)if you have a section) - โWhen creating
IndexPathfor table view โ use section index (viasectionIndex(forMealIndex:)if you have a meal index)
Already fixed
- โ
deleteItem(_:)at line 3934 - now properly separates mealIndex and sectionIndex
Acceptance Criteria
- โ Each location audited and fixed if needed
- โ Test delete item on TODAY with NOW cell visible
- โ Test delete item on other days (no NOW cell)
- โ No crashes when timing summary moves between meals
File
/Users/pxlshpr/Developer/NutriKit/NutriKit/Refactor Inbox/UIKit/DayViewController.swift