watch_theme_controller.dart
2.35 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
import 'package:doublefeel_flutter/app/routes/app_pages.dart';
import 'package:doublefeel_flutter/core/network/api/theme_api.dart';
import 'package:doublefeel_flutter/core/result/app_result.dart';
import 'package:get/get.dart';
import '../models/watch_theme_models.dart';
class WatchThemeController extends GetxController {
WatchThemeController(this._themeApi);
final ThemeApi _themeApi;
final selectedOfficialIndex = 0.obs;
final hasCustomThemes = false.obs;
final isPremium = false.obs;
final isLoadingThemes = false.obs;
final officialThemeItems = <WatchThemeItem>[...officialThemes].obs;
final customThemeItems = <WatchThemeItem>[...customThemes].obs;
@override
void onInit() {
super.onInit();
final args = Get.arguments;
if (args is Map) {
hasCustomThemes.value = args['hasCustomThemes'] == true;
isPremium.value = args['isPremium'] == true || hasCustomThemes.value;
}
loadThemeList();
}
Future<void> loadThemeList() async {
isLoadingThemes.value = true;
final result = await _themeApi.getThemeList(errorHandlingPolicy: null);
isLoadingThemes.value = false;
if (result is! AppSuccess<WatchThemeResponse>) {
hasCustomThemes.value = customThemeItems.isNotEmpty;
isPremium.value = isPremium.value || hasCustomThemes.value;
return;
}
final themes = result.data.themes;
if (themes.isEmpty) {
hasCustomThemes.value = customThemeItems.isNotEmpty;
isPremium.value = isPremium.value || hasCustomThemes.value;
return;
}
final official = themes.where((theme) => !theme.isCustomTheme).toList();
final custom = themes.where((theme) => theme.isCustomTheme).toList();
if (official.isNotEmpty) {
officialThemeItems.assignAll(official);
}
customThemeItems.assignAll(custom);
hasCustomThemes.value = custom.isNotEmpty;
isPremium.value = isPremium.value || hasCustomThemes.value;
}
void executeBackLogic() {
Get.back();
}
void selectOfficialTheme(int index) {
selectedOfficialIndex.value = index;
Get.toNamed(Routes.WATCH_THEME_PREVIEW, arguments: {
'theme': officialThemeItems[index],
});
}
void createCustomTheme() {
Get.toNamed(Routes.WATCH_THEME_CREATE);
}
void previewCustomTheme(WatchThemeItem theme) {
Get.toNamed(Routes.WATCH_THEME_CUSTOM_PREVIEW, arguments: {
'theme': theme,
});
}
}