improved performance and battery usage on older devices
This commit is contained in:
@@ -20,10 +20,15 @@ class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState>
|
||||
|
||||
LoadableStateBloc() : super(const LoadableStateState(connections: null)) {
|
||||
on<ConnectivityChanged>((event, emit) {
|
||||
// Only a real reconnect (or a resume) warrants a refetch: the initial
|
||||
// status after mount and Wi-Fi ↔ mobile handovers would otherwise
|
||||
// reload an already loaded page for nothing.
|
||||
final wasOffline = connectivityStatusKnown() && !isConnected();
|
||||
emit(event.state);
|
||||
if (connectivityStatusKnown() && isConnected()) {
|
||||
if (reFetch == null) return;
|
||||
reFetch!();
|
||||
if ((wasOffline || event.fromResume) &&
|
||||
connectivityStatusKnown() &&
|
||||
isConnected()) {
|
||||
reFetch?.call();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -54,7 +59,12 @@ class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState>
|
||||
unawaited(
|
||||
Connectivity().checkConnectivity().then((result) {
|
||||
if (isClosed) return;
|
||||
add(ConnectivityChanged(LoadableStateState(connections: result)));
|
||||
add(
|
||||
ConnectivityChanged(
|
||||
LoadableStateState(connections: result),
|
||||
fromResume: true,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,5 +4,10 @@ sealed class LoadableStateEvent {}
|
||||
|
||||
final class ConnectivityChanged extends LoadableStateEvent {
|
||||
final LoadableStateState state;
|
||||
ConnectivityChanged(this.state);
|
||||
|
||||
/// Re-check after the app came back to the foreground: refetches whenever
|
||||
/// online, not only on an offline → online transition.
|
||||
final bool fromResume;
|
||||
|
||||
ConnectivityChanged(this.state, {this.fromResume = false});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user