games_app/prediqo_mobile/lib/features/wallet/bloc/wallet_state.dart
2026-10-12 19:18:13 +02:00

216 lines
6.9 KiB
Dart

part of 'wallet_bloc.dart';
/// One Activity tab (cached in the app-scoped [WalletBloc]).
class WalletActivityPage extends Equatable {
const WalletActivityPage({
this.items = const [],
this.loading = false,
this.loadingMore = false,
this.loaded = false,
this.hasMore = true,
this.error,
});
final List<TransactionModel> items;
final bool loading;
final bool loadingMore;
final bool loaded;
final bool hasMore;
final String? error;
WalletActivityPage copyWith({
List<TransactionModel>? items,
bool? loading,
bool? loadingMore,
bool? loaded,
bool? hasMore,
String? error,
bool clearError = false,
}) {
return WalletActivityPage(
items: items ?? this.items,
loading: loading ?? this.loading,
loadingMore: loadingMore ?? this.loadingMore,
loaded: loaded ?? this.loaded,
hasMore: hasMore ?? this.hasMore,
error: clearError ? null : (error ?? this.error),
);
}
@override
List<Object?> get props => [items, loading, loadingMore, loaded, hasMore, error];
}
/// Immutable wallet UI state: balances, transactions, and operation results.
class WalletState extends Equatable {
const WalletState({
this.walletTypes = const [],
this.balances = const [],
this.transactions = const [],
this.deposits = const WalletActivityPage(),
this.withdrawals = const WalletActivityPage(),
this.transfers = const WalletActivityPage(),
this.predictions = const WalletActivityPage(),
this.testWalletStatus,
this.bonusWithdrawable,
this.withdrawalsEnabled = false,
this.withdrawalFeeRaw = '0',
this.healthLoaded = false,
this.depositAddress,
this.depositWalletType,
this.withdrawTxId,
this.withdrawStatus,
this.transferTxId,
this.transferStatus,
this.errorMessage,
this.errorCode,
this.isLoading = false,
});
const WalletState.initial() : this();
const WalletState.loading() : this(isLoading: true);
const WalletState.error(String msg) : this(errorMessage: msg);
const WalletState.loaded({
required List<WalletTypeModel> walletTypes,
required List<BalanceModel> balances,
required List<TransactionModel> transactions,
}) : this(
walletTypes: walletTypes,
balances: balances,
transactions: transactions,
);
const WalletState.depositAddress(String address, WalletTypeModel walletType)
: this(depositAddress: address, depositWalletType: walletType);
const WalletState.withdrawSuccess(String txId, String status)
: this(withdrawTxId: txId, withdrawStatus: status);
final List<WalletTypeModel> walletTypes;
final List<BalanceModel> balances;
final List<TransactionModel> transactions;
final WalletActivityPage deposits;
final WalletActivityPage withdrawals;
final WalletActivityPage transfers;
final WalletActivityPage predictions;
final TestWalletStatusModel? testWalletStatus;
final bool? bonusWithdrawable;
final bool withdrawalsEnabled;
final String withdrawalFeeRaw;
final bool healthLoaded;
final String? depositAddress;
final WalletTypeModel? depositWalletType;
final String? withdrawTxId;
final String? withdrawStatus;
final String? transferTxId;
final String? transferStatus;
final String? errorMessage;
final String? errorCode;
final bool isLoading;
WalletActivityPage activityFor(String category) {
switch (category) {
case 'withdrawals':
return withdrawals;
case 'transfers':
return transfers;
case 'predictions':
return predictions;
default:
return deposits;
}
}
/// Whether wallet types have been loaded and the bloc is not in a loading state.
bool get isLoaded => walletTypes.isNotEmpty && !isLoading;
/// Returns a copy with optional field overrides; [clearError] and [clearDeposit] reset those fields.
WalletState copyWith({
List<WalletTypeModel>? walletTypes,
List<BalanceModel>? balances,
List<TransactionModel>? transactions,
WalletActivityPage? deposits,
WalletActivityPage? withdrawals,
WalletActivityPage? transfers,
WalletActivityPage? predictions,
TestWalletStatusModel? testWalletStatus,
bool? bonusWithdrawable,
bool? withdrawalsEnabled,
String? withdrawalFeeRaw,
bool? healthLoaded,
String? depositAddress,
WalletTypeModel? depositWalletType,
String? withdrawTxId,
String? withdrawStatus,
String? transferTxId,
String? transferStatus,
String? errorMessage,
String? errorCode,
bool? isLoading,
bool clearError = false,
bool clearDeposit = false,
bool clearTransfer = false,
bool clearWithdraw = false,
}) {
return WalletState(
walletTypes: walletTypes ?? this.walletTypes,
balances: balances ?? this.balances,
transactions: transactions ?? this.transactions,
deposits: deposits ?? this.deposits,
withdrawals: withdrawals ?? this.withdrawals,
transfers: transfers ?? this.transfers,
predictions: predictions ?? this.predictions,
testWalletStatus: testWalletStatus ?? this.testWalletStatus,
bonusWithdrawable: bonusWithdrawable ?? this.bonusWithdrawable,
withdrawalsEnabled: withdrawalsEnabled ?? this.withdrawalsEnabled,
withdrawalFeeRaw: withdrawalFeeRaw ?? this.withdrawalFeeRaw,
healthLoaded: healthLoaded ?? this.healthLoaded,
depositAddress: clearDeposit ? null : (depositAddress ?? this.depositAddress),
depositWalletType: clearDeposit ? null : (depositWalletType ?? this.depositWalletType),
withdrawTxId: clearWithdraw ? null : (withdrawTxId ?? this.withdrawTxId),
withdrawStatus: clearWithdraw ? null : (withdrawStatus ?? this.withdrawStatus),
transferTxId: clearTransfer ? null : (transferTxId ?? this.transferTxId),
transferStatus: clearTransfer ? null : (transferStatus ?? this.transferStatus),
errorMessage: clearError ? null : (errorMessage ?? this.errorMessage),
errorCode: clearError ? null : (errorCode ?? this.errorCode),
isLoading: isLoading ?? this.isLoading,
);
}
WalletState copyWithActivity(String category, WalletActivityPage page) {
switch (category) {
case 'withdrawals':
return copyWith(withdrawals: page);
case 'transfers':
return copyWith(transfers: page);
case 'predictions':
return copyWith(predictions: page);
default:
return copyWith(deposits: page);
}
}
@override
List<Object?> get props => [
walletTypes,
balances,
transactions,
deposits,
withdrawals,
transfers,
predictions,
testWalletStatus,
bonusWithdrawable,
withdrawalsEnabled,
withdrawalFeeRaw,
healthLoaded,
depositAddress,
depositWalletType,
withdrawTxId,
withdrawStatus,
transferTxId,
transferStatus,
errorMessage,
errorCode,
isLoading,
];
}