import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../data/datasources/remote/trading_remote_datasource.dart'; import '../../data/models/p2p_transfer_model.dart'; import '../../core/di/injection.dart'; /// P2P转账状态 class TransferState { final bool isLoading; final String? error; final P2pTransferModel? lastTransfer; final AccountLookupModel? recipientAccount; TransferState({ this.isLoading = false, this.error, this.lastTransfer, this.recipientAccount, }); TransferState copyWith({ bool? isLoading, String? error, P2pTransferModel? lastTransfer, AccountLookupModel? recipientAccount, bool clearError = false, bool clearRecipient = false, }) { return TransferState( isLoading: isLoading ?? this.isLoading, error: clearError ? null : (error ?? this.error), lastTransfer: lastTransfer ?? this.lastTransfer, recipientAccount: clearRecipient ? null : (recipientAccount ?? this.recipientAccount), ); } } class TransferNotifier extends StateNotifier { final TradingRemoteDataSource _dataSource; TransferNotifier(this._dataSource) : super(TransferState()); /// 查询收款方账户 Future lookupRecipient(String phone) async { state = state.copyWith(isLoading: true, clearError: true, clearRecipient: true); try { final account = await _dataSource.lookupAccount(phone); state = state.copyWith( isLoading: false, recipientAccount: account, ); return account; } catch (e) { state = state.copyWith( isLoading: false, error: e.toString(), ); return null; } } /// 执行P2P转账 Future transfer({ required String toPhone, required String amount, String? memo, }) async { state = state.copyWith(isLoading: true, clearError: true); try { final result = await _dataSource.p2pTransfer( toPhone: toPhone, amount: amount, memo: memo, ); state = state.copyWith( isLoading: false, lastTransfer: result, ); return true; } catch (e) { state = state.copyWith( isLoading: false, error: e.toString(), ); return false; } } /// 清除状态 void clearState() { state = TransferState(); } /// 清除错误 void clearError() { state = state.copyWith(clearError: true); } } final transferNotifierProvider = StateNotifierProvider( (ref) => TransferNotifier(getIt()), ); /// P2P转账历史记录 final p2pTransferHistoryProvider = FutureProvider.family, String>( (ref, accountSequence) async { final dataSource = getIt(); return dataSource.getP2pTransferHistory(accountSequence); }, );