Pārlūkot izejas kodu

Add guard to avoid index out of range issues

Deniz Cengiz 1 gadu atpakaļ
vecāks
revīzija
b988b9cc2e

+ 10 - 1
FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorStateModel.swift

@@ -119,7 +119,16 @@ extension BasalProfileEditor {
         }
 
         func availableTimeIndices(_ itemIndex: Int) -> [Int] {
-            let usedIndicesByOtherItems = items.filter { $0 != items[itemIndex] }.map(\.timeIndex)
+            // avoid index out of range issues
+            guard itemIndex >= 0 && itemIndex < items.count else {
+                return []
+            }
+            
+            let usedIndicesByOtherItems = items
+                .enumerated()
+                .filter { $0.offset != itemIndex }
+                .map { $0.element.timeIndex }
+            
             return (0 ..< timeValues.count).filter { !usedIndicesByOtherItems.contains($0) }
         }
     }