zodiac_love_intimacy_controller.dart 5.5 KB

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