-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More pagination tweaks: debouncing, error widget
- Loading branch information
Showing
6 changed files
with
128 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 1 addition & 17 deletions
18
lib/src/features/movies/presentation/movies/movies_search_bar.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lib/src/features/movies/presentation/movies/movies_search_query_notifier.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'dart:async'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'movies_search_query_notifier.g.dart'; | ||
|
||
/// A notifier class to keep track of the search query (with debouncing) | ||
@riverpod | ||
class MoviesSearchQueryNotifier extends _$MoviesSearchQueryNotifier { | ||
/// Used to debounce the input queries | ||
final _searchQueryController = StreamController<String>.broadcast(); | ||
Timer? _debounceTimer; | ||
late final StreamSubscription<String> _subscription; | ||
|
||
@override | ||
String build() { | ||
// Listen to the stream of input queries | ||
_subscription = _searchQueryController.stream.listen((query) { | ||
// Cancel existing timer if there is one | ||
_debounceTimer?.cancel(); | ||
// Set a new timer to debounce the query | ||
_debounceTimer = Timer(const Duration(milliseconds: 500), () { | ||
_updateState(query); | ||
}); | ||
}); | ||
|
||
// don't forget to close the StreamController and cancel the subscriptions on dispose | ||
ref.onDispose(() { | ||
_searchQueryController.close(); | ||
_subscription.cancel(); | ||
_debounceTimer?.cancel(); | ||
}); | ||
|
||
// by default, return an empty query | ||
return ''; | ||
} | ||
|
||
void _updateState(String query) { | ||
// only update the state once the query has been debounced | ||
state = query; | ||
} | ||
|
||
void setQuery(String query) { | ||
_searchQueryController.sink.add(query); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...sentation/movies/movies_search_bar.g.dart → ...ovies/movies_search_query_notifier.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters