From ad07457cf7ab44cd74fadf91d94526ec8815c64b Mon Sep 17 00:00:00 2001 From: D Osman <80430633+DawudOsman@users.noreply.github.com> Date: Thu, 17 Apr 2025 15:58:57 +0100 Subject: [PATCH] add debounce to SearchBar (#81) --- Sora/Views/SearchView.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sora/Views/SearchView.swift b/Sora/Views/SearchView.swift index cfd739d..ab63ae9 100644 --- a/Sora/Views/SearchView.swift +++ b/Sora/Views/SearchView.swift @@ -311,6 +311,7 @@ struct SearchView: View { } struct SearchBar: View { + @State private var debounceTimer: Timer? @Binding var text: String var onSearchButtonClicked: () -> Void @@ -321,6 +322,14 @@ struct SearchBar: View { .padding(.horizontal, 25) .background(Color(.systemGray6)) .cornerRadius(8) + .onChange(of: text){newValue in + debounceTimer?.invalidate() + // Start a new timer to wait before performing the action + debounceTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in + // Perform the action after the delay (debouncing) + onSearchButtonClicked() + } + } .overlay( HStack { Image(systemName: "magnifyingglass")