dart format
This commit is contained in:
@@ -21,16 +21,19 @@ class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState>
|
||||
LoadableStateBloc() : super(const LoadableStateState(connections: null)) {
|
||||
on<ConnectivityChanged>((event, emit) {
|
||||
emit(event.state);
|
||||
if(connectivityStatusKnown() && isConnected()) {
|
||||
if(reFetch == null) return;
|
||||
if (connectivityStatusKnown() && isConnected()) {
|
||||
if (reFetch == null) return;
|
||||
reFetch!();
|
||||
}
|
||||
});
|
||||
|
||||
void emitConnectivity(List<ConnectivityResult> result) => add(ConnectivityChanged(LoadableStateState(connections: result)));
|
||||
void emitConnectivity(List<ConnectivityResult> result) =>
|
||||
add(ConnectivityChanged(LoadableStateState(connections: result)));
|
||||
|
||||
Connectivity().checkConnectivity().then(emitConnectivity);
|
||||
_updateStream = Connectivity().onConnectivityChanged.listen(emitConnectivity);
|
||||
_updateStream = Connectivity().onConnectivityChanged.listen(
|
||||
emitConnectivity,
|
||||
);
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@@ -38,42 +41,51 @@ class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState>
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state != AppLifecycleState.resumed) return;
|
||||
final now = DateTime.now();
|
||||
if (now.difference(_lastResumeRefetch) < const Duration(seconds: 10)) return;
|
||||
if (now.difference(_lastResumeRefetch) < const Duration(seconds: 10)) {
|
||||
return;
|
||||
}
|
||||
_lastResumeRefetch = now;
|
||||
// Re-check connectivity. The resulting [ConnectivityChanged] event takes
|
||||
// it from there: its handler updates the offline/online indicator and
|
||||
// triggers [reFetch] when the device is connected, so a stale
|
||||
// "Verbindung fehlgeschlagen" bar from a suspend-time fetch clears as
|
||||
// soon as the network is reachable again.
|
||||
unawaited(Connectivity().checkConnectivity().then(
|
||||
(result) => add(ConnectivityChanged(LoadableStateState(connections: result))),
|
||||
));
|
||||
unawaited(
|
||||
Connectivity().checkConnectivity().then(
|
||||
(result) =>
|
||||
add(ConnectivityChanged(LoadableStateState(connections: result))),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool connectivityStatusKnown() => state.connections != null;
|
||||
bool isConnected() => !(state.connections?.contains(ConnectivityResult.none) ?? true);
|
||||
bool isConnected() =>
|
||||
!(state.connections?.contains(ConnectivityResult.none) ?? true);
|
||||
bool allowRetry() => reFetch != null;
|
||||
|
||||
IconData connectionIcon() => connectivityStatusKnown()
|
||||
? isConnected()
|
||||
? Icons.nearby_error
|
||||
: Icons.signal_wifi_connected_no_internet_4
|
||||
? Icons.nearby_error
|
||||
: Icons.signal_wifi_connected_no_internet_4
|
||||
: Icons.device_unknown;
|
||||
|
||||
Color connectionColor(BuildContext context) => connectivityStatusKnown() && !isConnected()
|
||||
Color connectionColor(BuildContext context) =>
|
||||
connectivityStatusKnown() && !isConnected()
|
||||
? Colors.grey.shade600
|
||||
: Theme.of(context).primaryColor;
|
||||
|
||||
Color connectionForegroundColor(BuildContext context) => connectivityStatusKnown() && !isConnected()
|
||||
Color connectionForegroundColor(BuildContext context) =>
|
||||
connectivityStatusKnown() && !isConnected()
|
||||
? Colors.white
|
||||
: ThemeData.estimateBrightnessForColor(Theme.of(context).primaryColor) == Brightness.dark
|
||||
? Colors.white
|
||||
: Colors.black;
|
||||
: ThemeData.estimateBrightnessForColor(Theme.of(context).primaryColor) ==
|
||||
Brightness.dark
|
||||
? Colors.white
|
||||
: Colors.black;
|
||||
|
||||
String connectionText({int? lastUpdated}) => connectivityStatusKnown()
|
||||
? isConnected()
|
||||
? 'Verbindung fehlgeschlagen'
|
||||
: 'Offline${lastUpdated == null ? '' : ' - Stand von ${DateTime.fromMillisecondsSinceEpoch(lastUpdated).formatRelative()}'}'
|
||||
? 'Verbindung fehlgeschlagen'
|
||||
: 'Offline${lastUpdated == null ? '' : ' - Stand von ${DateTime.fromMillisecondsSinceEpoch(lastUpdated).formatRelative()}'}'
|
||||
: 'Unbekannte Fehlerursache';
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,6 +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