loadable error screen, reload actions, autoreload
This commit is contained in:
@ -2,21 +2,47 @@ import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../loading_error.dart';
|
||||
import 'loadable_state_event.dart';
|
||||
import 'loadable_state_state.dart';
|
||||
|
||||
class LoadableStateBloc extends Bloc<void, LoadableStateState> {
|
||||
class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState> {
|
||||
late StreamSubscription<List<ConnectivityResult>> _updateStream;
|
||||
LoadingError? loadingError;
|
||||
|
||||
LoadableStateBloc() : super(const LoadableStateState(connections: null)) {
|
||||
emitState(List<ConnectivityResult> v) => emit(state.copyWith(connections: v));
|
||||
on<ConnectivityChanged>((event, emit) {
|
||||
emit(event.state);
|
||||
if(connectivityStatusKnown() && isConnected() && loadingError != null) {
|
||||
if(!loadingError!.enableRetry) return;
|
||||
loadingError!.retry!();
|
||||
}
|
||||
});
|
||||
|
||||
Connectivity().checkConnectivity().then(emitState);
|
||||
_updateStream = Connectivity().onConnectivityChanged.listen(emitState);
|
||||
emitConnectivity(List<ConnectivityResult> result) => add(ConnectivityChanged(LoadableStateState(connections: result)));
|
||||
|
||||
Connectivity().checkConnectivity().then(emitConnectivity);
|
||||
_updateStream = Connectivity().onConnectivityChanged.listen(emitConnectivity);
|
||||
}
|
||||
|
||||
bool connectivityStatusKnown() => state.connections != null;
|
||||
bool isConnected({bool? def}) => !(state.connections?.contains(ConnectivityResult.none) ?? def!);
|
||||
bool isConnected() => !(state.connections?.contains(ConnectivityResult.none) ?? true);
|
||||
bool allowRetry() => loadingError?.retry != null;
|
||||
bool showErrorMessage() => isConnected() && loadingError != null;
|
||||
|
||||
IconData connectionIcon() => connectivityStatusKnown()
|
||||
? isConnected()
|
||||
? Icons.nearby_error
|
||||
: Icons.signal_wifi_connected_no_internet_4
|
||||
: Icons.device_unknown;
|
||||
|
||||
String connectionText() => connectivityStatusKnown()
|
||||
? isConnected()
|
||||
? 'Verbindung fehlgeschlagen'
|
||||
: 'Offline'
|
||||
: 'Unbekannte Fehlerursache';
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
|
@ -0,0 +1,7 @@
|
||||
import 'loadable_state_state.dart';
|
||||
|
||||
sealed class LoadableStateEvent {}
|
||||
final class ConnectivityChanged extends LoadableStateEvent {
|
||||
final LoadableStateState state;
|
||||
ConnectivityChanged(this.state);
|
||||
}
|
Reference in New Issue
Block a user