import 'package:bloc/bloc.dart'; import 'package:dartz/dartz.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:injectable/injectable.dart'; import '../../../domain/wallet/wallet.dart'; part 'wallet_loader_event.dart'; part 'wallet_loader_state.dart'; part 'wallet_loader_bloc.freezed.dart'; @injectable class WalletLoaderBloc extends Bloc { final IWalletRepository _repository; WalletLoaderBloc(this._repository) : super(WalletLoaderState.initial()) { on(_onWalletLoaderEvent); } Future _onWalletLoaderEvent( WalletLoaderEvent event, Emitter emit, ) { return event.map( fetched: (e) async { emit(state.copyWith(isFetching: true, failureOption: none())); final result = await _repository.getWallet(); final data = result.fold( (f) => state.copyWith(failureOption: optionOf(f)), (summary) => state.copyWith(summary: summary, hasLoaded: true), ); emit(data.copyWith(isFetching: false)); }, coinBalanceUpdated: (e) async { emit( state.copyWith( summary: state.summary.copyWith(coinBalance: e.coinBalance), ), ); }, cleared: (e) async => emit(WalletLoaderState.initial()), ); } }