mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
The four polling status cards (ARP, mDNS, scanners summary, devices
summary) each rolled their own Timer/_status/_error/_isLoading state and
flipped to a red 'Error' UI on the first failed poll, throwing away
last-known-good data that was still in memory. A momentary backend blip
made every card flash red.
Introduce PolledValue<T>, a ChangeNotifier that owns the periodic timer,
in-flight CancelToken, last value, last error, and a freshness
classification (initialLoading / fresh / stale / error). 'stale' kicks in
on the first failure but keeps the value visible; 'error' only takes over
after staleErrorAfter elapses without a success (30s for the 5s scanner
polls, 3min for the 1min device-summary poll). All four cards now branch
on freshness and show a small live-updating warning icon with a tooltip
('Last updated N ago — <error>') while stale, instead of flipping red.
Also threads CancelToken? through the three polled GET endpoints.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
151 lines
4.5 KiB
Dart
151 lines
4.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../model/arp_scanner_status.dart';
|
|
import '../utils/duration_formatter.dart';
|
|
import '../utils/oott_api.dart';
|
|
import '../utils/polled_value.dart';
|
|
import 'polled_stale_indicator.dart';
|
|
|
|
class ArpScannerCard extends StatefulWidget {
|
|
const ArpScannerCard({super.key});
|
|
|
|
@override
|
|
State<ArpScannerCard> createState() => _ArpScannerCardState();
|
|
}
|
|
|
|
class _ArpScannerCardState extends State<ArpScannerCard> {
|
|
late final PolledValue<ArpScannerStatus> _polled;
|
|
Timer? _tickTimer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_polled = PolledValue<ArpScannerStatus>(
|
|
fetch: ({cancelToken}) =>
|
|
BackendAPI.instance.getArpScannerStatus(cancelToken: cancelToken),
|
|
pollInterval: const Duration(seconds: 5),
|
|
staleErrorAfter: const Duration(seconds: 30),
|
|
);
|
|
_tickTimer = Timer.periodic(const Duration(seconds: 1), (_) {
|
|
if (mounted) setState(() {});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_tickTimer?.cancel();
|
|
_polled.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListenableBuilder(
|
|
listenable: _polled,
|
|
builder: (context, _) {
|
|
if (_polled.freshness == PolledFreshness.initialLoading) {
|
|
return const Card(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(16),
|
|
child: Center(child: CircularProgressIndicator()),
|
|
),
|
|
);
|
|
}
|
|
|
|
final (color, label, sublabel) = _resolveState();
|
|
final isStale = _polled.freshness == PolledFreshness.stale;
|
|
|
|
return Card(
|
|
clipBehavior: Clip.antiAlias,
|
|
child: InkWell(
|
|
onTap: () => context.go('/status'),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.circle, color: color, size: 14),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Text(
|
|
'ARP Scanner',
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
),
|
|
if (isStale) ...[
|
|
const SizedBox(width: 6),
|
|
PolledStaleIndicator(polled: _polled),
|
|
],
|
|
],
|
|
),
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
label,
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
),
|
|
if (sublabel != null) ...[
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
sublabel,
|
|
style: Theme.of(context).textTheme.bodySmall
|
|
?.copyWith(
|
|
color: Theme.of(context).colorScheme.outline,
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
(Color, String, String?) _resolveState() {
|
|
if (_polled.freshness == PolledFreshness.error) {
|
|
return (
|
|
Colors.red,
|
|
'Error',
|
|
_polled.lastErrorMessage ??
|
|
'Unable to reach the server. Check the logs for details.',
|
|
);
|
|
}
|
|
|
|
final status = _polled.value!;
|
|
final lastSuccessAt = _polled.lastSuccessAt!;
|
|
final elapsed = DateTime.now()
|
|
.difference(lastSuccessAt)
|
|
.inSeconds
|
|
.toDouble();
|
|
|
|
if (status.isRunning) {
|
|
final sub = status.runningForSeconds != null
|
|
? 'Running for ${formatSeconds(status.runningForSeconds! + elapsed)}'
|
|
: null;
|
|
return (Colors.green, 'Running', sub);
|
|
}
|
|
if (status.nextRunInSeconds != null) {
|
|
final remaining = (status.nextRunInSeconds! - elapsed).clamp(
|
|
0.0,
|
|
double.infinity,
|
|
);
|
|
return (
|
|
Colors.amber,
|
|
'Waiting for next run',
|
|
'Next run in ${formatSeconds(remaining)}',
|
|
);
|
|
}
|
|
return (Colors.grey, 'Not yet started', null);
|
|
}
|
|
}
|