user_onboarding_controller.dart
8.25 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import 'package:doublefeel_flutter/core/logging/app_logger.dart';
import 'package:doublefeel_flutter/core/network/api/user_api.dart';
import 'package:doublefeel_flutter/core/services/raw_data_service/health_raw_data_core_service.dart';
import 'package:doublefeel_flutter/core/services/thinking_data_service.dart';
import 'package:doublefeel_flutter/core/services/user_state_service.dart';
import 'package:doublefeel_flutter/data/local/user_account_storage.dart';
import 'package:doublefeel_flutter/data/local/user_preferences_storage.dart';
import 'package:doublefeel_flutter/pigeon/health_kit_api.g.dart';
import 'package:doublefeel_flutter/pigeon/platform_api.g.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../../core/util/platform.dart';
import '../../../routes/app_pages.dart';
class UserOnboardingController extends GetxController {
int totalPages = 9;
final currentPageIndex = 0.obs;
final selections = <int, Set<int>>{}.obs;
final selectionVersion = 0.obs;
UserOnboardingController(this._userApi);
final UserApi _userApi;
final HealthKitHostApi _hostApi = HealthKitHostApi();
final PlatformHostApi _platformHostApi = PlatformHostApi();
bool hasSelection(int pageIndex) =>
selections[pageIndex]?.isNotEmpty ?? false;
bool isSelected(int pageIndex, int optionIndex) =>
selections[pageIndex]?.contains(optionIndex) ?? false;
bool canContinue(int pageIndex) {
return switch (pageIndex) {
1 || 2 || 3 => hasSelection(pageIndex),
_ => true,
};
}
@override
void onInit() {
final argument = Get.arguments?['resumeStage'];
if (argument != null && argument is int) {
currentPageIndex.value = argument.clamp(0, totalPages - 1);
}
totalPages = 9;
super.onInit();
}
void toggleOption({
required int pageIndex,
required int optionIndex,
required bool multiple,
bool exclusive = false,
Set<int> exclusiveOptionIndexes = const {},
}) {
final next = Set<int>.of(selections[pageIndex] ?? const <int>{});
if (multiple) {
if (next.contains(optionIndex)) {
next.remove(optionIndex);
} else if (exclusive) {
next
..clear()
..add(optionIndex);
} else {
next.removeAll(exclusiveOptionIndexes);
next.add(optionIndex);
}
} else {
next
..clear()
..add(optionIndex);
}
if (next.isEmpty) {
selections.remove(pageIndex);
} else {
selections[pageIndex] = next;
}
selectionVersion.value++;
selections.refresh();
}
Future<void> goBack() async {
if (currentPageIndex.value > 0) {
currentPageIndex.value--;
return;
}
if (Get.key.currentState?.canPop() ?? false) {
Get.back();
} else {
// SystemNavigator.pop();
await _userApi.updateOwnerUserInfo(isPassNoviceGuide: 1);
final userId =
Get.find<UserPreferencesStorage>().preferences.value.meUserInfo?.id ??
0;
if (userId > 0) {
Get.find<UserAccountStorage>().markOnboardingCompleted(userId);
}
Get.offAllNamed(AppRoutes.home);
}
}
Future<void> goNext() async {
final pageIndex = currentPageIndex.value;
if (!await prepareContinue(pageIndex)) {
return;
}
// 保存当前页进度(中途退出后可断点续做)
final userId =
Get.find<UserPreferencesStorage>().preferences.value.meUserInfo?.id ??
0;
if (userId > 0) {
await Get.find<UserAccountStorage>()
.saveOnboardingStage(userId, pageIndex);
}
if (pageIndex >= totalPages - 1) {
finishOnboarding();
return;
}
try {
report();
} on Exception catch (error, stackTrace) {
AppLogger.e('Onboarding report failed', error, stackTrace);
}
currentPageIndex.value++;
if (currentPageIndex.value == 7) {
ta.track('enter_doublefeel_health_grant_page');
} else if (currentPageIndex.value == 8) {
ta.track('enter_doublefeel_notification_grant_page');
}
}
void report() {
const page1Labels = [
'stressAnxiety', // index 0
'tired', // index 1
'poorRest', // index 2
'needStimulants', // index 3
'none', // index 4
];
// page 2: 压力目标(单选)
const page2Labels = [
'findSource', // index 0
'reminder', // index 1
'lovedOnes', // index 2
'relax', // index 3
'bodyTalk', // index 4
];
// page 3: 缓解方式(多选)
const page3Labels = [
'sleep', // index 0
'care', // index 1
'exercise', // index 2
'sun', // index 3
'water', // index 4
'meditation', // index 5
];
late List<String> labels;
switch (currentPageIndex.value) {
case 1:
labels = page1Labels;
break;
case 2:
labels = page2Labels;
break;
case 3:
labels = page3Labels;
break;
}
final selected = (selections[currentPageIndex.value] ?? {})
.map((i) => labels[i])
.toList();
var key = '';
switch (currentPageIndex.value) {
case 1:
key = 'what_happened';
break;
case 2:
key = 'want_messages';
break;
case 3:
key = 'vent_ways';
break;
}
if (selected.isNotEmpty && key.isNotEmpty) {
_userApi.updateUserOnboarding({key: selected});
}
}
Future<bool> prepareContinue(int pageIndex) async {
if (!canContinue(pageIndex)) {
return false;
}
if (pageIndex == 7) {
return await requestHealthAuthorization();
} else if (pageIndex == 8) {
await requestNotificationAuthorization();
}
return true;
}
void finishOnboarding() {
// 标记当前账号引导已全部完成
_userApi.updateOwnerUserInfo(isPassNoviceGuide: 1);
final userId =
Get.find<UserPreferencesStorage>().preferences.value.meUserInfo?.id ??
0;
if (userId > 0) {
Get.find<UserAccountStorage>().markOnboardingCompleted(userId);
}
final userStateService = Get.find<UserStateService>();
if (userStateService.isBound) {
Get.offAllNamed(AppRoutes.home);
} else {
Get.offAllNamed(AppRoutes.bindPartner, arguments: {'from': 'onboarding'});
}
}
/// 计算 Apple Health 新数据。
Future<void> _performDataUpload() async {
try {
final service = Get.find<HealthRawDataCoreService>();
await service.performHealthDataUpload();
await service.startCoreCaculate();
} catch (error, stackTrace) {
AppLogger.e('Apple Health calculate failed', error, stackTrace);
}
}
Future<bool> requestHealthAuthorization() async {
if (isAndroid) {
return true;
} else {
if (GetPlatform.isIOS) {
try {
final result = await _hostApi.checkHealthAppAuthorization();
AppLogger.d("checkHealthAppAuthorization : $result");
if (result.status == 0) {
bool success = await _hostApi.requestHealthClientAuthorization();
AppLogger.d("requestHealthClientAuthorization : $success");
if (success) {
_performDataUpload();
return true;
} else {
// Get.to(NoHealthDataPage(
// onRefresh: _performDataUpload,
// ));
return false;
}
} else {
// status == 1: 已授权;status == 2: 已拒绝
// 两种情况都放行,已授权时额外上传数据
if (result.status == 1) _performDataUpload();
return true;
}
} catch (error, stackTrace) {
AppLogger.e('Health authorization failed', error, stackTrace);
}
}
}
return false;
}
Future<void> requestNotificationAuthorization() async {
try {
final status = await _platformHostApi.getApnsAuthStatus();
if (status == 1) {
// 已授权,无需再请求
return;
}
if (status == -1) {
// 用户永久拒绝(Android),引导去设置
await openAppSettings();
return;
}
// 未决定 → 弹出系统授权弹窗
await _platformHostApi.requestNotificationAuth();
} catch (error) {
AppLogger.e('Notification authorization failed: $error');
}
}
}