]> git.r.bdr.sh - rbdr/map/blobdiff - Map/Presentation/MapEditor.swift
Add some debouncing
[rbdr/map] / Map / Presentation / MapEditor.swift
index d62d4adfea1bf2259247fa32b7bc874defdbf463..5cda8f961c48c65488f9f969a6a4735083179a27 100644 (file)
@@ -20,6 +20,8 @@ struct MapEditor: View {
   @State var selectedEvolution: StageType = .behavior
   @State var isSearching: Bool = false
 
+  private let changeDebouncer: Debouncer = Debouncer(seconds: 0.05)
+
   @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
 
   let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
@@ -28,9 +30,11 @@ struct MapEditor: View {
   @State var searchTerm = ""
   @State var selectedTerm = 0
 
-  var results: [Range<String.Index>] {
+  @State var results: [Range<String.Index>] = []
+
+  private func updateRanges() {
     if !isSearching || searchTerm.isEmpty {
-      return []
+      results = []
     }
     let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
     var searchRange = document.text.startIndex..<document.text.endIndex
@@ -41,7 +45,7 @@ struct MapEditor: View {
       searchRange = range.upperBound..<document.text.endIndex
     }
 
-    return ranges
+    results = ranges
 
   }
 
@@ -72,15 +76,16 @@ struct MapEditor: View {
 
           },
           onDismiss: {
-            withAnimation {
-              isSearching = false
-            }
+            isSearching = false
           }
         )
         .onChange(
           of: searchTerm,
           {
-            selectedTerm = 0
+            changeDebouncer.debounce {
+              updateRanges()
+              selectedTerm = 0
+            }
           })
         Divider()
       }