Files
enaklo-flutter/lib/application/outlet/outlet_loader/outlet_loader_bloc.dart
T

41 lines
1.2 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 'outlet_loader_event.dart';
part 'outlet_loader_state.dart';
part 'outlet_loader_bloc.freezed.dart';
@injectable
class OutletLoaderBloc extends Bloc<OutletLoaderEvent, OutletLoaderState> {
final IOutletRepository _repository;
OutletLoaderBloc(this._repository) : super(OutletLoaderState.initial()) {
on<OutletLoaderEvent>(_onOutletLoaderEvent);
}
Future<void> _onOutletLoaderEvent(
OutletLoaderEvent event,
Emitter<OutletLoaderState> emit,
) {
return event.map(
fetched: (e) async {
emit(state.copyWith(isFetching: true, failureOption: none()));
final result = await _repository.getOutlets();
final data = result.fold(
(f) => state.copyWith(failureOption: optionOf(f)),
(outlets) => state.copyWith(outlets: outlets, hasLoaded: true),
);
emit(data.copyWith(isFetching: false));
},
cleared: (e) async => emit(OutletLoaderState.initial()),
);
}
}