mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
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.
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../utils/backend_reachability.dart';
|
||||
import '../utils/periodic_rebuild.dart';
|
||||
import '../utils/polled_value.dart';
|
||||
import 'polled_stale_indicator.dart';
|
||||
|
||||
typedef ScannerStatus = ({Color color, String label, List<String> sublabels});
|
||||
|
||||
typedef ScannerStatusResolver<T> =
|
||||
ScannerStatus Function(BuildContext context, T value, double elapsedSeconds);
|
||||
ScannerStatus Function(
|
||||
BuildContext context,
|
||||
T value,
|
||||
double elapsedSeconds,
|
||||
);
|
||||
|
||||
/// Generic card that polls a scanner status endpoint and renders the result
|
||||
/// using the provided [resolver]. The two scanner cards (ARP, mDNS) are thin
|
||||
/// wrappers over this widget.
|
||||
/// using the provided [resolver]. The per-scanner detail cards are configured
|
||||
/// over this widget in `scanner_status_cards.dart`.
|
||||
class ScannerStatusCard<T> extends StatefulWidget {
|
||||
const ScannerStatusCard({
|
||||
super.key,
|
||||
@@ -32,9 +35,9 @@ class ScannerStatusCard<T> extends StatefulWidget {
|
||||
State<ScannerStatusCard<T>> createState() => _ScannerStatusCardState<T>();
|
||||
}
|
||||
|
||||
class _ScannerStatusCardState<T> extends State<ScannerStatusCard<T>> {
|
||||
class _ScannerStatusCardState<T> extends State<ScannerStatusCard<T>>
|
||||
with PeriodicRebuild<ScannerStatusCard<T>> {
|
||||
late final PolledValue<T> _polled;
|
||||
Timer? _tickTimer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -44,14 +47,11 @@ class _ScannerStatusCardState<T> extends State<ScannerStatusCard<T>> {
|
||||
pollInterval: const Duration(seconds: 5),
|
||||
staleErrorAfter: const Duration(seconds: 30),
|
||||
);
|
||||
_tickTimer = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
startRebuildTicker();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tickTimer?.cancel();
|
||||
_polled.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -124,7 +124,10 @@ class _ScannerStatusCardState<T> extends State<ScannerStatusCard<T>> {
|
||||
);
|
||||
}
|
||||
|
||||
ScannerStatus _resolveStatus(BuildContext context, PolledFreshness freshness) {
|
||||
ScannerStatus _resolveStatus(
|
||||
BuildContext context,
|
||||
PolledFreshness freshness,
|
||||
) {
|
||||
if (freshness == PolledFreshness.error) {
|
||||
return (
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
|
||||
Reference in New Issue
Block a user