goods_surprise_controller.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. SmartDialog.dismiss();
  48. }
  49. });
  50. }
  51. void onCountdownEnd() {
  52. SmartDialog.dismiss(tag: SurpriseDialog.tag);
  53. }
  54. @override
  55. void onClose() {
  56. _timer?.cancel();
  57. super.onClose();
  58. _storeDataFuture?.cancel();
  59. }
  60. Future<void> getItemRetention() async {
  61. storeRepository.getItemRetention();
  62. }
  63. void refreshStoreData() {
  64. _storeDataFuture?.cancel();
  65. _storeDataFuture = AsyncUtil.retryWithExponentialBackoff(
  66. () => _requestGoodsInfoList(),
  67. 4,
  68. );
  69. _storeDataFuture?.catchError((error) {
  70. ErrorHandler.toastError(error);
  71. });
  72. }
  73. Future<void> _requestGoodsInfoList() async {
  74. try {
  75. final response = await storeRepository.getItemRetention();
  76. if (response.firstAmount != null) {
  77. _firstAmount.value = response.firstAmount;
  78. print('firstAmount: ${response.firstAmount?.toJson()}');
  79. }
  80. if (response.secondAmount != null) {
  81. _secondAmount.value = response.secondAmount;
  82. print('secondAmount: ${response.secondAmount?.toJson()}');
  83. }
  84. } catch (e) {
  85. AtmobLog.e(tag, e.toString());
  86. }
  87. }
  88. }