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(num amount) async { if (amount <= 0) return; emit(state.copyWith(action: WalletAction.toppingUp)); try { await _repo.topup(amount); if (isClosed) return; emit(state.copyWith(action: WalletAction.topupDone)); // الرصيد يُعاد جلبه من الخادم لا يُحسب محلياً: المال لا يُقدَّر // في التطبيق (docs/23 §0.2). await load(); } on ApiException { if (!isClosed) emit(state.copyWith(action: WalletAction.topupFailed)); } } }