watch_theme_controller.dart
3.97 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
import 'dart:async';
import 'package:doublefeel_flutter/app/routes/app_pages.dart';
import 'package:doublefeel_flutter/core/constants/intent_keys.dart';
import 'package:doublefeel_flutter/core/network/api/theme_api.dart';
import 'package:doublefeel_flutter/core/network/api/vip_api.dart';
import 'package:doublefeel_flutter/core/result/app_result.dart';
import 'package:doublefeel_flutter/data/local/user_preferences_storage.dart';
import 'package:doublefeel_flutter/data/models/local/user_preferences.dart';
import 'package:doublefeel_flutter/l10n/l10n_extensions.dart';
import 'package:get/get.dart';
import '../models/watch_theme_models.dart';
class WatchThemeController extends GetxController {
WatchThemeController(this._themeApi, this._vipApi);
static const int _developerOptionsTapCount = 8;
static const Duration _developerOptionsTapWindow = Duration(seconds: 3);
final ThemeApi _themeApi;
final VipApi _vipApi;
final isPremium = false.obs;
final isLoadingThemes = false.obs;
final officialThemeItems = <WatchThemeItem>[].obs;
final customThemeItems = <WatchThemeItem>[].obs;
final selectedThemeItem = Rx<WatchThemeItem?>(null);
int _headerTapCount = 0;
DateTime? _firstHeaderTapAt;
@override
void onInit() {
super.onInit();
unawaited(loadThemeList());
unawaited(_refreshVip());
}
Future<void> loadThemeList() async {
isLoadingThemes.value = true;
final themeListFuture = _themeApi.getThemeList(errorHandlingPolicy: null);
final activeThemeFuture =
_themeApi.getCurrentTheme(errorHandlingPolicy: null);
final result = await themeListFuture;
final activeResult = await activeThemeFuture;
isLoadingThemes.value = false;
if (activeResult case AppSuccess<WatchThemeItem>(data: final theme)) {
selectedThemeItem.value = theme;
} else {
selectedThemeItem.value = null;
}
if (result is! AppSuccess<WatchThemeResponse>) {
return;
}
final themes = result.data.themes;
final official = themes.where((theme) => theme.isOfficialTheme).toList();
final custom = themes.where((theme) => theme.isCustomTheme).toList();
officialThemeItems.assignAll(official);
customThemeItems.assignAll(custom);
isPremium.value = isPremium.value;
}
void executeBackLogic() {
Get.back();
}
void onHeaderTap() {
final now = DateTime.now();
final firstTapAt = _firstHeaderTapAt;
if (firstTapAt == null ||
now.difference(firstTapAt) > _developerOptionsTapWindow) {
_firstHeaderTapAt = now;
_headerTapCount = 1;
return;
}
_headerTapCount += 1;
if (_headerTapCount < _developerOptionsTapCount) {
return;
}
_firstHeaderTapAt = null;
_headerTapCount = 0;
Get.toNamed(Routes.DEVELOPER_OPTIONS);
}
Future<void> selectOfficialTheme(int index) async {
await Get.toNamed(Routes.WATCH_THEME_PREVIEW, arguments: {
'theme': officialThemeItems[index],
});
await loadThemeList();
}
Future<void> createCustomTheme() async {
if (isPremium.value) {
await Get.toNamed(Routes.WATCH_THEME_CREATE);
await loadThemeList();
} else {
// 进入会员页
_toPremiumPage();
}
}
_refreshVip() async {
final vipResult = await _vipApi.getVipInfo();
if (vipResult case AppSuccess(data: final vip)) {
try {
final vipPrefs = UserPreferencesVipInfo.fromVipInfo(vip);
await Get.find<UserPreferencesStorage>().updateVipInfo(vipPrefs);
isPremium.value = vipPrefs.isVip;
} on Exception catch (_) {
// Keep the current membership state when local persistence fails.
}
}
}
Future<void> _toPremiumPage() async {
await Get.toNamed(Routes.PURCHASE, arguments: {
IntentKeys.channelType: l10n.watchThemePurchaseChannel,
});
await _refreshVip();
}
Future<void> previewCustomTheme(WatchThemeItem theme) async {
await Get.toNamed(Routes.WATCH_THEME_CUSTOM_PREVIEW, arguments: {
'theme': theme,
});
await loadThemeList();
}
}