Skip to content

Commit 1256b0e

Browse files
committed
[LOOP-4685] Add favorite foods to user defaults; renaming
1 parent 8240def commit 1256b0e

File tree

6 files changed

+64
-67
lines changed

6 files changed

+64
-67
lines changed

Loop.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1419606928D9554E00BA86E0 /* LoopKitUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 437AFEE6203688CF008C4892 /* LoopKitUI.framework */; };
1212
1419606A28D955BC00BA86E0 /* MockKitUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C101947127DD473C004E7EB8 /* MockKitUI.framework */; };
1313
142CB7592A60BF2E0075748A /* EditMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142CB7582A60BF2E0075748A /* EditMode.swift */; };
14-
142CB75B2A60BFC30075748A /* MealsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142CB75A2A60BFC30075748A /* MealsView.swift */; };
14+
142CB75B2A60BFC30075748A /* FavoriteFoodsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142CB75A2A60BFC30075748A /* FavoriteFoodsView.swift */; };
1515
1481F9BB28DA26F4004C5AEB /* LoopUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4F75288B1DFE1DC600C322D6 /* LoopUI.framework */; };
1616
14B1735E28AED9EC006CCD7C /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 14B1735D28AED9EC006CCD7C /* WidgetKit.framework */; };
1717
14B1736028AED9EC006CCD7C /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 14B1735F28AED9EC006CCD7C /* SwiftUI.framework */; };
@@ -788,7 +788,7 @@
788788

789789
/* Begin PBXFileReference section */
790790
142CB7582A60BF2E0075748A /* EditMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditMode.swift; sourceTree = "<group>"; };
791-
142CB75A2A60BFC30075748A /* MealsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MealsView.swift; sourceTree = "<group>"; };
791+
142CB75A2A60BFC30075748A /* FavoriteFoodsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FavoriteFoodsView.swift; sourceTree = "<group>"; };
792792
14B1735C28AED9EC006CCD7C /* SmallStatusWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = SmallStatusWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
793793
14B1735D28AED9EC006CCD7C /* WidgetKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WidgetKit.framework; path = System/Library/Frameworks/WidgetKit.framework; sourceTree = SDKROOT; };
794794
14B1735F28AED9EC006CCD7C /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; };
@@ -2389,12 +2389,12 @@
23892389
431A8C3F1EC6E8AB00823B9C /* CircleMaskView.swift */,
23902390
A9A056B224B93C62007CF06D /* CriticalEventLogExportView.swift */,
23912391
C191D2A025B3ACAA00C26C0B /* DosingStrategySelectionView.swift */,
2392+
142CB75A2A60BFC30075748A /* FavoriteFoodsView.swift */,
23922393
43D381611EBD9759007F8C8F /* HeaderValuesTableViewCell.swift */,
23932394
B43CF07D29434EC4008A520B /* HowMuteAlertWorkView.swift */,
23942395
430D85881F44037000AF2D4F /* HUDViewTableViewCell.swift */,
23952396
A91D2A3E26CF0FF80023B075 /* IconTitleSubtitleTableViewCell.swift */,
23962397
C1742331259BEADC00399C9D /* ManualEntryDoseView.swift */,
2397-
142CB75A2A60BFC30075748A /* MealsView.swift */,
23982398
1DA46B5F2492E2E300D71A63 /* NotificationsCriticalAlertPermissionsView.swift */,
23992399
899433B723FE129700FA4BEA /* OverrideBadgeView.swift */,
24002400
89D6953D23B6DF8A002B3066 /* PotentialCarbEntryTableViewCell.swift */,
@@ -3856,7 +3856,7 @@
38563856
43A567691C94880B00334FAC /* LoopDataManager.swift in Sources */,
38573857
1DA649A9244126DA00F61E75 /* InAppModalAlertScheduler.swift in Sources */,
38583858
43B260491ED248FB008CAA77 /* CarbEntryTableViewCell.swift in Sources */,
3859-
142CB75B2A60BFC30075748A /* MealsView.swift in Sources */,
3859+
142CB75B2A60BFC30075748A /* FavoriteFoodsView.swift in Sources */,
38603860
A9D5C5B625DC6C6A00534873 /* LoopAppManager.swift in Sources */,
38613861
4302F4E11D4E9C8900F0FCAF /* TextFieldTableViewController.swift in Sources */,
38623862
C1742332259BEADC00399C9D /* ManualEntryDoseView.swift in Sources */,

Loop/Extensions/UserDefaults+Loop.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extension UserDefaults {
1616
case legacyServicesState = "com.loopkit.Loop.ServicesState"
1717
case loopNotRunningNotifications = "com.loopkit.Loop.loopNotRunningNotifications"
1818
case inFlightAutomaticDose = "com.loopkit.Loop.inFlightAutomaticDose"
19+
case favoriteFoodsEnabled = "com.loopkit.Loop.favoriteFoodsEnabled"
1920
}
2021

2122
var legacyPumpManagerRawValue: PumpManager.RawValue? {
@@ -89,4 +90,13 @@ extension UserDefaults {
8990
}
9091
}
9192
}
93+
94+
var favoriteFoodsEnabled: Bool {
95+
get {
96+
bool(forKey: Key.favoriteFoodsEnabled.rawValue)
97+
}
98+
set {
99+
set(newValue, forKey: Key.favoriteFoodsEnabled.rawValue)
100+
}
101+
}
92102
}

Loop/Managers/SettingsManager.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ class SettingsManager {
109109
maximumBolus: latestSettings.maximumBolus,
110110
suspendThreshold: latestSettings.suspendThreshold,
111111
automaticDosingStrategy: latestSettings.automaticDosingStrategy,
112-
defaultRapidActingModel: latestSettings.defaultRapidActingModel?.presetForRapidActingInsulin,
113-
favoriteFoodsEnabled: latestSettings.favoriteFoodsEnabled)
112+
defaultRapidActingModel: latestSettings.defaultRapidActingModel?.presetForRapidActingInsulin)
114113
}
115114
}
116115

@@ -133,7 +132,6 @@ class SettingsManager {
133132
deviceToken: deviceToken,
134133
insulinType: deviceStatusProvider?.pumpManagerStatus?.insulinType,
135134
defaultRapidActingModel: newLoopSettings.defaultRapidActingModel.map(StoredInsulinModel.init),
136-
favoriteFoodsEnabled: newLoopSettings.favoriteFoodsEnabled,
137135
basalRateSchedule: newLoopSettings.basalRateSchedule,
138136
insulinSensitivitySchedule: newLoopSettings.insulinSensitivitySchedule,
139137
carbRatioSchedule: newLoopSettings.carbRatioSchedule,

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,14 @@ final class StatusTableViewController: LoopChartsTableViewController {
274274
let bolus = UIBarButtonItem(image: UIImage(named: "bolus"), style: .plain, target: self, action: #selector(presentBolusScreen))
275275
let settings = UIBarButtonItem(image: UIImage(named: "settings"), style: .plain, target: self, action: #selector(onSettingsTapped))
276276

277-
let favoriteFoodsEnabled = deviceManager.loopManager.settings.favoriteFoodsEnabled
278-
if favoriteFoodsEnabled {
279-
let meals = UIBarButtonItem(image: UIImage(named: "meals"), style: .plain, target: self, action: #selector(presentMealsScreen))
277+
let favoriteFoodsEnabled = UserDefaults.appGroup?.favoriteFoodsEnabled
278+
if favoriteFoodsEnabled == true {
279+
let favoriteFoods = UIBarButtonItem(image: UIImage(named: "favorite-foods"), style: .plain, target: self, action: #selector(presentFavoriteFoodsScreen))
280280
let presets = createPresetsButtonItem(selected: false, isEnabled: true)
281281
toolbarItems = [
282282
carbs,
283283
space,
284-
meals,
284+
favoriteFoods,
285285
space,
286286
bolus,
287287
space,
@@ -319,8 +319,8 @@ final class StatusTableViewController: LoopChartsTableViewController {
319319
toolbarItems![8].accessibilityLabel = NSLocalizedString("Settings", comment: "The label of the settings button")
320320
toolbarItems![8].tintColor = UIColor.secondaryLabel
321321

322-
let favoriteFoodsEnabled = deviceManager.loopManager.settings.favoriteFoodsEnabled
323-
if favoriteFoodsEnabled {
322+
let favoriteFoodsEnabled = UserDefaults.appGroup?.favoriteFoodsEnabled
323+
if favoriteFoodsEnabled == true {
324324
toolbarItems![2] = createFavoriteFoodsButtonItem()
325325

326326
let selected = (preMealMode == true && preMealModeAllowed) || (workoutMode == true && workoutModeAllowed)
@@ -551,7 +551,7 @@ final class StatusTableViewController: LoopChartsTableViewController {
551551
}
552552
}
553553

554-
// updatePreMealModeAvailability(automaticDosingEnabled: automaticDosingEnabled)
554+
updatePreMealModeAvailability(automaticDosingEnabled: automaticDosingEnabled)
555555

556556
if deviceManager.loopManager.settings.preMealTargetRange == nil {
557557
preMealMode = nil
@@ -1414,11 +1414,11 @@ final class StatusTableViewController: LoopChartsTableViewController {
14141414
presentBolusEntryView()
14151415
}
14161416

1417-
@objc func presentMealsScreen() {
1417+
@objc func presentFavoriteFoodsScreen() {
14181418
let hostingController: DismissibleHostingController
14191419

1420-
let mealsView = MealsView().environmentObject(deviceManager.displayGlucosePreference)
1421-
hostingController = DismissibleHostingController(rootView: mealsView, isModalInPresentation: false)
1420+
let favoriteFoodsView = FavoriteFoodsView().environmentObject(deviceManager.displayGlucosePreference)
1421+
hostingController = DismissibleHostingController(rootView: favoriteFoodsView, isModalInPresentation: false)
14221422
present(hostingController, animated: true)
14231423
}
14241424

@@ -1444,7 +1444,7 @@ final class StatusTableViewController: LoopChartsTableViewController {
14441444
}
14451445

14461446
private func createFavoriteFoodsButtonItem() -> UIBarButtonItem {
1447-
let item = UIBarButtonItem(image: UIImage(named: "favorite-foods")!, style: .plain, target: self, action: #selector(presentMealsScreen))
1447+
let item = UIBarButtonItem(image: UIImage(named: "favorite-foods")!, style: .plain, target: self, action: #selector(presentFavoriteFoodsScreen))
14481448
item.accessibilityLabel = NSLocalizedString("Favorite Foods", comment: "The label of the favorite foods button")
14491449

14501450
item.tintColor = UIColor.carbTintColor
@@ -2323,9 +2323,7 @@ extension StatusTableViewController: ServicesViewModelDelegate {
23232323

23242324
extension StatusTableViewController: FavoriteFoodsFeatureUnlockDelegate {
23252325
func featureAvailabilityChanged() {
2326-
deviceManager.loopManager.mutateSettings { settings in
2327-
settings.favoriteFoodsEnabled.toggle()
2328-
}
2326+
UserDefaults.appGroup?.favoriteFoodsEnabled.toggle()
23292327
self.updateToolbarItems()
23302328
}
23312329
}

Loop/Views/MealsView.swift renamed to Loop/Views/FavoriteFoodsView.swift

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// MealsView.swift
2+
// FavoriteFoodsView.swift
33
// Loop
44
//
55
// Created by Noah Brauner on 7/12/23.
@@ -11,23 +11,23 @@ import LoopKit
1111
import LoopKitUI
1212
import HealthKit
1313

14-
struct MealsView: View {
14+
struct FavoriteFoodsView: View {
1515
@EnvironmentObject private var displayGlucosePreference: DisplayGlucosePreference
1616
@Environment(\.dismissAction) private var dismiss
1717
@Environment(\.carbTintColor) private var carbTintColor
1818

19-
@State private var mealToConfirmDeleteId: String? = nil
19+
@State private var foodToConfirmDeleteId: String? = nil
2020
@State private var editMode: EditMode = .inactive
2121

22-
@State private var meals = allMeals
22+
@State private var foods = allFoods
2323

2424
@State var isBolusViewActive = false
2525
@State var isEditViewActive = false
2626
@State var isAddViewActive = false
2727

28-
@State var selectedMeal: Meal? = nil
28+
@State var selectedFood: FavoriteFood? = nil
2929

30-
@State private var draggingMeal: Meal?
30+
@State private var draggingFood: FavoriteFood?
3131
@State private var hasChangedLocation: Bool = false
3232

3333
var body: some View {
@@ -46,13 +46,13 @@ struct MealsView: View {
4646
editButton
4747
}
4848

49-
ForEach(meals) { meal in
50-
draggableMealCardView(meal: meal)
49+
ForEach(foods) { food in
50+
draggableFoodCardView(food: food)
5151
}
5252
}
5353
.environment(\.editMode, self.$editMode)
5454

55-
newMealButton
55+
newFoodButton
5656
}
5757
.padding()
5858
}
@@ -78,58 +78,58 @@ struct MealsView: View {
7878
}
7979
.onChange(of: editMode) { newValue in
8080
if !newValue.isEditing {
81-
mealToConfirmDeleteId = nil
81+
foodToConfirmDeleteId = nil
8282
}
8383
}
8484
}
8585

86-
private func addMeal() {
86+
private func addFood() {
8787
isAddViewActive = true
8888
}
8989

90-
private func onMealTap(_ meal: Meal) {
91-
selectedMeal = meal
90+
private func onFoodTap(_ food: FavoriteFood) {
91+
selectedFood = food
9292
if editMode.isEditing {
9393
isEditViewActive = true
9494
}
9595
else {
9696
isBolusViewActive = true
9797
}
9898
}
99-
private func onMealDelete(_ meal: Meal) {
99+
private func onFoodDelete(_ food: FavoriteFood) {
100100
withAnimation(.easeInOut(duration: 0.3)) {
101-
_ = meals.remove(meal)
101+
_ = foods.remove(food)
102102
}
103103
}
104104

105-
private func onMealReorder(from: IndexSet, to: Int) {
105+
private func onFoodReorder(from: IndexSet, to: Int) {
106106
withAnimation {
107-
meals.move(fromOffsets: from, toOffset: to)
107+
foods.move(fromOffsets: from, toOffset: to)
108108
}
109109
}
110110
}
111111

112-
extension MealsView {
113-
@ViewBuilder func draggableMealCardView(meal: Meal) -> some View {
112+
extension FavoriteFoodsView {
113+
@ViewBuilder func draggableFoodCardView(food: FavoriteFood) -> some View {
114114
Button(action: {
115-
onMealTap(meal)
115+
onFoodTap(food)
116116
}) {
117-
MealCardView(meal: meal, mealToConfirmDeleteId: $mealToConfirmDeleteId, onMealTap: onMealTap(_:), onMealDelete: onMealDelete(_:))
117+
FavoriteFoodCardView(food: food, foodToConfirmDeleteId: $foodToConfirmDeleteId, onFoodTap: onFoodTap(_:), onFoodDelete: onFoodDelete(_:))
118118
.onDrag {
119-
draggingMeal = meal
120-
return NSItemProvider(object: "\(meal.id)" as NSString)
119+
draggingFood = food
120+
return NSItemProvider(object: "\(food.id)" as NSString)
121121
} preview: {
122-
MealCardView(meal: meal, mealToConfirmDeleteId: $mealToConfirmDeleteId, onMealTap: onMealTap(_:), onMealDelete: onMealDelete(_:))
122+
FavoriteFoodCardView(food: food, foodToConfirmDeleteId: $foodToConfirmDeleteId, onFoodTap: onFoodTap(_:), onFoodDelete: onFoodDelete(_:))
123123
}
124124
.onDrop(
125125
of: [UTType.text],
126126
delegate: DragRelocateDelegate(
127-
item: meal,
128-
listData: meals,
129-
current: $draggingMeal,
127+
item: food,
128+
listData: foods,
129+
current: $draggingFood,
130130
hasChangedLocation: $hasChangedLocation
131131
) { from, to in
132-
onMealReorder(from: from, to: to)
132+
onFoodReorder(from: from, to: to)
133133
}
134134
)
135135
.disabled(!editMode.isEditing)
@@ -144,7 +144,7 @@ extension MealsView {
144144
}
145145

146146
// private var plusButton: some View {
147-
// Button(action: addMeal) {
147+
// Button(action: addFood) {
148148
// Image(systemName: "plus")
149149
// }
150150
// }
@@ -159,8 +159,8 @@ extension MealsView {
159159
}
160160
}
161161

162-
private var newMealButton: some View {
163-
Button(action: addMeal) {
162+
private var newFoodButton: some View {
163+
Button(action: addFood) {
164164
HStack {
165165
Image(systemName: "plus.circle.fill")
166166

@@ -221,12 +221,12 @@ fileprivate struct DragRelocateDelegate<Item: Equatable>: DropDelegate {
221221
}
222222
}
223223

224-
fileprivate let allMeals = [
224+
fileprivate let allFoods = [
225225
// Some really yummy foods...
226-
Meal(carbsQuantity: carbs(55), foodType: "🥞🥚", absorptionTime: .hours(3), name: "Pancakes and Eggs"),
227-
Meal(carbsQuantity: carbs(35), foodType: "🍌🍞", absorptionTime: .hours(2), name: "Banana Bread"),
228-
Meal(carbsQuantity: carbs(63), foodType: "🍞🥜🍫🥛", absorptionTime: .hours(3), name: "The Best Lunch"),
229-
Meal(carbsQuantity: carbs(120), foodType: "🍕", absorptionTime: .hours(5), name: "Dad's Pizza"),
226+
FavoriteFood(carbsQuantity: carbs(55), foodType: "🥞🥚", absorptionTime: .hours(3), name: "Pancakes and Eggs"),
227+
FavoriteFood(carbsQuantity: carbs(35), foodType: "🍌🍞", absorptionTime: .hours(2), name: "Banana Bread"),
228+
FavoriteFood(carbsQuantity: carbs(63), foodType: "🍞🥜🍫🥛", absorptionTime: .hours(3), name: "The Best Lunch"),
229+
FavoriteFood(carbsQuantity: carbs(120), foodType: "🍕", absorptionTime: .hours(5), name: "Dad's Pizza"),
230230
]
231231

232232
fileprivate func carbs(_ value: Double) -> HKQuantity {

LoopCore/LoopSettings.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ public struct LoopSettings: Equatable {
7575

7676
public var defaultRapidActingModel: ExponentialInsulinModelPreset?
7777

78-
public var favoriteFoodsEnabled = false
79-
8078
public var glucoseUnit: HKUnit? {
8179
return glucoseTargetRangeSchedule?.unit
8280
}
@@ -96,8 +94,7 @@ public struct LoopSettings: Equatable {
9694
maximumBolus: Double? = nil,
9795
suspendThreshold: GlucoseThreshold? = nil,
9896
automaticDosingStrategy: AutomaticDosingStrategy = .tempBasalOnly,
99-
defaultRapidActingModel: ExponentialInsulinModelPreset? = nil,
100-
favoriteFoodsEnabled: Bool = false
97+
defaultRapidActingModel: ExponentialInsulinModelPreset? = nil
10198
) {
10299
self.dosingEnabled = dosingEnabled
103100
self.glucoseTargetRangeSchedule = glucoseTargetRangeSchedule
@@ -114,7 +111,6 @@ public struct LoopSettings: Equatable {
114111
self.suspendThreshold = suspendThreshold
115112
self.automaticDosingStrategy = automaticDosingStrategy
116113
self.defaultRapidActingModel = defaultRapidActingModel
117-
self.favoriteFoodsEnabled = favoriteFoodsEnabled
118114
}
119115
}
120116

@@ -281,10 +277,6 @@ extension LoopSettings: RawRepresentable {
281277
{
282278
self.automaticDosingStrategy = automaticDosingStrategy
283279
}
284-
285-
if let favoriteFoodsEnabled = rawValue["favoriteFoodsEnabled"] as? Bool {
286-
self.favoriteFoodsEnabled = favoriteFoodsEnabled
287-
}
288280
}
289281

290282
public var rawValue: RawValue {
@@ -303,7 +295,6 @@ extension LoopSettings: RawRepresentable {
303295
raw["maximumBolus"] = maximumBolus
304296
raw["minimumBGGuard"] = suspendThreshold?.rawValue
305297
raw["dosingStrategy"] = automaticDosingStrategy.rawValue
306-
raw["favoriteFoodsEnabled"] = favoriteFoodsEnabled
307298

308299
return raw
309300
}

0 commit comments

Comments
 (0)