Files
oott/frontend/lib/widgets/polled_stale_indicator.dart
T
rzuastiandClaude Opus 4.7 bb677aeebf Centralize status polling with last-known-good, stale, and error tiers
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>
2026-05-31 10:03:19 -04:00

55 lines
1.3 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../utils/duration_formatter.dart';
import '../utils/polled_value.dart';
class PolledStaleIndicator extends StatefulWidget {
const PolledStaleIndicator({super.key, required this.polled});
final PolledValue polled;
@override
State<PolledStaleIndicator> createState() => _PolledStaleIndicatorState();
}
class _PolledStaleIndicatorState extends State<PolledStaleIndicator> {
Timer? _ticker;
@override
void initState() {
super.initState();
_ticker = Timer.periodic(const Duration(seconds: 1), (_) {
if (mounted) setState(() {});
});
}
@override
void dispose() {
_ticker?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
final lastSuccessAt = widget.polled.lastSuccessAt;
final ago = lastSuccessAt != null
? formatSeconds(
DateTime.now().difference(lastSuccessAt).inSeconds.toDouble(),
)
: 'a while';
final message = widget.polled.lastErrorMessage != null
? 'Last updated $ago ago — ${widget.polled.lastErrorMessage}'
: 'Last updated $ago ago';
return Tooltip(
message: message,
child: Icon(
Icons.warning_amber_rounded,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
);
}
}