zodiac_love_intimacy_controller.dart 6.1 KB

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