Animate the radar timeline with a double-buffered overlay

Completes milestone 3: frames render over the map, play as a loop, and can be
scrubbed, with the legend drawn from the manifest rather than a constant that
could drift from what the worker actually rendered.

The overlay alternates two MapLibre image sources. Updating one source in place
flickers, because the layer briefly shows a half-written texture; adding every
frame as its own layer avoids that but pins them all in GPU memory, and twenty
512x512 RGBA frames is about 80 MB. Two buffers cost the same whether the
timeline holds six frames or sixty. Platform-channel work is serialised because
`show` is called faster than the round trip completes during playback, and
overlapping updates would swap visibility out of order and strobe.

Prefetching loads a window around the playhead, nearest first and forward
before backward, since playback moves forward and that frame is needed
soonest. `FrameCache` is byte-budgeted rather than entry-counted because frame
size tracks how much precipitation is on screen, and it evicts by distance from
the playhead: plain LRU would keep frames the prefetcher touched a moment ago
even after the playhead moved to the far end of the timeline.

Also adds DpcRadarSource, which reads published frames from our CDN and never
from the DPC API. Without it, the `dpc` adapter would have had to fall back to
mock, putting demo frames on screen under the label of live data — exactly the
confusion the adapter split exists to prevent. It now fails naming the missing
setting instead.

Running it on the emulator caught three things the tests had not:

- The notifier wrote to `state` from inside `build()`, which Riverpod rejects
  as an uninitialised provider. That broke startup, not just tests.
- Eight-month-old demo frames rendered as "Aggiornato 342535 minuti fa". The
  age formatter now steps up to hours and days.
- Demo mode sat permanently behind a stale-data warning and so never showed the
  working state it exists to demonstrate. MockRadarSource now shifts the
  bundled timestamps onto the present, leaving images, order and spacing
  untouched, so the timeline behaves exactly as it would on live data.

Corrects docs/stack-decisions.md, which described a disk cache that was not
built: mock frames already live in the asset bundle, so a disk layer belongs
with the network adapter where it would save a real request.

Verified: analyze clean, 129 tests passing, and on the emulator the loop
advances, wraps, and reports "Aggiornato ora".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-10 12:57:16 +02:00
co-authored by Claude Opus 5
parent 8ff8a903b8
commit 91f9ac0fbf
18 changed files with 1856 additions and 26 deletions
+187
View File
@@ -0,0 +1,187 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:nuvolari/core/cache/frame_cache.dart';
Uint8List bytesOf(int length, [int fill = 0]) =>
Uint8List.fromList(List<int>.filled(length, fill));
void main() {
group('FrameCache', () {
test('stores and returns bytes', () {
final cache = FrameCache(maxBytes: 1000);
final frame = bytesOf(10);
cache.put('a', frame);
expect(cache.get('a'), same(frame));
expect(cache.contains('a'), isTrue);
expect(cache.currentBytes, 10);
expect(cache.length, 1);
});
test('returns null for a key it does not hold', () {
expect(FrameCache(maxBytes: 1000).get('missing'), isNull);
});
test('replacing a key does not double-count its bytes', () {
final cache = FrameCache(maxBytes: 1000);
cache.put('a', bytesOf(100));
cache.put('a', bytesOf(40));
expect(cache.currentBytes, 40);
expect(cache.length, 1);
});
test('evicts the least recently used entry when over budget', () {
final cache = FrameCache(maxBytes: 250);
cache.put('a', bytesOf(100));
cache.put('b', bytesOf(100));
cache.put('c', bytesOf(100));
expect(cache.contains('a'), isFalse);
expect(cache.contains('b'), isTrue);
expect(cache.contains('c'), isTrue);
expect(cache.currentBytes, 200);
});
// Reading is what marks a frame as still wanted, so a frame the playhead
// keeps returning to must survive newer arrivals.
test('a read promotes an entry ahead of the eviction queue', () {
final cache = FrameCache(maxBytes: 250);
cache.put('a', bytesOf(100));
cache.put('b', bytesOf(100));
cache.get('a');
cache.put('c', bytesOf(100));
expect(cache.contains('a'), isTrue);
expect(cache.contains('b'), isFalse);
});
// Refusing an outsized frame would mean the timeline could never display
// that moment at all, which is worse than briefly exceeding the target.
test('keeps a frame larger than the whole budget', () {
final cache = FrameCache(maxBytes: 100);
cache.put('a', bytesOf(50));
cache.put('huge', bytesOf(400));
expect(cache.contains('huge'), isTrue);
expect(cache.length, 1);
expect(cache.currentBytes, 400);
});
test('clear empties it', () {
final cache = FrameCache(maxBytes: 1000);
cache.put('a', bytesOf(10));
cache.clear();
expect(cache.length, 0);
expect(cache.currentBytes, 0);
});
});
group('FrameCache.retainOnly', () {
test('drops everything outside the set', () {
final cache = FrameCache(maxBytes: 1000);
cache.put('a', bytesOf(10));
cache.put('b', bytesOf(20));
cache.put('c', bytesOf(30));
cache.retainOnly({'b'});
expect(cache.keys, orderedEquals(<String>['b']));
expect(cache.currentBytes, 20);
});
test('keeping everything changes nothing', () {
final cache = FrameCache(maxBytes: 1000);
cache.put('a', bytesOf(10));
cache.put('b', bytesOf(20));
cache.retainOnly({'a', 'b', 'not-present'});
expect(cache.length, 2);
expect(cache.currentBytes, 30);
});
test('an empty set empties the cache', () {
final cache = FrameCache(maxBytes: 1000);
cache.put('a', bytesOf(10));
cache.retainOnly(<String>{});
expect(cache.length, 0);
expect(cache.currentBytes, 0);
});
});
group('FrameCache.load', () {
test('fetches on a miss and caches the result', () async {
final cache = FrameCache(maxBytes: 1000);
var calls = 0;
final first = await cache.load('a', () async {
calls++;
return bytesOf(10, 1);
});
final second = await cache.load('a', () async {
calls++;
return bytesOf(10, 2);
});
expect(calls, 1);
expect(first, same(second));
});
// Scrubbing and playback can both ask for the same frame within the same
// millisecond. Two fetches would double the network cost for nothing.
test('concurrent loads of one key share a single fetch', () async {
final cache = FrameCache(maxBytes: 1000);
final gate = Completer<Uint8List>();
var calls = 0;
Future<Uint8List> fetch() {
calls++;
return gate.future;
}
final a = cache.load('a', fetch);
final b = cache.load('a', fetch);
gate.complete(bytesOf(10));
expect(await a, same(await b));
expect(calls, 1);
});
// A network blip must not make a frame permanently unavailable for the
// rest of the session.
test('a failed load is not cached and can be retried', () async {
final cache = FrameCache(maxBytes: 1000);
var attempt = 0;
await expectLater(
cache.load('a', () async {
attempt++;
throw StateError('boom');
}),
throwsStateError,
);
expect(cache.contains('a'), isFalse);
final bytes = await cache.load('a', () async {
attempt++;
return bytesOf(10);
});
expect(attempt, 2);
expect(bytes, hasLength(10));
expect(cache.contains('a'), isTrue);
});
});
}