Skip to content

Commit

Permalink
Fix endless loading in TimetableSearchPage
Browse files Browse the repository at this point in the history
  • Loading branch information
StarProxima committed Apr 8, 2023
1 parent 9924c4d commit e3080f2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:cube_system/features/timetable_search_page/state_holders/timetable_search_page_event.dart';

import 'package:cube_system/features/timetable_search_page/state_holders/timetable_search_page_querry_in_progress.dart';

import 'package:cube_system/features/timetable_search_page/state_holders/timetable_search_page_timer.dart';

final timetableSearchPageManager = Provider<TimetableSearchPageManager>((ref) {
Expand All @@ -26,7 +24,6 @@ final timetableSearchPageManager = Provider<TimetableSearchPageManager>((ref) {
searchContoller: ref.watch(timetableSearchPageSearchController.notifier),
searchFocus: ref.watch(timetableSearchPageSearchFocus.notifier),
event: ref.watch(timetableSearchPageEventType.notifier),
querryInProgress: ref.watch(timetableSearchPageQuerryInProgress.notifier),
timer: ref.watch(timetableSearchPageTimer.notifier),
);
});
Expand All @@ -39,7 +36,6 @@ class TimetableSearchPageManager {
final StateController<TextEditingController> searchContoller;
final StateController<FocusNode> searchFocus;
final StateController<TimetableSearchEventType> event;
final TimetableSearchPageQuerryInProgressNotifier querryInProgress;
final StateController<Timer> timer;

TimetableSearchPageManager({
Expand All @@ -49,7 +45,6 @@ class TimetableSearchPageManager {
required this.searchContoller,
required this.searchFocus,
required this.event,
required this.querryInProgress,
required this.timer,
});

Expand Down Expand Up @@ -98,12 +93,10 @@ class TimetableSearchPageManager {

Future<void> _search(String querry) async {
await Future(() {});
event.state = TimetableSearchEventType.loading;

querryInProgress.add();
event.state = TimetableSearchEventType.loading;

if (querry.strip().isEmpty) {
querryInProgress.sub();
event.state = TimetableSearchEventType.welcome;
return;
}
Expand All @@ -112,14 +105,15 @@ class TimetableSearchPageManager {

try {
response = await api.apiLessonsAutocompleteGet(q: querry);
querryInProgress.sub();
} catch (e) {
querryInProgress.sub();
event.state = TimetableSearchEventType.error;
return;
}

if (querryInProgress.state >= 1) return;
// If the text is different, then another request is coming soon
if (searchContoller.state.text != querry) {
return;
}

final res = response.body!;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';

final timetableSearchPageQuerryInProgress =
StateNotifierProvider<TimetableSearchPageQuerryInProgressNotifier, int>(
(ref) {
return TimetableSearchPageQuerryInProgressNotifier(
0,
);
});

class TimetableSearchPageQuerryInProgressNotifier extends StateNotifier<int> {
TimetableSearchPageQuerryInProgressNotifier(super.state);

@override
get state => super.state;

void add() => state = state + 1;

void sub() => state = state - 1;
}

0 comments on commit e3080f2

Please sign in to comment.