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>
77 lines
2.1 KiB
Dart
77 lines
2.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'oott_api.dart';
|
|
|
|
enum PolledFreshness { initialLoading, fresh, stale, error }
|
|
|
|
class PolledValue<T> extends ChangeNotifier {
|
|
PolledValue({
|
|
required Future<T> Function({CancelToken? cancelToken}) fetch,
|
|
required Duration pollInterval,
|
|
required Duration staleErrorAfter,
|
|
}) : _fetch = fetch,
|
|
_staleErrorAfter = staleErrorAfter {
|
|
_load();
|
|
_pollTimer = Timer.periodic(pollInterval, (_) => _load());
|
|
}
|
|
|
|
final Future<T> Function({CancelToken? cancelToken}) _fetch;
|
|
final Duration _staleErrorAfter;
|
|
Timer? _pollTimer;
|
|
CancelToken? _cancelToken;
|
|
bool _disposed = false;
|
|
|
|
T? _value;
|
|
DateTime? _lastSuccessAt;
|
|
String? _lastErrorMessage;
|
|
bool _everCompleted = false;
|
|
|
|
T? get value => _value;
|
|
DateTime? get lastSuccessAt => _lastSuccessAt;
|
|
String? get lastErrorMessage => _lastErrorMessage;
|
|
|
|
PolledFreshness get freshness {
|
|
if (!_everCompleted && _value == null) {
|
|
return PolledFreshness.initialLoading;
|
|
}
|
|
final last = _lastSuccessAt;
|
|
if (_value == null || last == null) return PolledFreshness.error;
|
|
if (_lastErrorMessage == null) return PolledFreshness.fresh;
|
|
return DateTime.now().difference(last) >= _staleErrorAfter
|
|
? PolledFreshness.error
|
|
: PolledFreshness.stale;
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
_cancelToken?.cancel();
|
|
final token = CancelToken();
|
|
_cancelToken = token;
|
|
try {
|
|
final result = await _fetch(cancelToken: token);
|
|
if (_disposed || token != _cancelToken) return;
|
|
_value = result;
|
|
_lastSuccessAt = DateTime.now();
|
|
_lastErrorMessage = null;
|
|
_everCompleted = true;
|
|
notifyListeners();
|
|
} catch (e) {
|
|
if (_disposed || token != _cancelToken) return;
|
|
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
|
_lastErrorMessage = dioErrorToUserMessage(e);
|
|
_everCompleted = true;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
_pollTimer?.cancel();
|
|
_cancelToken?.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|