moved reload actions out of error context

This commit is contained in:
2024-05-11 17:52:53 +02:00
parent 9fa711e460
commit 181682a424
14 changed files with 129 additions and 93 deletions

View File

@ -17,10 +17,10 @@ abstract class LoadableHydratedBloc<
> {
late TRepository _repository;
LoadableHydratedBloc() : super(const LoadableState()) {
on<Emit<TState>>((event, emit) => emit(LoadableState(isLoading: event.loading, data: event.state(innerState ?? fromNothing()))));
on<Reload<TState>>((event, emit) => emit(LoadableState(isLoading: true, data: innerState)));
on<Emit<TState>>((event, emit) => emit(LoadableState(isLoading: event.loading, data: event.state(innerState ?? fromNothing()), reFetch: retry)));
on<RefetchStarted<TState>>((event, emit) => emit(LoadableState(isLoading: true, data: innerState)));
on<ClearState<TState>>((event, emit) => emit(const LoadableState()));
on<Error<TState>>((event, emit) => emit(LoadableState(isLoading: false, data: innerState, error: event.error)));
on<Error<TState>>((event, emit) => emit(LoadableState(isLoading: false, data: innerState, reFetch: retry, error: event.error)));
_repository = repository();
fetch();
@ -29,22 +29,25 @@ abstract class LoadableHydratedBloc<
TState? get innerState => state.data;
TRepository get repo => _repository;
void retry() {
log('Fetch retry triggered for ${TState.toString()}');
add(RefetchStarted<TState>());
fetch();
}
void fetch() {
log('Fetching data for ${TState.toString()}');
gatherData().catchError(
(e) => add(
Error(
LoadingError(
message: e.toString(),
enableRetry: true,
retry: () {
log('Fetch retry on ${TState.toString()}');
add(Reload<TState>());
fetch();
}
)
)
)
);
(e) {
log('Error while fetching ${TState.toString()}: ${e.toString()}');
add(Error(LoadingError(
message: e.toString(),
allowRetry: true,
)));
}
).then((value) {
log('Fetch for ${TState.toString()} completed!');
});
}
@override

View File

@ -11,4 +11,4 @@ class Error<TState> extends LoadableHydratedBlocEvent<TState> {
final LoadingError error;
Error(this.error);
}
class Reload<TState> extends LoadableHydratedBlocEvent<TState> {}
class RefetchStarted<TState> extends LoadableHydratedBlocEvent<TState> {}