Skip to content

Commit

Permalink
debounce search to speed it up
Browse files Browse the repository at this point in the history
- combined with the changes made in a1179f1, this should make search both fast and accurate
  • Loading branch information
Chaphasilor committed Oct 9, 2024
1 parent 2c72061 commit 84c45cd
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/screens/music_screen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:finamp/services/queue_service.dart';
Expand Down Expand Up @@ -203,6 +204,8 @@ class _MusicScreenState extends ConsumerState<MusicScreen>
_buildTabController();
}

Timer? _debounce;

return PopScope(
canPop: !isSearching,
onPopInvoked: (popped) {
Expand All @@ -223,6 +226,16 @@ class _MusicScreenState extends ConsumerState<MusicScreen>
autofocus: true,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.search,
onChanged: (value) {
if (_debounce?.isActive ?? false) _debounce!.cancel();
_debounce =
Timer(const Duration(milliseconds: 400), () {
setState(() {
searchQuery = value;
});
});
},
onSubmitted: (value) => setState(() {
searchQuery = value;
}),
decoration: InputDecoration(
Expand Down

0 comments on commit 84c45cd

Please sign in to comment.