|
| 1 | +// |
| 2 | +// AddEditFavoriteFoodView.swift |
| 3 | +// Loop |
| 4 | +// |
| 5 | +// Created by Noah Brauner on 7/31/23. |
| 6 | +// Copyright © 2023 LoopKit Authors. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | +import LoopKit |
| 11 | +import LoopKitUI |
| 12 | + |
| 13 | +struct AddEditFavoriteFoodView: View { |
| 14 | + @Environment(\.dismiss) var dismiss |
| 15 | + |
| 16 | + @StateObject private var viewModel: AddEditFavoriteFoodViewModel |
| 17 | + |
| 18 | + @State private var expandedRow: Row? |
| 19 | + @State private var showHowAbsorptionTimeWorks = false |
| 20 | + |
| 21 | + private var isNewEntry = true |
| 22 | + |
| 23 | + /// Initializer for adding a new favorite food or editing a `StoredFavoriteFood` |
| 24 | + init(originalFavoriteFood: StoredFavoriteFood? = nil, onSave: @escaping (NewFavoriteFood) -> Void) { |
| 25 | + self._viewModel = StateObject(wrappedValue: AddEditFavoriteFoodViewModel(originalFavoriteFood: originalFavoriteFood, onSave: onSave)) |
| 26 | + self.isNewEntry = originalFavoriteFood == nil |
| 27 | + } |
| 28 | + |
| 29 | + /// Initializer for presenting the `AddEditFavoriteFoodView` prepopulated from the `CarbEntryView` |
| 30 | + init(carbsQuantity: Double?, foodType: String, absorptionTime: TimeInterval, onSave: @escaping (NewFavoriteFood) -> Void) { |
| 31 | + self._viewModel = StateObject(wrappedValue: AddEditFavoriteFoodViewModel(carbsQuantity: carbsQuantity, foodType: foodType, absorptionTime: absorptionTime, onSave: onSave)) |
| 32 | + } |
| 33 | + |
| 34 | + var body: some View { |
| 35 | + if isNewEntry { |
| 36 | + NavigationView { |
| 37 | + content |
| 38 | + .toolbar { |
| 39 | + ToolbarItem(placement: .navigationBarLeading) { |
| 40 | + dismissButton |
| 41 | + } |
| 42 | + |
| 43 | + ToolbarItem(placement: .navigationBarTrailing) { |
| 44 | + saveButton |
| 45 | + } |
| 46 | + } |
| 47 | + .navigationBarTitle("New Favorite Food", displayMode: .inline) |
| 48 | + .onAppear { |
| 49 | + expandedRow = .name |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + else { |
| 54 | + content |
| 55 | + .toolbar { |
| 56 | + ToolbarItem(placement: .navigationBarLeading) { |
| 57 | + if viewModel.updatedFavoriteFood != nil { |
| 58 | + dismissButton |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + ToolbarItem(placement: .navigationBarTrailing) { |
| 63 | + saveButton |
| 64 | + } |
| 65 | + } |
| 66 | + .navigationBarBackButtonHidden(viewModel.updatedFavoriteFood != nil) |
| 67 | + .navigationBarTitle(viewModel.originalFavoriteFood?.title ?? "", displayMode: .inline) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private var content: some View { |
| 72 | + ZStack { |
| 73 | + Color(.systemGroupedBackground) |
| 74 | + .edgesIgnoringSafeArea(.all) |
| 75 | + |
| 76 | + ScrollView { |
| 77 | + card |
| 78 | + .padding(.top, 8) |
| 79 | + |
| 80 | + saveActionButton |
| 81 | + } |
| 82 | + } |
| 83 | + .alert(item: $viewModel.alert, content: alert(for:)) |
| 84 | + .sheet(isPresented: $showHowAbsorptionTimeWorks) { |
| 85 | + HowAbsorptionTimeWorksView() |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private var card: some View { |
| 90 | + VStack(spacing: 10) { |
| 91 | + TextFieldRow(text: $viewModel.name, title: "Name", placeholder: "Apple", expandedRow: $expandedRow, row: .name) |
| 92 | + |
| 93 | + CardSectionDivider() |
| 94 | + |
| 95 | + CarbQuantityRow(quantity: $viewModel.carbsQuantity, title: "Carb Quantity", preferredCarbUnit: viewModel.preferredCarbUnit, expandedRow: $expandedRow, row: Row.amountConsumed) |
| 96 | + |
| 97 | + CardSectionDivider() |
| 98 | + |
| 99 | + EmojiRow(emojiType: .food, text: $viewModel.foodType, title: "Food Type", expandedRow: $expandedRow, row: .foodType) |
| 100 | + |
| 101 | + CardSectionDivider() |
| 102 | + |
| 103 | + AbsorptionTimePickerRow(absorptionTime: $viewModel.absorptionTime, validDurationRange: viewModel.absorptionRimesRange, expandedRow: $expandedRow, row: Row.absorptionTime, showHowAbsorptionTimeWorks: $showHowAbsorptionTimeWorks) |
| 104 | + .padding(.bottom, 2) |
| 105 | + } |
| 106 | + .padding(.vertical, 12) |
| 107 | + .padding(.horizontal) |
| 108 | + .background(CardBackground()) |
| 109 | + .padding(.horizontal) |
| 110 | + } |
| 111 | + |
| 112 | + private func alert(for alert: AddEditFavoriteFoodViewModel.Alert) -> SwiftUI.Alert { |
| 113 | + switch alert { |
| 114 | + case .maxQuantityExceded: |
| 115 | + let message = String( |
| 116 | + format: NSLocalizedString("The maximum allowed amount is %@ grams.", comment: "Alert body displayed for quantity greater than max (1: maximum quantity in grams)"), |
| 117 | + NumberFormatter.localizedString(from: NSNumber(value: viewModel.maxCarbEntryQuantity.doubleValue(for: viewModel.preferredCarbUnit)), number: .none) |
| 118 | + ) |
| 119 | + let okMessage = NSLocalizedString("com.loudnate.LoopKit.errorAlertActionTitle", value: "OK", comment: "The title of the action used to dismiss an error alert") |
| 120 | + return SwiftUI.Alert( |
| 121 | + title: Text("Large Meal Entered", comment: "Title of the warning shown when a large meal was entered"), |
| 122 | + message: Text(message), |
| 123 | + dismissButton: .cancel(Text(okMessage), action: viewModel.clearAlert) |
| 124 | + ) |
| 125 | + case .warningQuantityValidation: |
| 126 | + let message = String( |
| 127 | + format: NSLocalizedString("Did you intend to enter %1$@ grams as the amount of carbohydrates for this meal?", comment: "Alert body when entered carbohydrates is greater than threshold (1: entered quantity in grams)"), |
| 128 | + NumberFormatter.localizedString(from: NSNumber(value: viewModel.carbsQuantity ?? 0), number: .none) |
| 129 | + ) |
| 130 | + return SwiftUI.Alert( |
| 131 | + title: Text("Large Meal Entered", comment: "Title of the warning shown when a large meal was entered"), |
| 132 | + message: Text(message), |
| 133 | + primaryButton: .default(Text("No, edit amount", comment: "The title of the action used when rejecting the the amount of carbohydrates entered."), action: viewModel.clearAlert), |
| 134 | + secondaryButton: .cancel(Text("Yes", comment: "The title of the action used when confirming entered amount of carbohydrates."), action: viewModel.clearAlertAndSave) |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +extension AddEditFavoriteFoodView { |
| 141 | + private var dismissButton: some View { |
| 142 | + Button(action: dismiss.callAsFunction) { |
| 143 | + Text("Cancel") |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private var saveActionButton: some View { |
| 148 | + Button(action: viewModel.save) { |
| 149 | + Text("Save") |
| 150 | + } |
| 151 | + .buttonStyle(ActionButtonStyle()) |
| 152 | + .padding() |
| 153 | + .disabled(viewModel.updatedFavoriteFood == nil) |
| 154 | + } |
| 155 | + |
| 156 | + private var saveButton: some View { |
| 157 | + Button(action: viewModel.save) { |
| 158 | + Text("Save") |
| 159 | + } |
| 160 | + .disabled(viewModel.updatedFavoriteFood == nil) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +extension AddEditFavoriteFoodView { |
| 165 | + enum Row { |
| 166 | + case name, amountConsumed, foodType, absorptionTime |
| 167 | + } |
| 168 | +} |
0 commit comments