Files
oott/frontend/lib/utils/periodic_rebuild.dart
T
rzuasti 48d73fb207 Consolidate frontend scanner, pagination, and tick-timer duplication
Collapse the five scanner status models into two shared shapes,
ActiveScannerStatus and PassiveScannerStatus, mirroring the backend's
active/passive vocabulary. Replace the five near-identical per-scanner
detail card files with a single scanner_status_cards.dart (two shared
resolvers plus a config list), and rebuild the combined home card to
iterate a list of scanners with two shape resolvers instead of five
copy-pasted resolve methods.

Extract two reusable mixins:
- PeriodicRebuild: the shared once-a-second "rebuild to refresh elapsed
  text" timer used by the scanner cards and the stale indicator.
- PaginatedListState: the shared pagination state, page-size/page-count
  getters, cancel-token-aware fetch orchestration, and disposal used by
  the device and notification lists.

No behaviour change; ~900 lines removed. Tests and analyzer pass.
2026-06-05 21:57:58 -04:00

34 lines
993 B
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
/// Mixin for [State]s that display continuously-updating elapsed-time text
/// (e.g. "Running for 3m 12s"). It runs a once-per-second timer that calls
/// [setState] so those labels stay current, and cancels it automatically on
/// dispose.
///
/// Call [startRebuildTicker] from `initState` (or when becoming visible) and
/// [stopRebuildTicker] when the widget is paused/hidden. Both are idempotent.
mixin PeriodicRebuild<T extends StatefulWidget> on State<T> {
Timer? _rebuildTicker;
/// Starts the per-second rebuild ticker if it isn't already running.
void startRebuildTicker() {
_rebuildTicker ??= Timer.periodic(const Duration(seconds: 1), (_) {
if (mounted) setState(() {});
});
}
/// Stops the per-second rebuild ticker.
void stopRebuildTicker() {
_rebuildTicker?.cancel();
_rebuildTicker = null;
}
@override
void dispose() {
stopRebuildTicker();
super.dispose();
}
}