Back to Blocks
PXL-805Done
Urgent

๐Ÿ• Fix crashes when the "Now" indicator is visible

Labels
1-circetimelineui/uxBug

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)

LineCurrent CodeIssue
460sortedMeals.firstIndex as sectionMay need sectionIndex(forMealIndex:)
1680sortedMeals[indexPath.section]Should use mealIndex(forSection:)
1728sortedMeals[indexPath.section]Should use mealIndex(forSection:)
2464sortedMeals.firstIndex as sectionIndexMay need sectionIndex(forMealIndex:)
4003sortedMeals.firstIndex as sectionIndexNeed to separate mealIndex/sectionIndex
4093sortedMeals[indexPath.section]Should use mealIndex(forSection:)
4141sortedMeals[indexPath.section]Should use mealIndex(forSection:)

Pattern to fix

  • โ—When accessing sortedMeals array โ†’ use meal index (via mealIndex(forSection:) if you have a section)
  • โ—When creating IndexPath for table view โ†’ use section index (via sectionIndex(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

Created Dec 29, 2025, 10:41 PM ยท Updated Jan 6, 2026, 1:02 PM