Files
oott/frontend/lib/widgets/mdns_scanner_card.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

153 lines
4.6 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../model/mdns_scanner_status.dart';
import '../utils/duration_formatter.dart';
import '../utils/oott_api.dart';
import '../utils/polled_value.dart';
import 'polled_stale_indicator.dart';
class MdnsScannerCard extends StatefulWidget {
const MdnsScannerCard({super.key});
@override
State<MdnsScannerCard> createState() => _MdnsScannerCardState();
}
class _MdnsScannerCardState extends State<MdnsScannerCard> {
late final PolledValue<MdnsScannerStatus> _polled;
Timer? _tickTimer;
@override
void initState() {
super.initState();
_polled = PolledValue<MdnsScannerStatus>(
fetch: ({cancelToken}) =>
BackendAPI.instance.getMdnsScannerStatus(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, sublabels) = _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(
'mDNS 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,
),
for (final sublabel in sublabels) ...[
const SizedBox(height: 2),
Text(
sublabel,
style: Theme.of(context).textTheme.bodySmall
?.copyWith(
color: Theme.of(context).colorScheme.outline,
),
),
],
],
),
),
],
),
),
),
);
},
);
}
(Color, String, List<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.isListening) {
final sublabels = <String>[];
if (status.listeningForSeconds != null) {
sublabels.add(
'Listening for ${formatSeconds(status.listeningForSeconds! + elapsed)} · ${status.devicesSeen} devices seen',
);
} else {
sublabels.add('${status.devicesSeen} devices seen');
}
if (status.lastDeviceSeenSecondsAgo != null) {
sublabels.add(
'Last device ${formatSeconds(status.lastDeviceSeenSecondsAgo! + elapsed)} ago',
);
}
return (Colors.green, 'Listening', sublabels);
}
return (Colors.grey, 'Not yet started', <String>[]);
}
}