withdraw_detail_provider.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import '../core/network/dio_client.dart';
  3. import '../data/models/asset/withdraw_record.dart';
  4. import '../data/services/withdraw_service.dart';
  5. // ══════════════════════════════════════════════════════════════
  6. // Withdraw Detail State
  7. // ═════════════════���════════════════════════════════════════════
  8. class WithdrawDetailState {
  9. final WithdrawRecord? record;
  10. final bool isLoading;
  11. final bool isCancelling;
  12. final String? errorMessage;
  13. const WithdrawDetailState({
  14. this.record,
  15. this.isLoading = false,
  16. this.isCancelling = false,
  17. this.errorMessage,
  18. });
  19. WithdrawDetailState copyWith({
  20. WithdrawRecord? record,
  21. bool? isLoading,
  22. bool? isCancelling,
  23. String? errorMessage,
  24. }) =>
  25. WithdrawDetailState(
  26. record: record ?? this.record,
  27. isLoading: isLoading ?? this.isLoading,
  28. isCancelling: isCancelling ?? this.isCancelling,
  29. errorMessage: errorMessage,
  30. );
  31. }
  32. // ══════════════════════════════════════════════════════════════
  33. // Withdraw Detail Notifier
  34. // ══════════════════════════════════════════════════════════════
  35. class WithdrawDetailNotifier extends Notifier<WithdrawDetailState> {
  36. @override
  37. WithdrawDetailState build() {
  38. return const WithdrawDetailState();
  39. }
  40. /// 加载单条提现/转账记录
  41. Future<void> loadRecord(WithdrawRecord initialRecord, {required bool isTransfer}) async {
  42. state = state.copyWith(isLoading: true, errorMessage: null);
  43. try {
  44. final dio = ref.read(dioClientProvider);
  45. final service = WithdrawService(dio);
  46. final record = isTransfer
  47. ? await service.getTransferRecord(initialRecord.id)
  48. : await service.getWithdrawRecord(initialRecord.id);
  49. state = state.copyWith(record: record, isLoading: false);
  50. } catch (e) {
  51. state = state.copyWith(isLoading: false, errorMessage: e.toString());
  52. }
  53. }
  54. /// 取消提现/转账
  55. Future<bool> cancel({required bool isTransfer}) async {
  56. final record = state.record;
  57. if (record == null) return false;
  58. state = state.copyWith(isCancelling: true, errorMessage: null);
  59. try {
  60. final dio = ref.read(dioClientProvider);
  61. final service = WithdrawService(dio);
  62. if (isTransfer) {
  63. await service.cancelTransfer(record.id);
  64. } else {
  65. await service.cancelWithdraw(record.id);
  66. }
  67. // 取消成功后刷新数据
  68. await loadRecord(record, isTransfer: isTransfer);
  69. state = state.copyWith(isCancelling: false);
  70. return true;
  71. } catch (e) {
  72. state = state.copyWith(isCancelling: false, errorMessage: e.toString());
  73. return false;
  74. }
  75. }
  76. /// 刷新记录
  77. Future<void> refresh({required bool isTransfer}) async {
  78. final record = state.record;
  79. if (record != null) {
  80. await loadRecord(record, isTransfer: isTransfer);
  81. }
  82. }
  83. }
  84. // ══════════════════════════════════════════════════════════════
  85. // Provider
  86. // ══════════════════════════════════════════════════════════════
  87. final withdrawDetailProvider =
  88. NotifierProvider<WithdrawDetailNotifier, WithdrawDetailState>(
  89. WithdrawDetailNotifier.new,
  90. );