add debounce to SearchBar

This commit is contained in:
DawudOsman 2025-04-16 11:30:47 +01:00
parent 0f85714268
commit 6640a8a4b7

View file

@ -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.25, repeats: false) { _ in
// Perform the action after the delay (debouncing)
onSearchButtonClicked()
}
}
.overlay(
HStack {
Image(systemName: "magnifyingglass")