Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce #24

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions OTPKit/Services/LocationServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@ public final class LocationService: NSObject, ObservableObject, MKLocalSearchCom

@Published var completions = [Location]()

init(completer: MKLocalSearchCompleter = MKLocalSearchCompleter()) {
init(completer: MKLocalSearchCompleter = MKLocalSearchCompleter(), debounceInterval: TimeInterval = 0.3) {
self.completer = completer
self.debounceInterval = debounceInterval
super.init()
self.completer.delegate = self
}

/// update method responsible for updating the queryFragement of `completer`
/// - Parameter queryFragment: this manage the searched query for `completer`
deinit {
debounceTimer?.invalidate()
}

private let debounceInterval: TimeInterval

private var debounceTimer: Timer?

/// Initiates a local search for `queryFragment`.
/// This will be debounced, as set by the `debounceInterval` on the initializer.
/// - Parameter queryFragment: The search term
public func update(queryFragment: String) {
completer.resultTypes = .query
completer.queryFragment = queryFragment
debounceTimer?.invalidate()
debounceTimer = Timer.scheduledTimer(withTimeInterval: debounceInterval, repeats: false) { [weak self] _ in
guard let self else { return }
completer.resultTypes = .query
completer.queryFragment = queryFragment
}
}

/// completerDidUpdateResults is method that finished the search functionality and update the `completer`.
Expand Down
1 change: 1 addition & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ targets:
path: OTPKitDemo/Info.plist
properties:
CFBundleShortVersionString: "$(MARKETING_VERSION)"
NSLocationWhenInUseUsageDescription: See where you are in relation to transit, and help you navigate more easily.
type: application
platform: iOS
settings:
Expand Down
Loading