Claude Terminal
Click to expand and connect
Complete energy balance calorie target calculations
When setting calorie goals, the app can calculate your "maintenance calories" (how much you burn daily) by looking at your weight changes and food intake over time. Right now this feature shows fake example data - this task connects it to your real tracking data and makes sure the math works reliably even when you don't have perfect records.
Technical details
Overview
Complete the energy balance equation implementation for calorie targets across both the wizard flow (EnergyTargetWizard.swift) and the editing flow (EnergyBalanceSettingsSheet.swift). Currently the wizard has ebPreviewMode = true which displays dummy/preview data instead of real user data.
Key Files
- ●
/Users/pxlshpr/Developer/NutriKit/NutriKit/Views/TargetWizard/EnergyTargetWizard.swift- Wizard flow withebPreviewModeflag at line 86 - ●
/Users/pxlshpr/Developer/NutriKit/NutriKit/Refactor Inbox/Views/Maintenance/EnergyBalanceSettingsSheet.swift- Editing flow - ●
/Users/pxlshpr/Developer/NutriKit/NutriKit/Refactor Inbox/Services/MaintenanceCalculator.swift- Core calculation logic - ●
/Users/pxlshpr/Developer/NutriKit/NutriKit/Models/BiometricDay.swift- Data model for daily biometrics
Requirements
1. Replace Dummy Data with Real Data
- ●Set
ebPreviewMode = falseinEnergyTargetWizard.swift - ●Ensure
fetchEnergyBalanceData()properly fetches real data from:- ●
Daymodel for tracked calories (day.energyInKcal) - ●
BiometricDaymodel for weight data - ●Apple HealthKit for weight and dietary energy when available
- ●
- ●Implement missing HealthKit data fetching in
EnergyBalanceSettingsSheet.swift(TODOs at lines 205-208)
2. Verify Calculation Accuracy
The energy balance equation in MaintenanceCalculator.swift:
Maintenance = Average Daily Intake - (Weight Change * 7700 / Days)
Where 7700 kcal ≈ 1 kg of body weight
Ensure both calculation methods work correctly:
- ●
calculateEnergyBalance()- uses date range and ModelContext - ●
calculateEnergyBalanceWithBiometricDays()- uses BiometricDay array
3. Handle Edge Cases
Create robust handling for:
- ●Insufficient data: User has only 0-2 days of calorie or weight data
- ●Partial data: User has 50% or fewer days with complete data in the range
- ●No weight change: Start and end weights are identical
- ●Extreme values: Unrealistic calorie intake (< 500 or > 10,000 kcal/day)
- ●Negative maintenance result: Calculation yields negative or extremely low values
- ●Large weight fluctuations: Day-to-day changes > 2kg (likely water weight)
- ●Missing HealthKit permissions: Graceful fallback to manual/app-tracked data
- ●Date range issues: Invalid or reversed date ranges
4. Create Comprehensive Test Suite
Create tests in a new test file EnergyBalanceCalculatorTests.swift covering:
Happy Path Tests:
- ●7-day period with complete calorie and weight data
- ●14-day period with gradual weight loss (maintenance calculation accuracy)
- ●14-day period with gradual weight gain
Edge Case Tests:
- ●Zero days of data - should return nil/error gracefully
- ●Only 1 day of data - should return nil/error
- ●Only 2 days of data - minimum viable calculation
- ●3 days with data in 14-day range (< 50% threshold)
- ●All calories but no weight data
- ●All weight but no calorie data
- ●Mix of data sources (HealthKit + app-tracked + manual)
Boundary Tests:
- ●Extreme calorie values (100 kcal/day, 15,000 kcal/day)
- ●Extreme weight change (5kg loss in 7 days)
- ●Very small weight change (0.1kg over 14 days)
- ●Identical start/end weights
- ●Calculation result below 500 kcal (unrealistic)
- ●Calculation result above 6000 kcal (unrealistic)
Data Source Priority Tests:
- ●Manual overrides HealthKit
- ●App-tracked overrides HealthKit
- ●Excluded days are properly skipped
Acceptance Criteria
- ●
ebPreviewModeis set tofalseand real data displays in wizard - ● Editing flow fetches and displays real data
- ● Calculations match expected results within 1% tolerance
- ● All edge cases display appropriate user-facing messages
- ● Test suite passes with > 90% code coverage on
MaintenanceCalculator - ● No crashes when data is missing or malformed
Notes
- ●The
BiometricDaymodel already hasisValidForCalculationandisExcludedFromCalculationproperties - leverage these - ●Minimum days required threshold is currently
max(3, dayCount / 2)- verify this is appropriate - ●Consider adding user-facing warnings when data quality is low
Build instruction: Use -destination 'platform=iOS Simulator,name=iPhone 17 Pro' when building this project