goods_surprise_controller.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 =>
  21. storeRepository.newDiscountGoodsInfoList;
  22. final Rxn<GoodsInfo> _firstAmount = Rxn<GoodsInfo>();
  23. GoodsInfo? get firstAmount => _firstAmount.value;
  24. final Rxn<GoodsInfo> _secondAmount = Rxn<GoodsInfo>();
  25. GoodsInfo? get secondAmount => _secondAmount.value;
  26. GoodsSurpriseController(this.storeRepository);
  27. @override
  28. void onInit() {
  29. startCountdown();
  30. if (goodsInfoList.isEmpty) {
  31. return;
  32. }
  33. _firstAmount.value = goodsInfoList.first;
  34. _secondAmount.value = goodsInfoList.last;
  35. super.onInit();
  36. }
  37. @override
  38. void onReady() {
  39. super.onReady();
  40. }
  41. void startCountdown() {
  42. _timer = Timer.periodic(const Duration(milliseconds: 10), (timer) {
  43. if (timeLeft.value > 0) {
  44. timeLeft.value--;
  45. } else {
  46. timer.cancel();
  47. }
  48. });
  49. }
  50. void onCountdownEnd() {
  51. SmartDialog.dismiss(tag: SurpriseDialog.tag);
  52. }
  53. @override
  54. void onClose() {
  55. _timer?.cancel();
  56. super.onClose();
  57. _storeDataFuture?.cancel();
  58. }
  59. Future<void> getItemRetention() async {
  60. storeRepository.getItemRetention();
  61. }
  62. void refreshStoreData() {
  63. _storeDataFuture?.cancel();
  64. _storeDataFuture = AsyncUtil.retryWithExponentialBackoff(
  65. () => _requestGoodsInfoList(),
  66. 4,
  67. );
  68. _storeDataFuture?.catchError((error) {
  69. ErrorHandler.toastError(error);
  70. });
  71. }
  72. Future<void> _requestGoodsInfoList() async {
  73. try {
  74. final response = await storeRepository.getItemRetention();
  75. if (response.firstAmount != null) {
  76. _firstAmount.value = response.firstAmount;
  77. print('firstAmount: ${response.firstAmount?.toJson()}');
  78. }
  79. if (response.secondAmount != null) {
  80. _secondAmount.value = response.secondAmount;
  81. print('secondAmount: ${response.secondAmount?.toJson()}');
  82. }
  83. } catch (e) {
  84. AtmobLog.e(tag, e.toString());
  85. }
  86. }
  87. }