zodiac_love_intimacy_controller.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import 'package:flutter/Material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/base/base_controller.dart';
  5. import 'package:keyboard/module/login/login_page.dart';
  6. import 'package:keyboard/widget/platform_util.dart';
  7. import '../../data/api/response/user_info_response.dart';
  8. import '../../data/api/response/zodiac_love_intimacy_love_info_response.dart';
  9. import '../../data/consts/error_code.dart';
  10. import '../../data/consts/event_report.dart';
  11. import '../../data/repository/account_repository.dart';
  12. import '../../data/repository/zodiac_love_intimacy_repository.dart';
  13. import '../../dialog/login/login_dialog.dart';
  14. import '../../handler/event_handler.dart';
  15. import '../../resource/string.gen.dart';
  16. import '../../router/app_page_arguments.dart';
  17. import '../../utils/age_zodiac_sign_util.dart';
  18. import '../../utils/atmob_log.dart';
  19. import '../../utils/error_handler.dart';
  20. import '../../utils/http_handler.dart';
  21. import '../../utils/toast_util.dart';
  22. import '../store/new_discount/new_discount_page.dart';
  23. import '../user_profile/user_profile_page.dart';
  24. import 'enums/zodiac_love_intimacy_tab.dart';
  25. /// 星座恋爱分析Controller
  26. @injectable
  27. class ZodiacLoveIntimacyController extends BaseController
  28. with GetTickerProviderStateMixin {
  29. final String _tag = "ZodiacLoveIntimacyController";
  30. /// 账户信息Repository
  31. final AccountRepository accountRepository;
  32. /// 星座恋爱分析Repository
  33. final ZodiacLoveIntimacyRepository zodiacLoveIntimacyRepository;
  34. /// Tab列表
  35. RxList<ZodiacLoveIntimacyTab> tabBarList = ZodiacLoveIntimacyTab.values.obs;
  36. /// Tab控制器
  37. late final TabController tabController;
  38. /// PageView控制器
  39. late final PageController pageController;
  40. /// PageView是否允许滑动切换
  41. RxBool isPageViewSwipeEnabled = true.obs;
  42. /// 当前Tab的索引
  43. Rx<int> currentTabIndex = 0.obs;
  44. /// 当前用户信息
  45. Rxn<UserInfoResponse> get userInfo => accountRepository.userInfo;
  46. /// 星座恋爱分析-基础信息
  47. Rxn<ZodiacLoveIntimacyLoveInfoResponse> zodiacLoveIntimacyLoveInfoResponse =
  48. Rxn<ZodiacLoveIntimacyLoveInfoResponse>();
  49. /// 我的星座信息
  50. Zodiac? get myZodiacInfo {
  51. String? birthday =
  52. zodiacLoveIntimacyLoveInfoResponse.value?.birthday ??=
  53. userInfo.value?.birthday;
  54. int? gender =
  55. zodiacLoveIntimacyLoveInfoResponse.value?.gender ??=
  56. userInfo.value?.gender;
  57. return getUserZodiacInfo(birthday, gender);
  58. }
  59. /// Ta的星座信息
  60. Zodiac? get taZodiacInfo {
  61. final birthday = zodiacLoveIntimacyLoveInfoResponse.value?.targetBirthday;
  62. final gender = zodiacLoveIntimacyLoveInfoResponse.value?.targetGender;
  63. return getUserZodiacInfo(birthday, gender);
  64. }
  65. ZodiacLoveIntimacyController(
  66. this.accountRepository,
  67. this.zodiacLoveIntimacyRepository,
  68. );
  69. @override
  70. void onInit() {
  71. super.onInit();
  72. _initArgs();
  73. _getZodiacLoveIntimacyLoveInfo();
  74. }
  75. /// 初始化参数
  76. void _initArgs() {
  77. final arguments = Get.arguments as Map<String, dynamic>?;
  78. // 当前索引
  79. ZodiacLoveIntimacyTab? tab;
  80. if (arguments?[AppPageArguments.index] == null) {
  81. AtmobLog.i(_tag, '没有传递 index 参数');
  82. } else {
  83. final int? index = arguments?[AppPageArguments.index] as int?;
  84. if (index != null) {
  85. tab = ZodiacLoveIntimacyTab.fromIndex(index);
  86. if (tab != null) {
  87. AtmobLog.i(_tag, "index: ${tab.tabIndex}");
  88. }
  89. }
  90. }
  91. // 默认选中第一个Tab
  92. tab ??= ZodiacLoveIntimacyTab.today;
  93. // 创建PageController和TabController,并设置当前选中的index为初始索引
  94. pageController = PageController(initialPage: tab.tabIndex);
  95. tabController = TabController(
  96. initialIndex: tab.tabIndex,
  97. length: tabBarList.length,
  98. vsync: this,
  99. );
  100. currentTabIndex.value = tab.tabIndex;
  101. if (tab.tabIndex == ZodiacLoveIntimacyTab.today.tabIndex) {
  102. EventHandler.report(EventId.event_08001);
  103. } else if (tab.tabIndex == ZodiacLoveIntimacyTab.futureWeek.tabIndex) {
  104. EventHandler.report(EventId.event_08002);
  105. }
  106. currentTabIndex.listen((index) {
  107. if(index==ZodiacLoveIntimacyTab.today.tabIndex) {
  108. EventHandler.report(EventId.event_08001);
  109. }
  110. else if(index==ZodiacLoveIntimacyTab.futureWeek.tabIndex) {
  111. EventHandler.report(EventId.event_08002);
  112. }
  113. });
  114. }
  115. /// 返回
  116. void clickBack() {
  117. Get.back();
  118. }
  119. @override
  120. void onClose() {
  121. tabController.dispose();
  122. pageController.dispose();
  123. super.onClose();
  124. }
  125. /// 处理Tab切换,联动PageView
  126. void handleTabChange(int index) {
  127. pageController.animateToPage(
  128. index,
  129. duration: const Duration(milliseconds: 300),
  130. curve: Curves.fastOutSlowIn,
  131. );
  132. }
  133. /// 处理PageView切换,联动Tab
  134. void handlePageChange(int index) {
  135. tabController.animateTo(index, duration: const Duration(milliseconds: 300));
  136. currentTabIndex.value = index;
  137. }
  138. /// 获取星座恋爱分析-基本信息
  139. void _getZodiacLoveIntimacyLoveInfo() async {
  140. try {
  141. ZodiacLoveIntimacyLoveInfoResponse resp =
  142. await zodiacLoveIntimacyRepository.getZodiacLoveIntimacyLoveInfo();
  143. zodiacLoveIntimacyLoveInfoResponse.value = resp;
  144. } catch (error) {
  145. if (error is ServerErrorException) {
  146. // 未登录
  147. if (error.code == ErrorCode.noLoginError) {
  148. ToastUtil.show(StringName.accountNoLogin);
  149. LoginDialog.show();
  150. } else if (error.code == ErrorCode.noMember) {
  151. // 需要VIP
  152. ToastUtil.show(StringName.needVipTip);
  153. NewDiscountPage.start();
  154. } else if (error.code == ErrorCode.noSetBirthday) {
  155. // 未设置生日
  156. ErrorHandler.toastError(error);
  157. await UserProfilePage.start();
  158. } else {
  159. ErrorHandler.toastError(error);
  160. }
  161. } else {
  162. ErrorHandler.toastError(error);
  163. }
  164. }
  165. }
  166. /// 获取用户的星座信息
  167. Zodiac? getUserZodiacInfo(String? birthday, int? gender) {
  168. if (birthday?.isEmpty != false || gender == null) {
  169. return null;
  170. }
  171. final zodiac = AgeZodiacSignUtil.getZodiacWithGenderLabel(
  172. birthday!,
  173. gender,
  174. );
  175. if (zodiac.name == '未知星座') {
  176. return null;
  177. }
  178. return zodiac;
  179. }
  180. }