zodiac_love_intimacy_controller.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. int tabIndex = ZodiacLoveIntimacyTab.today.tabIndex;
  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. tabIndex = index;
  82. AtmobLog.i(_tag, "index: $tabIndex");
  83. }
  84. }
  85. // 创建PageController和TabController,并设置当前选中的index为初始索引
  86. pageController = PageController(initialPage: tabIndex);
  87. tabController = TabController(
  88. initialIndex: tabIndex,
  89. length: tabBarList.length,
  90. vsync: this,
  91. );
  92. currentTabIndex.value = tabIndex;
  93. }
  94. /// 返回
  95. void clickBack() {
  96. Get.back();
  97. }
  98. @override
  99. void onClose() {
  100. tabController.dispose();
  101. pageController.dispose();
  102. super.onClose();
  103. }
  104. /// 处理Tab切换,联动PageView
  105. void handleTabChange(int index) {
  106. pageController.animateToPage(
  107. index,
  108. duration: const Duration(milliseconds: 300),
  109. curve: Curves.easeInToLinear,
  110. );
  111. }
  112. /// 处理PageView切换,联动Tab
  113. void handlePageChange(int index) {
  114. tabController.animateTo(index, duration: const Duration(milliseconds: 300));
  115. currentTabIndex.value = index;
  116. }
  117. /// 获取星座恋爱分析-基本信息
  118. void _getZodiacLoveIntimacyLoveInfo() async {
  119. try {
  120. ZodiacLoveIntimacyLoveInfoResponse resp =
  121. await zodiacLoveIntimacyRepository.getZodiacLoveIntimacyLoveInfo();
  122. zodiacLoveIntimacyLoveInfoResponse.value = resp;
  123. } catch (error) {
  124. if (error is ServerErrorException) {
  125. // 未登录
  126. if (error.code == ErrorCode.noLoginError) {
  127. ToastUtil.show(StringName.accountNoLogin);
  128. LoginDialog.show();
  129. } else if (error.code == ErrorCode.noMember) {
  130. // 需要VIP
  131. ToastUtil.show(StringName.needVipTip);
  132. NewDiscountPage.start();
  133. } else if (error.code == ErrorCode.noSetBirthday) {
  134. // 未设置生日
  135. ErrorHandler.toastError(error);
  136. await UserProfilePage.start();
  137. } else {
  138. ErrorHandler.toastError(error);
  139. }
  140. } else {
  141. ErrorHandler.toastError(error);
  142. }
  143. }
  144. }
  145. /// 获取用户的星座信息
  146. Zodiac? getUserZodiacInfo(String? birthday, int? gender) {
  147. if (birthday?.isEmpty != false || gender == null) {
  148. return null;
  149. }
  150. final zodiac = AgeZodiacSignUtil.getZodiacWithGenderLabel(
  151. birthday!,
  152. gender,
  153. );
  154. if (zodiac.name == '未知星座') {
  155. return null;
  156. }
  157. return zodiac;
  158. }
  159. }