2 Commits

7 changed files with 194 additions and 28 deletions
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../../extensions/date_time.dart'; import '../../../../../extensions/date_time.dart';
import '../loadable_state_indicators.dart';
import 'loadable_state_event.dart'; import 'loadable_state_event.dart';
import 'loadable_state_state.dart'; import 'loadable_state_state.dart';
@@ -13,48 +14,75 @@ class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState>
late StreamSubscription<List<ConnectivityResult>> _updateStream; late StreamSubscription<List<ConnectivityResult>> _updateStream;
void Function()? reFetch; void Function()? reFetch;
/// Last time [reFetch] was triggered by an [AppLifecycleState.resumed] static const Duration _refetchThrottle = Duration(seconds: 10);
/// event. Used to coalesce rapid foreground/background flips so we don't
/// spam the network when the user briefly checks notifications. /// Last time [reFetch] was triggered automatically (resume or reconnect).
DateTime _lastResumeRefetch = DateTime.fromMillisecondsSinceEpoch(0); /// Used to coalesce rapid foreground/background flips so we don't spam the
/// network when the user briefly checks notifications.
DateTime _lastRefetch = DateTime.fromMillisecondsSinceEpoch(0);
/// Delays committing a transition *into* the offline state. On resume
/// connectivity_plus frequently emits a transient `[none]` before the real
/// result lands; debouncing it prevents the offline bar from flashing.
Timer? _offlineDebounce;
LoadableStateBloc() : super(const LoadableStateState(connections: null)) { LoadableStateBloc() : super(const LoadableStateState(connections: null)) {
on<ConnectivityChanged>((event, emit) { on<ConnectivityChanged>((event, emit) {
emit(event.state); emit(event.state);
if (connectivityStatusKnown() && isConnected()) { if (connectivityStatusKnown() && isConnected()) _triggerRefetch();
if (reFetch == null) return;
reFetch!();
}
}); });
void emitConnectivity(List<ConnectivityResult> result) { Connectivity().checkConnectivity().then(_emitConnectivity);
_updateStream = Connectivity().onConnectivityChanged.listen(
_emitConnectivity,
);
WidgetsBinding.instance.addObserver(this);
}
void _emitConnectivity(List<ConnectivityResult> result) {
_offlineDebounce?.cancel();
// Commit a connected reading immediately (snappy recovery); only debounce
// the drop to offline so a transient tick doesn't surface as offline.
if (!result.contains(ConnectivityResult.none)) {
// The initial checkConnectivity() future is not cancellable and may // The initial checkConnectivity() future is not cancellable and may
// resolve after the bloc was disposed, so guard against a closed sink. // resolve after the bloc was disposed, so guard against a closed sink.
if (isClosed) return; if (isClosed) return;
add(ConnectivityChanged(LoadableStateState(connections: result))); add(ConnectivityChanged(LoadableStateState(connections: result)));
return;
}
_offlineDebounce = Timer(const Duration(milliseconds: 1200), () {
if (isClosed) return;
add(ConnectivityChanged(LoadableStateState(connections: result)));
});
} }
Connectivity().checkConnectivity().then(emitConnectivity); void _triggerRefetch() {
_updateStream = Connectivity().onConnectivityChanged.listen( final now = DateTime.now();
emitConnectivity, if (!shouldRefetch(
); _lastRefetch,
WidgetsBinding.instance.addObserver(this); now,
_refetchThrottle,
hasReFetch: reFetch != null,
)) {
return;
}
_lastRefetch = now;
reFetch!();
} }
@override @override
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
if (state != AppLifecycleState.resumed) return; if (state != AppLifecycleState.resumed) return;
final now = DateTime.now(); // Trigger the refetch synchronously so [RefetchStarted] clears any stale
if (now.difference(_lastResumeRefetch) < const Duration(seconds: 10)) { // error and enters the loading state before the first resume frame paints,
return; // instead of only after the async connectivity round-trip below.
} _triggerRefetch();
_lastResumeRefetch = now; // Still re-check connectivity so the (debounced) offline bar reflects the
// Re-check connectivity so the resulting [ConnectivityChanged] handler // real network state after resume.
// clears a stale error bar and triggers [reFetch] once reachable again.
unawaited( unawaited(
Connectivity().checkConnectivity().then((result) { Connectivity().checkConnectivity().then((result) {
if (isClosed) return; if (isClosed) return;
add(ConnectivityChanged(LoadableStateState(connections: result))); _emitConnectivity(result);
}), }),
); );
} }
@@ -92,6 +120,7 @@ class LoadableStateBloc extends Bloc<LoadableStateEvent, LoadableStateState>
@override @override
Future<void> close() { Future<void> close() {
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
_offlineDebounce?.cancel();
_updateStream.cancel(); _updateStream.cancel();
return super.close(); return super.close();
} }
@@ -0,0 +1,21 @@
// Pure decision helpers for the shared loadable-state indicators. Kept free of
// Flutter/connectivity imports so they can be unit-tested in isolation.
/// Whether the offline bar should be shown over cached content. Suppressed while
/// a fetch is in flight so a transient connectivity dip does not flash the bar
/// during the resume/refresh window.
bool shouldShowOfflineBar({
required bool hasContent,
required bool loading,
required bool connectivityKnown,
required bool connected,
}) => hasContent && !loading && connectivityKnown && !connected;
/// Whether an automatic refetch may run: only when a refetch callback exists and
/// the throttle [window] since the [last] trigger has elapsed.
bool shouldRefetch(
DateTime last,
DateTime now,
Duration window, {
required bool hasReFetch,
}) => hasReFetch && now.difference(last) >= window;
@@ -62,8 +62,8 @@ class LoadableStateConsumer<
final showPrimaryLoading = isLoading && !hasContent; final showPrimaryLoading = isLoading && !hasContent;
final showBackgroundLoading = isLoading && hasContent; final showBackgroundLoading = isLoading && hasContent;
final showError = hasError && !hasContent; final showError = hasError && !hasContent && !isLoading;
final showErrorBar = hasError && hasContent; final showErrorBar = hasError && hasContent && !isLoading;
// Keep the wrapper hierarchy stable across refresh cycles: reFetch flips to // Keep the wrapper hierarchy stable across refresh cycles: reFetch flips to
// null mid-refetch, and toggling the RefreshIndicator on that signal would // null mid-refetch, and toggling the RefreshIndicator on that signal would
@@ -102,6 +102,7 @@ class LoadableStateConsumer<
LoadableStateErrorBar( LoadableStateErrorBar(
visible: showErrorBar, visible: showErrorBar,
hasContent: hasContent, hasContent: hasContent,
loading: isLoading,
message: loadableState.error?.message, message: loadableState.error?.message,
technicalDetails: loadableState.error?.technicalDetails, technicalDetails: loadableState.error?.technicalDetails,
lastUpdated: loadableState.lastFetch, lastUpdated: loadableState.lastFetch,
@@ -5,16 +5,19 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../../widget/info_dialog.dart'; import '../../../../../widget/info_dialog.dart';
import '../bloc/loadable_state_bloc.dart'; import '../bloc/loadable_state_bloc.dart';
import '../loadable_state_indicators.dart';
class LoadableStateErrorBar extends StatelessWidget { class LoadableStateErrorBar extends StatelessWidget {
final bool visible; final bool visible;
final bool hasContent; final bool hasContent;
final bool loading;
final String? message; final String? message;
final String? technicalDetails; final String? technicalDetails;
final int? lastUpdated; final int? lastUpdated;
const LoadableStateErrorBar({ const LoadableStateErrorBar({
required this.visible, required this.visible,
this.hasContent = false, this.hasContent = false,
this.loading = false,
this.message, this.message,
this.technicalDetails, this.technicalDetails,
this.lastUpdated, this.lastUpdated,
@@ -26,8 +29,12 @@ class LoadableStateErrorBar extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final bloc = context.watch<LoadableStateBloc>(); final bloc = context.watch<LoadableStateBloc>();
final isOfflineWithCache = final isOfflineWithCache = shouldShowOfflineBar(
hasContent && bloc.connectivityStatusKnown() && !bloc.isConnected(); hasContent: hasContent,
loading: loading,
connectivityKnown: bloc.connectivityStatusKnown(),
connected: bloc.isConnected(),
);
final shouldShow = visible || isOfflineWithCache; final shouldShow = visible || isOfflineWithCache;
return AnimatedSize( return AnimatedSize(
@@ -55,7 +55,7 @@ class FilesSearchResults extends StatelessWidget {
final showPrimaryLoading = isLoading && !hasContent; final showPrimaryLoading = isLoading && !hasContent;
final showBackgroundLoading = isLoading && hasContent; final showBackgroundLoading = isLoading && hasContent;
final showErrorScreen = hasError && !hasContent && !isLoading; final showErrorScreen = hasError && !hasContent && !isLoading;
final showErrorBar = hasError && hasContent; final showErrorBar = hasError && hasContent && !isLoading;
final showEmpty = !hasContent && !hasError && !isLoading; final showEmpty = !hasContent && !hasError && !isLoading;
final errorMessage = hasError ? errorToUserMessage(controller.serverError) : null; final errorMessage = hasError ? errorToUserMessage(controller.serverError) : null;
@@ -65,6 +65,7 @@ class FilesSearchResults extends StatelessWidget {
LoadableStateErrorBar( LoadableStateErrorBar(
visible: showErrorBar, visible: showErrorBar,
hasContent: hasContent, hasContent: hasContent,
loading: isLoading,
message: errorMessage, message: errorMessage,
), ),
// Background loading sits *outside* the result Stack so the linear // Background loading sits *outside* the result Stack so the linear
+1 -1
View File
@@ -3,7 +3,7 @@ description: Mobile client for Webuntis and Nextcloud with Talk integration
publish_to: 'none' publish_to: 'none'
version: 1.4.0+59 version: 1.5.0+60
environment: environment:
sdk: ">=3.8.0 <4.0.0" sdk: ">=3.8.0 <4.0.0"
@@ -0,0 +1,107 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/state/app/infrastructure/loadable_state/loadable_state_indicators.dart';
void main() {
group('shouldShowOfflineBar', () {
test('shows when offline with cache and not loading', () {
expect(
shouldShowOfflineBar(
hasContent: true,
loading: false,
connectivityKnown: true,
connected: false,
),
isTrue,
);
});
test('is suppressed while loading even if offline with cache', () {
expect(
shouldShowOfflineBar(
hasContent: true,
loading: true,
connectivityKnown: true,
connected: false,
),
isFalse,
);
});
test('is hidden without cached content', () {
expect(
shouldShowOfflineBar(
hasContent: false,
loading: false,
connectivityKnown: true,
connected: false,
),
isFalse,
);
});
test('is hidden while connectivity is unknown', () {
expect(
shouldShowOfflineBar(
hasContent: true,
loading: false,
connectivityKnown: false,
connected: false,
),
isFalse,
);
});
test('is hidden when connected', () {
expect(
shouldShowOfflineBar(
hasContent: true,
loading: false,
connectivityKnown: true,
connected: true,
),
isFalse,
);
});
});
group('shouldRefetch', () {
final window = const Duration(seconds: 10);
final base = DateTime(2026, 1, 1, 12, 0, 0);
test('is false within the throttle window', () {
expect(
shouldRefetch(
base,
base.add(const Duration(seconds: 9)),
window,
hasReFetch: true,
),
isFalse,
);
});
test('is true once the window has elapsed', () {
expect(
shouldRefetch(
base,
base.add(const Duration(seconds: 10)),
window,
hasReFetch: true,
),
isTrue,
);
});
test('is false without a refetch callback', () {
expect(
shouldRefetch(
base,
base.add(const Duration(minutes: 5)),
window,
hasReFetch: false,
),
isFalse,
);
});
});
}