import 'package:flutter_bloc/flutter_bloc.dart'; import '../../../core/api/api_exception.dart'; import '../../../core/ui/view_status.dart'; import '../data/wallet_repository.dart'; import 'wallet_state.dart'; class WalletCubit extends Cubit { WalletCubit(this._repo) : super(const WalletState()); final WalletRepository _repo; Future load() async { emit(state.copyWith(status: ViewStatus.loading)); try { final wallet = await _repo.wallet(); final txns = await _repo.transactions(); if (isClosed) return; emit(state.copyWith( status: ViewStatus.success, wallet: wallet, transactions: txns, )); } on ApiException catch (e) { if (!isClosed) { emit(state.copyWith(status: ViewStatus.error, error: e.message)); } } } /// شحن حقيقي. **لا يزيد الرصيد فوراً** إلا في نمط الدفع الفوري — الفاتورة /// تُعرض للمستخدم ويحوّل، والتسوية تصل لاحقاً عبر مطابقة رسالة المزوّد. Future topup({required num amount, required String provider}) async { if (amount <= 0) return; emit(state.copyWith(action: WalletAction.toppingUp)); try { final result = await _repo.charge( amount: amount, provider: provider, currency: state.currency, ); if (isClosed) return; if (result.isInstant) { emit(state.copyWith(action: WalletAction.topupDone)); // الرصيد يُعاد جلبه من الخادم لا يُحسب محلياً: المال لا يُقدَّر // في التطبيق (docs/23 §0.2). await load(); return; } emit(state.copyWith( action: WalletAction.invoiceReady, invoice: result, )); } on ApiException { if (!isClosed) emit(state.copyWith(action: WalletAction.topupFailed)); } } }