add debounce to SearchBar (#81)

This commit is contained in:
D Osman 2025-04-17 15:58:57 +01:00 committed by GitHub
parent c582d45fb5
commit ad07457cf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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