-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgrades_viewmodel.dart
130 lines (111 loc) · 3.98 KB
/
grades_viewmodel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Flutter imports:
import 'package:flutter/material.dart';
// Package imports:
import 'package:ets_api_clients/models.dart';
import 'package:feature_discovery/feature_discovery.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:stacked/stacked.dart';
// Project imports:
import 'package:notredame/core/constants/discovery_ids.dart';
import 'package:notredame/core/constants/preferences_flags.dart';
import 'package:notredame/core/managers/course_repository.dart';
import 'package:notredame/core/managers/settings_manager.dart';
import 'package:notredame/locator.dart';
import 'package:notredame/ui/utils/discovery_components.dart';
class GradesViewModel extends FutureViewModel<Map<String, List<Course>>> {
/// Used to get the courses of the student
final CourseRepository _courseRepository = locator<CourseRepository>();
/// Localization class of the application.
final AppIntl _appIntl;
/// Contains all the courses of the student sorted by session
final Map<String, List<Course>> coursesBySession = {};
/// Chronological order of the sessions. The first index is the most recent
/// session.
final List<String> sessionOrder = [];
GradesViewModel({required AppIntl intl}) : _appIntl = intl;
@override
Future<Map<String, List<Course>>> futureToRun() async {
try {
final coursesCached =
await _courseRepository.getCourses(fromCacheOnly: true);
setBusy(true);
_buildCoursesBySession(coursesCached);
await _courseRepository.getCourses();
if (_courseRepository.courses != null) {
// Update the courses list
_buildCoursesBySession(_courseRepository.courses!);
}
} catch (error) {
onError(error);
} finally {
setBusy(false);
}
return coursesBySession;
}
@override
// ignore: type_annotate_public_apis
void onError(error) {
Fluttertoast.showToast(msg: _appIntl.error);
}
/// Reload the courses from Signets and rebuild the view.
Future refresh() async {
try {
await _courseRepository.getCourses();
if (_courseRepository.courses != null) {
_buildCoursesBySession(_courseRepository.courses!);
}
notifyListeners();
} on Exception catch (error) {
onError(error);
}
}
/// Sort [courses] by session.
void _buildCoursesBySession(List<Course> courses) {
for (final Course course in courses) {
coursesBySession.update(course.session, (value) {
// Remove the current version of the course
value.removeWhere((element) => element.acronym == course.acronym);
// Add the updated version of the course
value.add(course);
value.sort((a, b) => a.acronym.compareTo(b.acronym));
return value;
}, ifAbsent: () {
sessionOrder.add(course.session);
return [course];
});
}
sessionOrder.sort((a, b) {
if (a == b) return 0;
// When the session is 's.o.' we put the course at the end of the list
if (a == "s.o.") {
return 1;
} else if (b == "s.o.") {
return -1;
}
final yearA = int.parse(a.substring(1));
final yearB = int.parse(b.substring(1));
if (yearA < yearB) {
return 1;
} else if (yearA == yearB) {
if (a[0] == 'H' || a[0] == 'É' && b[0] == 'A') {
return 1;
}
}
return -1;
});
}
static Future<void> startDiscovery(BuildContext context) async {
final SettingsManager settingsManager = locator<SettingsManager>();
if (await settingsManager.getBool(PreferencesFlag.discoveryStudentGrade) ==
null) {
final List<String> ids =
findDiscoveriesByGroupName(context, DiscoveryGroupIds.pageStudent)
.map((e) => e.featureId)
.toList();
Future.delayed(const Duration(seconds: 1),
() => FeatureDiscovery.discoverFeatures(context, ids));
settingsManager.setBool(PreferencesFlag.discoveryStudentGrade, true);
}
}
}