-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprofile_viewmodel.dart
130 lines (107 loc) · 3.82 KB
/
profile_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
// Package imports:
import 'package:ets_api_clients/models.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/programs_credits.dart';
import 'package:notredame/core/managers/user_repository.dart';
import 'package:notredame/core/services/analytics_service.dart';
import 'package:notredame/locator.dart';
class ProfileViewModel extends FutureViewModel<List<Program>> {
/// Load the user
final UserRepository _userRepository = locator<UserRepository>();
final AnalyticsService analyticsService = locator<AnalyticsService>();
/// Localization class of the application.
final AppIntl _appIntl;
/// List of the programs
List<Program> _programList = List.empty();
/// Student's profile
final ProfileStudent _student = ProfileStudent(
balance: "", firstName: "", lastName: "", permanentCode: "");
/// Return the profileStudent
ProfileStudent get profileStudent {
return _userRepository.info ?? _student;
}
/// Return the universal access code of the student
String get universalAccessCode =>
_userRepository.monETSUser?.universalCode ?? '';
ProfileViewModel({required AppIntl intl}) : _appIntl = intl;
double get programProgression {
final ProgramCredits programCredits = ProgramCredits();
int percentage = 0;
if (programList.isNotEmpty) {
Program currentProgram = programList.first;
for (final program in programList) {
if (int.parse(program.registeredCredits) >
int.parse(currentProgram.registeredCredits)) {
currentProgram = program;
}
}
final int numberOfCreditsCompleted =
int.parse(currentProgram.accumulatedCredits);
final String code = currentProgram.code;
bool foundMatch = false;
programCredits.programsCredits.forEach((key, value) {
if (key == code || currentProgram.name.startsWith(key)) {
percentage = (numberOfCreditsCompleted / value * 100).round();
foundMatch = true;
}
});
if (!foundMatch) {
final String programName = currentProgram.name;
analyticsService.logEvent("profile_view",
'The program $programName (code: $code) does not match any program');
percentage = 0;
}
}
return percentage.toDouble();
}
@override
// ignore: type_annotate_public_apis
void onError(error) {
Fluttertoast.showToast(msg: _appIntl.error);
}
/// Return the list of programs for the student
List<Program> get programList {
if (_programList.isEmpty) {
_programList = [];
}
if (_userRepository.programs != null) {
_programList = _userRepository.programs!;
}
_programList.sort((a, b) => b.status.compareTo(a.status));
_programList
.sort((a, b) => a.registeredCredits.compareTo(b.registeredCredits));
return _programList;
}
bool isLoadingEvents = false;
@override
Future<List<Program>> futureToRun() async {
try {
await _userRepository.getInfo(fromCacheOnly: true);
await _userRepository.getPrograms(fromCacheOnly: true);
setBusyForObject(isLoadingEvents, true);
await _userRepository.getInfo();
return await _userRepository.getPrograms();
} catch (error) {
onError(error);
} finally {
setBusyForObject(isLoadingEvents, false);
}
return _userRepository.programs ?? [];
}
Future refresh() async {
try {
setBusyForObject(isLoadingEvents, true);
_userRepository
.getInfo()
.then((value) => _userRepository.getPrograms().then((value) {
setBusyForObject(isLoadingEvents, false);
notifyListeners();
}));
} on Exception catch (error) {
onError(error);
}
}
}