Преглед на файлове

Add guard to avoid index out of range issues

Deniz Cengiz преди 1 година
родител
ревизия
b988b9cc2e
променени са 1 файла, в които са добавени 10 реда и са изтрити 1 реда
  1. 10 1
      FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorStateModel.swift

+ 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) }
         }
     }