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

Edit profile overrides
- Enable edit of all settings and/or name for already saved profile overrides
- Swipe left to edit or delete
- Get list of all settings included in the override when saving a new one or editing an existing one

dsnallfot 2 лет назад
Родитель
Сommit
cf3f87e962

+ 20 - 0
FreeAPS/Sources/Modules/OverrideProfilesConfig/OverrideProfilesStateModel.swift

@@ -248,5 +248,25 @@ extension OverrideProfilesConfig {
             smbMinutes = defaultSmbMinutes
             uamMinutes = defaultUamMinutes
         }
+
+        func updatePreset(_ preset: OverridePresets) {
+            let context = CoreDataStack.shared.persistentContainer.viewContext
+            context.performAndWait {
+                preset.name = profileName
+                preset.percentage = percentage
+                preset.duration = NSDecimalNumber(decimal: duration)
+                preset.target = override_target ? NSDecimalNumber(decimal: target) : nil
+                preset.indefinite = _indefinite
+                preset.advancedSettings = advancedSettings
+                preset.smbIsOff = smbIsOff
+                preset.smbIsScheduledOff = smbIsScheduledOff
+                preset.isf = isf
+                preset.cr = cr
+                preset.smbMinutes = NSDecimalNumber(decimal: smbMinutes)
+                preset.uamMinutes = NSDecimalNumber(decimal: uamMinutes)
+                preset.isfAndCr = isfAndCr
+                try? context.save()
+            }
+        }
     }
 }

+ 136 - 4
FreeAPS/Sources/Modules/OverrideProfilesConfig/View/OverrideProfilesRootView.swift

@@ -10,6 +10,9 @@ extension OverrideProfilesConfig {
         @State private var isEditing = false
         @State private var showAlert = false
         @State private var showingDetail = false
+        @State private var alertString = ""
+        @State private var selectedPreset: OverridePresets?
+        @State private var isEditSheetPresented: Bool = false
         @State private var alertSring = ""
         @State var isSheetPresented: Bool = false
 
@@ -44,8 +47,42 @@ extension OverrideProfilesConfig {
         var presetPopover: some View {
             Form {
                 Section {
-                    TextField("Name Of Profile", text: $state.profileName)
-                } header: { Text("Enter Name of Profile") }
+                    TextField("Override preset name", text: $state.profileName)
+                } header: { Text("Enter a name") }
+
+                Section(header: Text("Settings to save")) {
+                    let percentString = Text("Override: \(Int(state.percentage))%")
+                    let targetString = state
+                        .target != 0 ? Text("Target: \(state.target.formatted()) \(state.units.rawValue)") :
+                        Text("")
+                    let durationString = state
+                        ._indefinite ? Text("Duration: Indefinite") :
+                        Text("Duration: \(state.duration.formatted()) minutes")
+                    let isfString = state.isf ? Text("Change ISF") : Text("")
+                    let crString = state.cr ? Text("Change CR") : Text("")
+                    let smbString = state.smbIsOff ? Text("Disable SMB") : Text("")
+                    let scheduledSMBString = state.smbIsScheduledOff ? Text("SMB Schedule On") : Text("")
+                    let maxMinutesSMBString = state
+                        .smbMinutes != 0 ? Text("\(state.smbMinutes.formatted()) SMB Basal minutes") :
+                        Text("")
+                    let maxMinutesUAMString = state
+                        .uamMinutes != 0 ? Text("\(state.uamMinutes.formatted()) UAM Basal minutes") :
+                        Text("")
+
+                    VStack(alignment: .leading, spacing: 2) {
+                        percentString
+                        if targetString != Text("") { targetString }
+                        if durationString != Text("") { durationString }
+                        if isfString != Text("") { isfString }
+                        if crString != Text("") { crString }
+                        if smbString != Text("") { smbString }
+                        if scheduledSMBString != Text("") { scheduledSMBString }
+                        if maxMinutesSMBString != Text("") { maxMinutesSMBString }
+                        if maxMinutesUAMString != Text("") { maxMinutesUAMString }
+                    }
+                    .foregroundColor(.secondary)
+                    .font(.caption)
+                }
 
                 Section {
                     Button("Save") {
@@ -61,13 +98,104 @@ extension OverrideProfilesConfig {
             }
         }
 
+        var editPresetPopover: some View {
+            Form {
+                Section {
+                    TextField("Override preset name", text: $state.profileName)
+                } header: { Text("Keep or change name?") }
+
+                Section(header: Text("New settings to save")) {
+                    let percentString = Text("Override: \(Int(state.percentage))%")
+                    let targetString = state
+                        .target != 0 ? Text("Target: \(state.target.formatted()) \(state.units.rawValue)") : Text("")
+                    let durationString = state
+                        ._indefinite ? Text("Duration: Indefinite") : Text("Duration: \(state.duration.formatted()) minutes")
+                    let isfString = state.isf ? Text("Change ISF") : Text("")
+                    let crString = state.cr ? Text("Change CR") : Text("")
+                    let smbString = state.smbIsOff ? Text("Disable SMB") : Text("")
+                    let scheduledSMBString = state.smbIsScheduledOff ? Text("SMB Schedule On") : Text("")
+                    let maxMinutesSMBString = state
+                        .smbMinutes != 0 ? Text("\(state.smbMinutes.formatted()) SMB Basal minutes") : Text("")
+                    let maxMinutesUAMString = state
+                        .uamMinutes != 0 ? Text("\(state.uamMinutes.formatted()) UAM Basal minutes") : Text("")
+
+                    VStack(alignment: .leading, spacing: 2) {
+                        percentString
+                        if targetString != Text("") { targetString }
+                        if durationString != Text("") { durationString }
+                        if isfString != Text("") { isfString }
+                        if crString != Text("") { crString }
+                        if smbString != Text("") { smbString }
+                        if scheduledSMBString != Text("") { scheduledSMBString }
+                        if maxMinutesSMBString != Text("") { maxMinutesSMBString }
+                        if maxMinutesUAMString != Text("") { maxMinutesUAMString }
+                    }
+                    .foregroundColor(.secondary)
+                    .font(.caption)
+                }
+
+                Section {
+                    Button("Save") {
+                        guard let selectedPreset = selectedPreset else { return }
+                        state.updatePreset(selectedPreset)
+                        isEditSheetPresented = false
+                    }
+                    .disabled(state.profileName.isEmpty)
+
+                    Button("Cancel") {
+                        isEditSheetPresented = false
+                    }
+                }
+            }
+        }
+
         var body: some View {
             Form {
                 if state.presets.isNotEmpty {
                     Section {
-                        ForEach(fetchedProfiles) { preset in
+                        ForEach(fetchedProfiles.indices, id: \.self) { index in
+                            let preset = fetchedProfiles[index]
                             profilesView(for: preset)
-                        }.onDelete(perform: removeProfile)
+                                .swipeActions {
+                                    Button(role: .destructive) {
+                                        removeProfile(at: IndexSet(integer: index))
+                                    } label: {
+                                        Label("Ta bort", systemImage: "trash")
+                                    }
+
+                                    Button {
+                                        selectedPreset = preset
+                                        state.profileName = preset.name ?? ""
+                                        isEditSheetPresented = true
+                                    } label: {
+                                        Label("Redigera", systemImage: "square.and.pencil")
+                                    }.tint(.blue)
+                                }
+                        }
+                    }
+                    header: { Text("Activate preset override") }
+                    footer: { VStack(alignment: .leading) {
+                        Text("Swipe left on a preset to edit or delete it.")
+                        Text("When you want to edit a preset:")
+                        HStack(alignment: .top) {
+                            Text(" •")
+                            Text(
+                                "First use the override configurator below and select the settings you want to include."
+                            )
+                        }
+                        HStack(alignment: .top) {
+                            Text(" •")
+                            Text(
+                                "Then swipe left on the preset you want to change and click the edit symbol."
+                            )
+                        }
+                        HStack(alignment: .top) {
+                            Text(" •")
+                            Text(
+                                "In the pop-up: Use the existing preset name or enter a new name and click save - Done!"
+                            )
+                        }
+                    }
                     }
                 }
                 Section {
@@ -273,6 +401,10 @@ extension OverrideProfilesConfig {
             .navigationBarTitle("Profiles")
             .navigationBarTitleDisplayMode(.automatic)
             .navigationBarItems(leading: Button("Close", action: state.hideModal))
+            .sheet(isPresented: $isEditSheetPresented) {
+                editPresetPopover
+                    .padding()
+            }
         }
 
         @ViewBuilder private func profilesView(for preset: OverridePresets) -> some View {