Skip to content

Commit c95c24d

Browse files
Adds a method to enable Food Search to Add Carb Entry View
- Easy discovery: Users see the brain icon toggle when Food Search is disabled - Instant enablement: Toggle works immediately to show full Food Search UI - Consistent disabling: Settings changes (via gear icon) instantly update the UI - Responsive both ways: Works whether toggled from carb entry or settings
1 parent a7893f4 commit c95c24d

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

Loop/Views/CarbEntryView.swift

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct CarbEntryView: View, HorizontalSizeClassOverride {
2626
@State private var showAddFavoriteFood = false
2727
@State private var showingAICamera = false
2828
@State private var showingAISettings = false
29+
@State private var isFoodSearchEnabled = UserDefaults.standard.foodSearchEnabled
2930

3031
// MARK: - Row enum
3132
enum Row: Hashable {
@@ -127,6 +128,16 @@ struct CarbEntryView: View, HorizontalSizeClassOverride {
127128
.sheet(isPresented: $showingAISettings) {
128129
AISettingsView()
129130
}
131+
.onAppear {
132+
isFoodSearchEnabled = UserDefaults.standard.foodSearchEnabled
133+
}
134+
.onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in
135+
// Update state when UserDefaults changes (e.g., from Settings screen)
136+
let currentSetting = UserDefaults.standard.foodSearchEnabled
137+
if currentSetting != isFoodSearchEnabled {
138+
isFoodSearchEnabled = currentSetting
139+
}
140+
}
130141
}
131142

132143
private var mainCard: some View {
@@ -139,7 +150,7 @@ struct CarbEntryView: View, HorizontalSizeClassOverride {
139150
CarbQuantityRow(quantity: $viewModel.carbsQuantity, isFocused: amountConsumedFocused, title: NSLocalizedString("Amount Consumed", comment: "Label for carb quantity entry row on carb entry screen"), preferredCarbUnit: viewModel.preferredCarbUnit)
140151

141152
// Food search section - moved up from bottom
142-
if isNewEntry && UserDefaults.standard.foodSearchEnabled {
153+
if isNewEntry && isFoodSearchEnabled {
143154
CardSectionDivider()
144155

145156
VStack(spacing: 16) {
@@ -206,7 +217,7 @@ struct CarbEntryView: View, HorizontalSizeClassOverride {
206217
}
207218

208219
// Food-related rows (only show if food search is enabled)
209-
if UserDefaults.standard.foodSearchEnabled {
220+
if isFoodSearchEnabled {
210221
// Always show servings row when food search is enabled
211222
ServingsDisplayRow(
212223
servings: $viewModel.numberOfServings,
@@ -436,6 +447,14 @@ struct CarbEntryView: View, HorizontalSizeClassOverride {
436447
print("🎯 AIAbsorptionTimePickerRow received isAIGenerated: \(isAIGenerated)")
437448
}
438449
.padding(.bottom, 2)
450+
451+
// Food Search enablement toggle (only show when Food Search is disabled)
452+
if !isFoodSearchEnabled {
453+
CardSectionDivider()
454+
455+
FoodSearchEnableRow(isFoodSearchEnabled: $isFoodSearchEnabled)
456+
.padding(.bottom, 2)
457+
}
439458
}
440459
.padding(.vertical, 12)
441460
.padding(.horizontal, 12)
@@ -1628,3 +1647,46 @@ struct AIAbsorptionTimePickerRow: View {
16281647
return durationFormatter.string(from: absorptionTime) ?? ""
16291648
}
16301649
}
1650+
1651+
// MARK: - Food Search Enable Row
1652+
struct FoodSearchEnableRow: View {
1653+
@Binding var isFoodSearchEnabled: Bool
1654+
@State private var isAnimating = false
1655+
1656+
var body: some View {
1657+
VStack(alignment: .leading, spacing: 0) {
1658+
HStack {
1659+
HStack(spacing: 8) {
1660+
Image(systemName: "brain.head.profile")
1661+
.font(.title3)
1662+
.foregroundColor(.blue)
1663+
.scaleEffect(isAnimating ? 1.1 : 1.0)
1664+
.animation(.easeInOut(duration: 2.0).repeatForever(autoreverses: true), value: isAnimating)
1665+
1666+
Text("Enable Food Search")
1667+
.font(.body)
1668+
.fontWeight(.medium)
1669+
.foregroundColor(.primary)
1670+
}
1671+
1672+
Spacer()
1673+
1674+
Toggle("", isOn: $isFoodSearchEnabled)
1675+
.labelsHidden()
1676+
.scaleEffect(0.8)
1677+
.onChange(of: isFoodSearchEnabled) { newValue in
1678+
UserDefaults.standard.foodSearchEnabled = newValue
1679+
}
1680+
}
1681+
1682+
Text("Add AI-powered nutrition analysis")
1683+
.font(.caption)
1684+
.foregroundColor(.secondary)
1685+
.padding(.top, 2)
1686+
.padding(.leading, 32) // Align with text above
1687+
}
1688+
.onAppear {
1689+
isAnimating = true
1690+
}
1691+
}
1692+
}

0 commit comments

Comments
 (0)