goods_surprise_controller.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:get/get.dart';
  6. import 'package:keyboard/module/store/suprise/surprise_dialog.dart';
  7. import '../../../base/base_controller.dart';
  8. import '../../../data/bean/goods_info.dart';
  9. import '../../../data/consts/constants.dart';
  10. import '../../../data/repository/store_repository.dart';
  11. import '../../../utils/async_util.dart';
  12. import '../../../utils/error_handler.dart';
  13. import 'package:keyboard/utils/atmob_log.dart';
  14. // 这里控制两个弹窗的数据
  15. @injectable
  16. class GoodsSurpriseController extends BaseController {
  17. final tag = 'GoodsSurpriseController';
  18. static const int countdownTime = 10 * 60 * 100; // 10分钟(毫秒单位,10ms 为 1 计数)
  19. final RxInt timeLeft = countdownTime.obs; // 剩余时间(10ms 为单位)
  20. Timer? _timer;
  21. final StoreRepository storeRepository;
  22. // 华为不展示
  23. CancelableFuture? _storeDataFuture;
  24. RxList<GoodsInfo> get goodsInfoList =>
  25. storeRepository.newDiscountGoodsInfoList;
  26. final Rxn<GoodsInfo> _firstAmount = Rxn<GoodsInfo>();
  27. GoodsInfo? get firstAmount => _firstAmount.value;
  28. final Rxn<GoodsInfo> _secondAmount = Rxn<GoodsInfo>();
  29. GoodsInfo? get secondAmount => _secondAmount.value;
  30. GoodsSurpriseController(this.storeRepository);
  31. @override
  32. void onInit() {
  33. if (isNotHWChannel()) {
  34. startCountdown();
  35. }
  36. if (goodsInfoList.isEmpty) {
  37. return;
  38. }
  39. if (goodsInfoList.length < 2) {
  40. AtmobLog.e(tag, "商品列表少于2个,无法处理");
  41. return;
  42. }
  43. if (Platform.isAndroid) {
  44. // 第一个直接拿第一个
  45. _firstAmount.value = goodsInfoList.first;
  46. // 第二个找一个 code 不一样的
  47. _secondAmount.value = goodsInfoList.firstWhereOrNull(
  48. (item) => item.code != _firstAmount.value?.code,
  49. );
  50. } else if (Platform.isIOS) {
  51. // iOS 直接拿第二个
  52. _secondAmount.value = goodsInfoList.first;
  53. // 第二个找一个 code 不一样的
  54. _firstAmount.value = goodsInfoList.firstWhereOrNull(
  55. (item) => item.code != _secondAmount.value?.code,
  56. );
  57. }
  58. // 打印
  59. print('qqq firstAmount: ${_firstAmount.value?.toJson()}');
  60. print('qqq secondAmount: ${_secondAmount.value?.toJson()}');
  61. if (_secondAmount.value == null) {
  62. // 如果没有找到不同的 code,默认取第二个
  63. _secondAmount.value = goodsInfoList[1];
  64. }
  65. super.onInit();
  66. }
  67. @override
  68. void onReady() {
  69. super.onReady();
  70. }
  71. void startCountdown() {
  72. _timer = Timer.periodic(const Duration(milliseconds: 10), (timer) {
  73. if (timeLeft.value > 0) {
  74. timeLeft.value--;
  75. } else {
  76. timer.cancel();
  77. SmartDialog.dismiss();
  78. }
  79. });
  80. }
  81. void onCountdownEnd() {
  82. SmartDialog.dismiss(tag: SurpriseDialog.tag);
  83. }
  84. @override
  85. void onClose() {
  86. _timer?.cancel();
  87. super.onClose();
  88. _storeDataFuture?.cancel();
  89. }
  90. Future<void> getItemRetention() async {
  91. storeRepository.getItemRetention();
  92. }
  93. void refreshStoreData() {
  94. _storeDataFuture?.cancel();
  95. _storeDataFuture = AsyncUtil.retryWithExponentialBackoff(
  96. () => _requestGoodsInfoList(),
  97. 4,
  98. );
  99. _storeDataFuture?.catchError((error) {
  100. ErrorHandler.toastError(error);
  101. });
  102. }
  103. Future<void> _requestGoodsInfoList() async {
  104. try {
  105. final response = await storeRepository.getItemRetention();
  106. if (response.firstAmount != null) {
  107. _firstAmount.value = response.firstAmount;
  108. print('firstAmount: ${response.firstAmount?.toJson()}');
  109. }
  110. if (response.secondAmount != null) {
  111. _secondAmount.value = response.secondAmount;
  112. print('secondAmount: ${response.secondAmount?.toJson()}');
  113. }
  114. } catch (e) {
  115. AtmobLog.e(tag, e.toString());
  116. }
  117. }
  118. }