Skip to content

Commit ab26a80

Browse files
authored
Merge pull request #121 from HighlanderRobotics/lookup
debounce team lookup text
2 parents 4313374 + 3c6ebc7 commit ab26a80

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

.vscode/settings.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"diffEditor.codeLens": true,
3-
"editor.tabCompletion": "on",
4-
"discord.enabled": true,
5-
"cmake.configureOnOpen": false,
6-
"java.configuration.updateBuildConfiguration": "automatic"
7-
}
2+
"diffEditor.codeLens": true,
3+
"editor.tabCompletion": "on",
4+
"discord.enabled": true,
5+
"cmake.configureOnOpen": false,
6+
"java.configuration.updateBuildConfiguration": "automatic"
7+
}

lib/pages/team_lookup/team_lookup.dart

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:scouting_dashboard_app/pages/team_lookup/edit_team_lookup_flag.d
88
import 'package:scouting_dashboard_app/pages/team_lookup/tabs/team_lookup_breakdowns.dart';
99
import 'package:scouting_dashboard_app/pages/team_lookup/tabs/team_lookup_categories.dart';
1010
import 'package:scouting_dashboard_app/pages/team_lookup/tabs/team_lookup_notes.dart';
11+
import 'package:scouting_dashboard_app/reusable/debouncer.dart';
1112
import 'package:scouting_dashboard_app/reusable/flag_models.dart';
1213
import 'package:scouting_dashboard_app/reusable/page_body.dart';
1314
import 'package:shared_preferences/shared_preferences.dart';
@@ -22,6 +23,7 @@ class TeamLookupPage extends StatefulWidget {
2223
}
2324

2425
class _TeamLookupPageState extends State<TeamLookupPage> {
26+
final debouncer = Debouncer(delay: const Duration(milliseconds: 200));
2527
String teamFieldValue = "";
2628
TextEditingController? teamFieldController;
2729
int? teamNumberForAnalysis;
@@ -70,12 +72,16 @@ class _TeamLookupPageState extends State<TeamLookupPage> {
7072
),
7173
keyboardType: TextInputType.number,
7274
onChanged: (value) {
73-
setState(() {
74-
teamFieldValue = value;
75-
if (int.tryParse(value) != null) {
76-
teamNumberForAnalysis = int.parse(value);
77-
}
78-
updateIncrement++;
75+
debouncer.run(() {
76+
setState(() {
77+
teamFieldValue = value;
78+
if (int.tryParse(value) != null) {
79+
teamNumberForAnalysis = int.parse(value);
80+
} else {
81+
teamNumberForAnalysis = null;
82+
}
83+
updateIncrement++;
84+
});
7985
});
8086
},
8187
controller: teamFieldController,
@@ -201,15 +207,18 @@ class _TeamLookupFlagState extends State<TeamLookupFlag> {
201207
Widget build(BuildContext context) {
202208
if (loadingTeam != widget.team) load();
203209

204-
return prefs == null || int.tryParse(widget.team) == null
205-
? Container()
206-
: NetworkFlag(
207-
team: int.parse(widget.team),
208-
flag: FlagConfiguration.fromJson(
209-
jsonDecode(
210-
prefs!.getString('team_lookup_flag')!,
211-
),
212-
),
213-
);
210+
if (prefs == null ||
211+
int.tryParse(widget.team) == null ||
212+
prefs!.getString('team_lookup_flag') == null) {
213+
return Container();
214+
}
215+
return NetworkFlag(
216+
team: int.parse(widget.team),
217+
flag: FlagConfiguration.fromJson(
218+
jsonDecode(
219+
prefs!.getString('team_lookup_flag')!,
220+
),
221+
),
222+
);
214223
}
215224
}

lib/reusable/debouncer.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/foundation.dart';
4+
5+
class Debouncer {
6+
final Duration delay;
7+
Timer? _timer;
8+
9+
Debouncer({required this.delay});
10+
11+
void run(VoidCallback action) {
12+
_timer?.cancel();
13+
_timer = Timer(delay, action);
14+
}
15+
16+
void dispose() {
17+
_timer?.cancel();
18+
_timer = null;
19+
}
20+
}

0 commit comments

Comments
 (0)