Files
enaklo-flutter/lib/application/wallet/wallet_loader/wallet_loader_bloc.dart
T

48 lines
1.3 KiB
Dart
Raw Normal View History

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<WalletLoaderEvent, WalletLoaderState> {
final IWalletRepository _repository;
WalletLoaderBloc(this._repository) : super(WalletLoaderState.initial()) {
on<WalletLoaderEvent>(_onWalletLoaderEvent);
}
Future<void> _onWalletLoaderEvent(
WalletLoaderEvent event,
Emitter<WalletLoaderState> 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()),
);
}
}