-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstartup_viewmodel.dart
59 lines (50 loc) · 2.34 KB
/
startup_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
// Package imports:
import 'package:stacked/stacked.dart';
// Project imports:
import 'package:notredame/constants/preferences_flags.dart';
import 'package:notredame/features/app/integration/networking_service.dart';
import 'package:notredame/features/app/navigation/navigation_service.dart';
import 'package:notredame/features/app/navigation/router_paths.dart';
import 'package:notredame/features/app/repository/user_repository.dart';
import 'package:notredame/features/more/settings/settings_manager.dart';
import 'package:notredame/utils/locator.dart';
class StartUpViewModel extends BaseViewModel {
/// Manage the settings
final SettingsManager _settingsManager = locator<SettingsManager>();
/// Used to authenticate the user
final UserRepository _userRepository = locator<UserRepository>();
/// Used to verify if the user has internet connectivity
final NetworkingService _networkingService = locator<NetworkingService>();
/// Used to redirect on the dashboard.
final NavigationService _navigationService = locator<NavigationService>();
/// Try to silent authenticate the user then redirect to [LoginView] or [DashboardView]
Future handleStartUp() async {
if (await handleConnectivityIssues()) return;
final bool isLogin = await _userRepository.silentAuthenticate();
if (isLogin) {
_navigationService.pushNamedAndRemoveUntil(RouterPaths.dashboard);
} else {
if (await _settingsManager.getBool(PreferencesFlag.languageChoice) ==
null) {
_navigationService.pushNamed(RouterPaths.chooseLanguage);
_settingsManager.setBool(PreferencesFlag.languageChoice, true);
} else {
_navigationService.pop();
_navigationService.pushNamed(RouterPaths.login);
}
}
}
/// Verify if user has an active internet connection. If the user does have
/// an active internet connection, proceed with the normal app workflow.
/// Otherwise if the user was previously logged in, let him access the app
/// with the cached data
Future<bool> handleConnectivityIssues() async {
final hasConnectivityIssues = !await _networkingService.hasConnectivity();
final wasLoggedIn = await _userRepository.wasPreviouslyLoggedIn();
if (hasConnectivityIssues && wasLoggedIn) {
_navigationService.pushNamedAndRemoveUntil(RouterPaths.dashboard);
return true;
}
return false;
}
}