goods_surprise_controller.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:async';
  2. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:get/get.dart';
  5. import 'package:keyboard/module/store/suprise/surprise_dialog.dart';
  6. import '../../../base/base_controller.dart';
  7. import '../../../data/bean/goods_info.dart';
  8. import '../../../data/repository/store_repository.dart';
  9. import '../../../utils/async_util.dart';
  10. import '../../../utils/error_handler.dart';
  11. import 'package:keyboard/utils/atmob_log.dart';
  12. @injectable
  13. class GoodsSurpriseController extends BaseController {
  14. final tag = 'GoodsSurpriseController';
  15. static const int countdownTime = 10 * 60 * 100; // 10分钟(毫秒单位,10ms 为 1 计数)
  16. final RxInt timeLeft = countdownTime.obs; // 剩余时间(10ms 为单位)
  17. Timer? _timer;
  18. final StoreRepository storeRepository;
  19. CancelableFuture? _storeDataFuture;
  20. RxList<GoodsInfo> get goodsInfoList => storeRepository.newDiscountGoodsInfoList;
  21. final Rxn<GoodsInfo> _firstAmount = Rxn<GoodsInfo>();
  22. GoodsInfo? get firstAmount => _firstAmount.value;
  23. final Rxn<GoodsInfo> _secondAmount = Rxn<GoodsInfo>();
  24. GoodsInfo? get secondAmount => _secondAmount.value;
  25. GoodsSurpriseController(this.storeRepository);
  26. @override
  27. void onInit() {
  28. startCountdown();
  29. _firstAmount.value = goodsInfoList.first;
  30. _secondAmount.value = goodsInfoList.last;
  31. super.onInit();
  32. }
  33. @override
  34. void onReady() {
  35. super.onReady();
  36. }
  37. void startCountdown() {
  38. _timer = Timer.periodic(const Duration(milliseconds: 10), (timer) {
  39. if (timeLeft.value > 0) {
  40. timeLeft.value--;
  41. } else {
  42. timer.cancel();
  43. }
  44. });
  45. }
  46. void onCountdownEnd() {
  47. SmartDialog.dismiss(tag: SurpriseDialog.tag);
  48. }
  49. @override
  50. void onClose() {
  51. _timer?.cancel();
  52. super.onClose();
  53. _storeDataFuture?.cancel();
  54. }
  55. Future<void> getItemRetention() async {
  56. storeRepository.getItemRetention();
  57. }
  58. void refreshStoreData() {
  59. _storeDataFuture?.cancel();
  60. _storeDataFuture = AsyncUtil.retryWithExponentialBackoff(
  61. () => _requestGoodsInfoList(),
  62. 4,
  63. );
  64. _storeDataFuture?.catchError((error) {
  65. ErrorHandler.toastError(error);
  66. });
  67. }
  68. Future<void> _requestGoodsInfoList() async {
  69. try {
  70. final response = await storeRepository.getItemRetention();
  71. if (response.firstAmount != null) {
  72. _firstAmount.value = response.firstAmount;
  73. print('firstAmount: ${response.firstAmount?.toJson()}');
  74. }
  75. if (response.secondAmount != null) {
  76. _secondAmount.value = response.secondAmount;
  77. print('secondAmount: ${response.secondAmount?.toJson()}');
  78. }
  79. } catch (e) {
  80. AtmobLog.e(tag, e.toString());
  81. }
  82. }
  83. }