-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathsplash_controller.dart
More file actions
178 lines (151 loc) · 5.69 KB
/
splash_controller.dart
File metadata and controls
178 lines (151 loc) · 5.69 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ignore_for_file: body_might_complete_normally_catch_error, depend_on_referenced_packages
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:in_app_update/in_app_update.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:taskwarrior/app/models/storage.dart';
import 'package:taskwarrior/app/modules/home/controllers/home_controller.dart';
import 'package:taskwarrior/app/routes/app_pages.dart';
import 'package:taskwarrior/app/utils/taskfunctions/profiles.dart';
import 'package:taskwarrior/app/v3/models/task.dart';
import 'package:taskwarrior/app/services/deep_link_service.dart';
class SplashController extends GetxController {
late Rx<Directory> baseDirectory = Directory('').obs;
late RxMap<String, String?> profilesMap = <String, String?>{}.obs;
late RxString currentProfile = ''.obs;
Profiles get _profiles => Profiles(baseDirectory.value);
@override
void onInit() {
debugPrint("🚀 BOOT: SplashController.onInit()");
super.onInit();
}
@override
void onReady() async {
super.onReady();
await initBaseDir();
_checkProfiles();
profilesMap.value = _profiles.profilesMap();
currentProfile.value = _profiles.getCurrentProfile()!;
final deepLinkService = Get.find<DeepLinkService>();
if (deepLinkService.queuedUri != null) {
debugPrint("🚀 TRACE: Bypassing Splash routing for queued URI");
Get.offNamed(Routes.HOME);
return;
}
await checkForUpdate();
sendToNextPage();
}
Future<void> initBaseDir() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? directory = prefs.getString('baseDirectory');
Directory dir = (directory != null)
? Directory(directory)
: await getDefaultDirectory();
baseDirectory.value = dir;
}
void _checkProfiles() {
if (_profiles.profilesMap().isEmpty) {
_profiles.setCurrentProfile(_profiles.addProfile());
} else if (!_profiles
.profilesMap()
.containsKey(_profiles.getCurrentProfile())) {
_profiles.setCurrentProfile(_profiles.profilesMap().keys.first);
}
}
Future<Directory> getDefaultDirectory() async {
return await getApplicationDocumentsDirectory();
}
void setBaseDirectory(Directory newBaseDirectory) {
baseDirectory.value = newBaseDirectory;
profilesMap.value = _profiles.profilesMap();
Get.find<HomeController>().changeInDirectory();
}
void addProfile() {
_profiles.addProfile();
profilesMap.value = _profiles.profilesMap();
}
Future<void> copyConfigToNewProfile(String profile) async {
await _profiles.copyConfigToNewProfile(profile);
profilesMap.value = _profiles.profilesMap();
}
Future<void> deleteProfile(String profile) async {
await _profiles.deleteProfile(profile);
_checkProfiles();
profilesMap.value = _profiles.profilesMap();
currentProfile.value = _profiles.getCurrentProfile()!;
}
void renameProfile({required String profile, required String? alias}) {
_profiles.setAlias(profile: profile, alias: alias!);
profilesMap.value = _profiles.profilesMap();
}
void selectProfile(String profile) async {
_profiles.setCurrentProfile(profile);
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool('settings_taskc', getMode(profile) == 'TW3');
await prefs.setBool('settings_taskr_repl', getMode(profile) == 'TW3C');
Get.find<HomeController>().taskchampion.value = getMode(profile) == 'TW3';
Get.find<HomeController>().taskReplica.value = getMode(profile) == 'TW3C';
if (getMode(profile) == "TW3C") {
Get.find<HomeController>().refreshReplicaTaskList();
}
Get.find<HomeController>().tasks.value = <TaskForC>[].obs;
currentProfile.value = _profiles.getCurrentProfile()!;
}
String getMode(String profile) {
return _profiles.getMode(profile) ?? 'TW2';
}
void changeModeTo(String profile, String mode) {
_profiles.setModeTo(profile, mode);
selectProfile(currentProfile.value);
profilesMap.value = _profiles.profilesMap();
}
Map<String, String?> getTaskcCreds(String profile) {
return _profiles.getTaskcCreds(profile);
}
void setTaskcCreds(
String profile, String clientId, String clientSecret, String apiUrl) {
_profiles.setTaskcCreds(profile, clientId, clientSecret, apiUrl);
}
Storage getStorage(String profile) {
return _profiles.getStorage(profile);
}
RxBool hasCompletedOnboarding = false.obs;
Future<void> checkOnboardingStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
hasCompletedOnboarding.value =
prefs.getBool('onboarding_completed') ?? false;
}
void sendToNextPage() async {
await checkOnboardingStatus();
if (hasCompletedOnboarding.value) {
Get.offNamed(Routes.HOME);
} else {
Get.offNamed(Routes.ONBOARDING);
}
}
Future<void> checkForUpdate() async {
if (!Platform.isAndroid) return;
try {
AppUpdateInfo updateInfo = await InAppUpdate.checkForUpdate();
if (updateInfo.updateAvailability == UpdateAvailability.updateAvailable) {
if (updateInfo.immediateUpdateAllowed) {
InAppUpdate.performImmediateUpdate().catchError((e) {
debugPrint(e.toString());
});
} else if (updateInfo.flexibleUpdateAllowed) {
InAppUpdate.startFlexibleUpdate().then((_) {
InAppUpdate.completeFlexibleUpdate().catchError((e) {
debugPrint(e.toString());
});
}).catchError((e) {
debugPrint(e.toString());
});
}
}
} catch (e) {
debugPrint(e.toString());
}
}
}