Просмотр исходного кода

Clean up

(cherry picked from commit bcc9781072ca7d7761c244ad2b01c68758d5af44)
Jon Mårtensson 2 лет назад
Родитель
Сommit
4d4c831abb

+ 0 - 9
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -217,15 +217,6 @@ extension Home {
             }
         }
 
-        func saveSettings() {
-            coredataContext.perform {
-                let settings = UXSettings(context: self.coredataContext)
-                settings.hours = self.hours
-                settings.date = Date.now
-                try? self.coredataContext.save()
-            }
-        }
-
         private func setupGlucose() {
             DispatchQueue.main.async { [weak self] in
                 guard let self = self else { return }

+ 14 - 43
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -23,7 +23,7 @@ extension Home {
         @State var timeButtons: [Buttons] = [
             Buttons(label: "2 hours", number: "2", active: false, hours: 2),
             Buttons(label: "4 hours", number: "4", active: false, hours: 4),
-            Buttons(label: "6 hours", number: "6", active: false, hours: 6),
+            Buttons(label: "6 hours", number: "6", active: true, hours: 6),
             Buttons(label: "12 hours", number: "12", active: false, hours: 12),
             Buttons(label: "24 hours", number: "24", active: false, hours: 24)
         ]
@@ -55,11 +55,6 @@ extension Home {
             sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
         ) var enactedSliderTT: FetchedResults<TempTargetsSlider>
 
-        @FetchRequest(
-            entity: UXSettings.entity(),
-            sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
-        ) var fetchedSettings: FetchedResults<UXSettings>
-
         private var numberFormatter: NumberFormatter {
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
@@ -354,19 +349,15 @@ extension Home {
         }
 
         var timeInterval: some View {
-            HStack(alignment: .center) {
-                let saveButton = UXSettings(context: moc)
+            HStack {
                 ForEach(timeButtons) { button in
                     Text(button.active ? NSLocalizedString(button.label, comment: "") : button.number).onTapGesture {
                         let index = timeButtons.firstIndex(where: { $0.label == button.label }) ?? 0
-                        highlightButtons(index, onAppear: false)
-                        saveButton.hours = button.hours
-                        saveButton.date = Date.now
-                        try? moc.save()
+                        highlightButtons(index)
                         state.hours = button.hours
                     }
                     .foregroundStyle(button.active ? .primary : .secondary)
-                    .frame(maxHeight: 20).padding(.horizontal)
+                    .frame(maxHeight: 20).padding(.horizontal, button.active ? 20 : 10)
                     .background(button.active ? Color(.systemGray5) : .clear, in: .capsule(style: .circular))
                 }
                 Image(systemName: "ellipsis.circle.fill")
@@ -530,29 +521,16 @@ extension Home {
             return (name: profileString, isOn: display)
         }
 
-        func highlightButtons(_ int: Int?, onAppear: Bool) {
+        func highlightButtons(_ int: Int) {
             var index = 0
-            if let integer = int, !onAppear {
-                repeat {
-                    if index == integer {
-                        timeButtons[index].active = true
-                    } else {
-                        timeButtons[index].active = false
-                    }
-                    index += 1
-                } while index < timeButtons.count
-            } else if onAppear {
-                let i = timeButtons.firstIndex(where: { $0.hours == (fetchedSettings.first?.hours ?? 6) }) ?? 2
-                index = 0
-                repeat {
-                    if index == i {
-                        timeButtons[index].active = true
-                    } else {
-                        timeButtons[index].active = false
-                    }
-                    index += 1
-                } while index < timeButtons.count
-            }
+            repeat {
+                if index == int {
+                    timeButtons[index].active = true
+                } else {
+                    timeButtons[index].active = false
+                }
+                index += 1
+            } while index < timeButtons.count
         }
 
         @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
@@ -660,11 +638,7 @@ extension Home {
                 }
                 .edgesIgnoringSafeArea(.vertical)
             }
-            .onAppear {
-                configureView {
-                    highlightButtons(nil, onAppear: true)
-                }
-            }
+            .onAppear(perform: configureView)
             .navigationTitle("Home")
             .navigationBarHidden(true)
             .ignoresSafeArea(.keyboard)
@@ -687,9 +661,6 @@ extension Home {
                             }
                     )
             }
-            .onDisappear {
-                state.saveSettings()
-            }
         }
 
         private var popup: some View {