Merge remote-tracking branch 'origin/main'
# Conflicts: # apps/app_seeds/lib/i18n/strings.g.dart # apps/app_seeds/lib/state/inventory_cubit.dart
This commit is contained in:
commit
00db9d4b6a
13 changed files with 218 additions and 17 deletions
|
|
@ -23,6 +23,7 @@ class InventoryState extends Equatable {
|
|||
this.sowThisMonthOnly = false,
|
||||
this.filterMonth = 0,
|
||||
this.loading = true,
|
||||
this.error,
|
||||
this.selectionMode = false,
|
||||
this.selectedIds = const {},
|
||||
});
|
||||
|
|
@ -63,6 +64,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;
|
||||
|
|
@ -131,6 +137,7 @@ class InventoryState extends Equatable {
|
|||
bool? sowThisMonthOnly,
|
||||
int? filterMonth,
|
||||
bool? loading,
|
||||
String? Function()? error,
|
||||
bool? selectionMode,
|
||||
Set<String>? selectedIds,
|
||||
}) {
|
||||
|
|
@ -147,6 +154,7 @@ class InventoryState extends Equatable {
|
|||
sowThisMonthOnly: sowThisMonthOnly ?? this.sowThisMonthOnly,
|
||||
filterMonth: filterMonth ?? this.filterMonth,
|
||||
loading: loading ?? this.loading,
|
||||
error: error != null ? error() : this.error,
|
||||
selectionMode: selectionMode ?? this.selectionMode,
|
||||
selectedIds: selectedIds ?? this.selectedIds,
|
||||
);
|
||||
|
|
@ -165,6 +173,7 @@ class InventoryState extends Equatable {
|
|||
sowThisMonthOnly,
|
||||
filterMonth,
|
||||
loading,
|
||||
error,
|
||||
selectionMode,
|
||||
selectedIds,
|
||||
];
|
||||
|
|
@ -176,22 +185,81 @@ class InventoryCubit extends Cubit<InventoryState> {
|
|||
InventoryCubit(this._repo, {int Function()? nowMonth})
|
||||
: _nowMonth = nowMonth ?? (() => DateTime.now().month),
|
||||
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;
|
||||
final int Function() _nowMonth;
|
||||
late final StreamSubscription<
|
||||
StreamSubscription<
|
||||
({List<VarietyListItem> items, List<VarietyListItem> drafts})
|
||||
>
|
||||
>?
|
||||
_sub;
|
||||
|
||||
/// Pending auto-retry, cancelled on a fresh (re)subscribe or on close.
|
||||
Timer? _retryTimer;
|
||||
|
||||
/// Consecutive stream failures since the last good emission. Drives the
|
||||
/// backoff and, once [_maxAutoRetries] is hit, the switch to a manual retry.
|
||||
int _failures = 0;
|
||||
|
||||
/// How many times we silently re-open the stream before giving up and asking
|
||||
/// the user. The startup DB-not-ready race clears in well under a second, so
|
||||
/// a handful of backed-off attempts recovers it without the user noticing.
|
||||
static const _maxAutoRetries = 6;
|
||||
|
||||
/// (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 auto-retry with backoff (staying in [loading]
|
||||
/// so the user just sees the spinner briefly), and only surface [error] for a
|
||||
/// manual [retry] once the transient window has clearly passed.
|
||||
void _subscribe() {
|
||||
_retryTimer?.cancel();
|
||||
_sub?.cancel();
|
||||
_sub = _repo.watchInventoryView().listen(
|
||||
(view) {
|
||||
_failures = 0;
|
||||
emit(
|
||||
state.copyWith(
|
||||
items: view.items,
|
||||
drafts: view.drafts,
|
||||
loading: false,
|
||||
error: () => null,
|
||||
),
|
||||
);
|
||||
},
|
||||
onError: _onStreamError,
|
||||
);
|
||||
}
|
||||
|
||||
void _onStreamError(Object e) {
|
||||
if (isClosed) return;
|
||||
_failures++;
|
||||
if (_failures <= _maxAutoRetries) {
|
||||
// Exponential backoff capped at ~4s: 250ms, 500ms, 1s, 2s, 4s, 4s.
|
||||
final delayMs = (250 * (1 << (_failures - 1))).clamp(250, 4000);
|
||||
// Stay in loading — an auto-recovering spinner, not an error screen.
|
||||
_retryTimer = Timer(Duration(milliseconds: delayMs), () {
|
||||
if (!isClosed) _subscribe();
|
||||
});
|
||||
} else {
|
||||
// Transient window has passed; hand it to the user.
|
||||
emit(state.copyWith(loading: false, error: () => '$e'));
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-opens the inventory stream on demand (from the manual retry button),
|
||||
/// resetting the auto-retry budget and returning to the loading state.
|
||||
void retry() {
|
||||
_failures = 0;
|
||||
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).
|
||||
|
|
@ -270,7 +338,8 @@ class InventoryCubit extends Cubit<InventoryState> {
|
|||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await _sub.cancel();
|
||||
_retryTimer?.cancel();
|
||||
await _sub?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue