fix(inventory): recover from a stuck loading spinner

The inventory stream subscription had no onError handler, so a transient
failure at startup (e.g. the encrypted DB not yet ready) went unhandled
and left loading=true forever — the spinner that only a restart cleared.

Handle stream errors: drop out of loading, surface an error state, and
offer a retry that re-opens the stream. Add a _LoadError view (i18n en/
es/ast/pt) and cover both the failure and the retry-recovers paths.
This commit is contained in:
vjrj 2026-07-10 22:12:24 +02:00
parent bb4ee2fd89
commit f45c452615
12 changed files with 158 additions and 17 deletions

View file

@ -20,6 +20,7 @@ class InventoryState extends Equatable {
this.needsReproductionOnly = false,
this.sharingOnly = false,
this.loading = true,
this.error,
this.selectionMode = false,
this.selectedIds = const {},
});
@ -51,6 +52,11 @@ class InventoryState extends Equatable {
final bool loading;
/// Set when the inventory stream fails (e.g. the encrypted DB wasn't ready).
/// The UI shows a retry affordance instead of an endless spinner; null when
/// fine. See [InventoryCubit.retry].
final String? error;
/// Whether the list is in multi-select mode used to pick a subset of the
/// inventory to print labels for.
final bool selectionMode;
@ -110,6 +116,7 @@ class InventoryState extends Equatable {
bool? needsReproductionOnly,
bool? sharingOnly,
bool? loading,
String? Function()? error,
bool? selectionMode,
Set<String>? selectedIds,
}) {
@ -124,6 +131,7 @@ class InventoryState extends Equatable {
needsReproductionOnly ?? this.needsReproductionOnly,
sharingOnly: sharingOnly ?? this.sharingOnly,
loading: loading ?? this.loading,
error: error != null ? error() : this.error,
selectionMode: selectionMode ?? this.selectionMode,
selectedIds: selectedIds ?? this.selectedIds,
);
@ -140,6 +148,7 @@ class InventoryState extends Equatable {
needsReproductionOnly,
sharingOnly,
loading,
error,
selectionMode,
selectedIds,
];
@ -149,21 +158,46 @@ class InventoryState extends Equatable {
/// automatically after a quick-add no manual refresh.
class InventoryCubit extends Cubit<InventoryState> {
InventoryCubit(this._repo) : super(const InventoryState()) {
// One combined subscription (list + draft tray). Two separate StreamGroups
// here re-emit in a loop that hangs widget tests see watchInventoryView.
_sub = _repo.watchInventoryView().listen(
(view) => emit(
state.copyWith(items: view.items, drafts: view.drafts, loading: false),
),
);
_subscribe();
}
final VarietyRepository _repo;
late final StreamSubscription<
StreamSubscription<
({List<VarietyListItem> items, List<VarietyListItem> drafts})
>
>?
_sub;
/// (Re)opens the combined inventory subscription (list + draft tray). One
/// subscription only: two separate StreamGroups here re-emit in a loop that
/// hangs widget tests see watchInventoryView.
///
/// The onError handler is load-bearing: without it a transient stream failure
/// (e.g. the encrypted DB not yet ready at startup) would go unhandled and
/// leave [InventoryState.loading] true forever the "stuck spinner" that a
/// restart clears. On error we drop out of loading and surface [error] so the
/// UI can offer [retry].
void _subscribe() {
_sub?.cancel();
_sub = _repo.watchInventoryView().listen(
(view) => emit(
state.copyWith(
items: view.items,
drafts: view.drafts,
loading: false,
error: () => null,
),
),
onError: (Object e) =>
emit(state.copyWith(loading: false, error: () => '$e')),
);
}
/// Re-opens the inventory stream after a failure, back to the loading state.
void retry() {
emit(state.copyWith(loading: true, error: () => null));
_subscribe();
}
void search(String query) => emit(state.copyWith(query: query));
/// Toggles a category in the filter (add if absent, remove if present).
@ -235,7 +269,7 @@ class InventoryCubit extends Cubit<InventoryState> {
@override
Future<void> close() async {
await _sub.cancel();
await _sub?.cancel();
return super.close();
}
}