Преглед изворни кода

fix picker being closed abruptly when changing a value

This was due to the generation of new UUIDs which made selectedItemID invalid and led to the picker being closed
Marvin Polscheit пре 6 месеци
родитељ
комит
8958e7de75
1 измењених фајлова са 10 додато и 0 уклоњено
  1. 10 0
      Trio/Sources/Modules/Onboarding/View/TherapySettingEditorView.swift

+ 10 - 0
Trio/Sources/Modules/Onboarding/View/TherapySettingEditorView.swift

@@ -212,6 +212,11 @@ struct TherapySettingEditorView: View {
     }
 
     private func validateTherapySettingItems() {
+        // Store the time value of the currently selected item (if any)
+        let selectedTime = selectedItemID.flatMap { id in
+            items.first(where: { $0.id == id })?.time
+        }
+
         // validates therapy items (i.e. parsed therapy settings into wrapper class)
         var newItems = Array(Set(items)).sorted { $0.time < $1.time }
         if !newItems.isEmpty {
@@ -225,6 +230,11 @@ struct TherapySettingEditorView: View {
         // force ALL items to have new UUIDs (to enforce binding update)
         items = newItems.map { TherapySettingItem(copying: $0, newID: true) }
 
+        // Restore selection by finding the item with the same time value
+        if let selectedTime = selectedTime {
+            selectedItemID = items.first(where: { $0.time == selectedTime })?.id
+        }
+
         // validates underlying "raw" therapy setting (i.e. item of type basal, target, isf, carb ratio)
         validateOnDelete?()
     }