Claude Terminal
Loading...
⏰ Fix timing display bugs
We discovered a bug where meal item cell callbacks (onEdit, onDelete, onLogAgain, onMove, onCircleTapped) were using indexPath.section directly to index into sortedMeals, without accounting for the timing summary section that can appear at various positions in the table.
The fix was to use mealIndex(forSection:) helper which correctly maps table section indices to meal array indices.
Comprehensively audit DayViewController.swift and related files to find any other places where:
- ●
indexPath.sectionis used to directly index intosortedMealswithout usingmealIndex(forSection:) - ●
sortedMealsindices are used to createIndexPathwithout usingsectionIndex(forMealIndex:) - ●Any other table section/meal index mapping that doesn't account for
timingSummarySectionIndex
Known Additional Issue: Meal Flash Animation
When adding a meal via the time slider, the flash/highlight animation is flashing the wrong meal (the one before the newly added meal). This is likely the same root cause - the flash animation code is not accounting for the timing summary section offset when determining which section to flash.
Look for: Code that handles .scrollToMeal notification or any meal highlighting/flashing logic that uses meal indices to create section indices.
- ●
DayViewController.swift(primary) - ●Any other files that interact with the day table view sections
- ●The timing summary section ("NOW" indicator) can appear at different positions based on the current time
- ●When it's present, table sections don't map 1:1 to
sortedMealsindices - ●Helper functions exist:
mealIndex(forSection:)andsectionIndex(forMealIndex:)
- ● All usages of
indexPath.sectionwithsortedMealsusemealIndex(forSection:) - ● All usages of meal indices to create IndexPaths use
sectionIndex(forMealIndex:) - ● Meal flash animation highlights the correct newly-added meal
- ● Build succeeds:
xcodebuild -scheme NutriKit -destination 'platform=iOS Simulator,name=iPhone 17 Pro' build - ● Manual test: Add meal via slider when timing summary is visible, verify correct meal flashes
Changes Made
Problem
Code was treating table section indices as meal array indices interchangeably. When the timing summary section ("NOW" indicator) is present, they differ by 1 for all meals after the timing section.
Table Structure (when timing summary is present)
Section 0: Meal ← mealIndex 0
Section 1: Meal ← mealIndex 1
Section 2: ⏰ NOW ← timingSummarySectionIndex (NO meal!)
Section 3: Meal ← mealIndex 2 ⚠️ OFF BY ONE!
Section 4: Meal ← mealIndex 3 ⚠️ OFF BY ONE!
Fixes Applied to DayViewController.swift
Fix 1: handleDisplaySettingsChanged (line 1294)
// BEFORE:
guard indexPath.section < sortedMeals.count else { return false }
let meal = sortedMeals[indexPath.section]
// AFTER:
guard let mealIdx = mealIndex(forSection: indexPath.section) else { return false }
let meal = sortedMeals[mealIdx]
Fix 2: handleLogEntriesModeChanged (lines 1323-1345)
// BEFORE:
for sectionIndex in 0..<sortedMeals.count {
let meal = sortedMeals[sectionIndex]
allHeaderPaths.append(IndexPath(row: 0, section: sectionIndex))
}
headerCell.updateValues(with: sortedMeals[indexPath.section], ...)
// AFTER:
for mealIdx in 0..<sortedMeals.count {
let meal = sortedMeals[mealIdx]
let tableSection = sectionIndex(forMealIndex: mealIdx)
allHeaderPaths.append(IndexPath(row: 0, section: tableSection))
}
if let mealIdx = mealIndex(forSection: indexPath.section) {
headerCell.updateValues(with: sortedMeals[mealIdx], ...)
}
Fix 3: scrollToMeal ⭐ MAIN FLASH BUG (lines 1979-2015)
// BEFORE:
guard let sectionIndex = sortedMeals.firstIndex(...) else {...}
let headerIndexPath = IndexPath(row: 0, section: sectionIndex)
flashMeal(at: sectionIndex) // ❌ FLASHING WRONG MEAL!
// AFTER:
guard let mealIdx = sortedMeals.firstIndex(...) else {...}
let tableSection = sectionIndex(forMealIndex: mealIdx)
let headerIndexPath = IndexPath(row: 0, section: tableSection)
flashMeal(at: tableSection) // ✅ NOW FLASHES CORRECT MEAL!
Fix 4: didSelectRowAt (lines 3550-3557)
// BEFORE:
guard indexPath.section < sortedMeals.count else { return }
let meal = sortedMeals[indexPath.section]
// AFTER:
guard let mealIdx = mealIndex(forSection: indexPath.section) else { return }
let meal = sortedMeals[mealIdx]
Fix 5: willDisplay (lines 3602-3605)
// BEFORE:
guard indexPath.section < sortedMeals.count else { return }
let meal = sortedMeals[indexPath.section]
// AFTER:
guard let mealIdx = mealIndex(forSection: indexPath.section) else { return }
let meal = sortedMeals[mealIdx]
Build Status
✅ Build succeeded: xcodebuild -scheme NutriKit -destination 'platform=iOS Simulator,name=iPhone 17 Pro' build
Remaining
- ● Manual test: Add meal via slider when timing summary is visible, verify correct meal flashes