user_onboarding_controller.dart
3.76 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
import 'package:doublefeel_flutter/core/services/user_state_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../../core/services/health_kit_upload_service.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;
bool hasSelection(int pageIndex) =>
selections[pageIndex]?.isNotEmpty ?? false;
bool isSelected(int pageIndex, int optionIndex) =>
selections[pageIndex]?.contains(optionIndex) ?? false;
bool canContinue(int pageIndex) {
if (Get.arguments?['type'] == 'MembershipOfferPage') {
return true;
}
return switch (pageIndex) {
1 || 2 || 3 => hasSelection(pageIndex),
_ => true,
};
}
@override
void onInit() {
totalPages = Get.arguments?['type'] == 'MembershipOfferPage'
? (Get.arguments?['needBindSuccessGuide'] == true ? 2 : 1)
: 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();
}
void goBack() {
if (currentPageIndex.value > 0) {
currentPageIndex.value--;
return;
}
if (Get.key.currentState?.canPop() ?? false) {
Get.back();
} else {
SystemNavigator.pop();
}
}
Future<void> goNext() async {
final pageIndex = currentPageIndex.value;
if (!await prepareContinue(pageIndex)) {
return;
}
if (pageIndex >= totalPages - 1) {
finishOnboarding();
return;
}
currentPageIndex.value++;
}
Future<bool> prepareContinue(int pageIndex) async {
if (!canContinue(pageIndex)) {
return false;
}
if (pageIndex == 7) {
await requestHealthAuthorization();
} else if (pageIndex == 8) {
await requestNotificationAuthorization();
}
return true;
}
void finishOnboarding() {
if (Get.arguments?['type'] == 'MembershipOfferPage') {
Get.offAllNamed(AppRoutes.home);
} else {
final userStateService = Get.find<UserStateService>();
if (userStateService.isBound) {
Get.offAllNamed(AppRoutes.home);
} else {
Get.offAllNamed(AppRoutes.bindPartner);
}
}
}
Future<void> requestHealthAuthorization() async {
if (isAndroid) {
return;
}
try {
await Get.find<HealthKitUploadService>().requestClientAuthorization();
} catch (error) {
debugPrint('Health authorization skipped: $error');
}
}
Future<void> requestNotificationAuthorization() async {
try {
final result = await Permission.notification.request();
if (result.isGranted) {
debugPrint('Notification authorization granted');
} else {
debugPrint('Notification authorization skipped');
}
} catch (error) {
debugPrint('Notification authorization skipped: $error');
}
}
}