UI-02: animated skeleton map when tiles can't be fetched
Adds MapConnectivityState, a shared tracker of tile-fetch outcomes (cache miss + network failure) that flips every map into an animated skeleton after 3 consecutive failures and recovers on a single success -- either an ordinary fetch succeeding, or (once TileLayer has been fully unmounted in skeleton mode) a periodic single-tile probe every 15s. Detected at the fetch level rather than via an OS connectivity API, since a captive portal or degraded connection can report "online" while every real fetch times out. SkeletonMapLayer reuses the Stitch exports' 40px grid-overlay treatment with a shimmer sweep, replacing TileLayer entirely (never fetching underneath its own placeholder) while markers/polylines keep rendering since they come from local data. RideMap and the route planner's independent FlutterMap both wire this in via a plain skeletonMode bool. Moved the tile-source constants into a new tiles/tile_config.dart so the connectivity probe (in the app-layer composition root) doesn't need to import from ui/ to build its request URL. Verified end-to-end on a real emulator: cut network, cleared the tile cache, confirmed the skeleton renders after real fetch failures, then confirmed automatic recovery within one probe interval once network returned -- not just via the widget/unit tests that also cover this. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Xki7YAcc2TiN2PRZJ2tXr
This commit is contained in:
@@ -11,17 +11,24 @@ import 'package:flutter/painting.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'map_connectivity.dart';
|
||||
import 'tile_cache.dart';
|
||||
import 'tile_math.dart';
|
||||
|
||||
class CachedTileProvider extends TileProvider {
|
||||
CachedTileProvider({required this.cache, http.Client? client})
|
||||
CachedTileProvider({required this.cache, this.connectivity, http.Client? client})
|
||||
: _client = client ?? http.Client(),
|
||||
super();
|
||||
|
||||
final TileCache cache;
|
||||
final http.Client _client;
|
||||
|
||||
/// UI-02: told about every fetch outcome so it can decide whether the map should be
|
||||
/// showing an animated skeleton instead of tiles. Null in tests/callers that don't
|
||||
/// care -- skeleton mode is a UI concern layered on top of caching, not something
|
||||
/// this provider requires to function.
|
||||
final MapConnectivityState? connectivity;
|
||||
|
||||
@override
|
||||
ImageProvider getImage(TileCoordinates coordinates, TileLayer options) =>
|
||||
_CacheBackedImage(
|
||||
@@ -30,6 +37,7 @@ class CachedTileProvider extends TileProvider {
|
||||
headers: headers,
|
||||
cache: cache,
|
||||
client: _client,
|
||||
connectivity: connectivity,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,6 +48,7 @@ class _CacheBackedImage extends ImageProvider<_CacheBackedImage> {
|
||||
required this.headers,
|
||||
required this.cache,
|
||||
required this.client,
|
||||
this.connectivity,
|
||||
});
|
||||
|
||||
final TileKey key;
|
||||
@@ -47,6 +56,7 @@ class _CacheBackedImage extends ImageProvider<_CacheBackedImage> {
|
||||
final Map<String, String> headers;
|
||||
final TileCache cache;
|
||||
final http.Client client;
|
||||
final MapConnectivityState? connectivity;
|
||||
|
||||
@override
|
||||
Future<_CacheBackedImage> obtainKey(ImageConfiguration configuration) =>
|
||||
@@ -62,21 +72,32 @@ class _CacheBackedImage extends ImageProvider<_CacheBackedImage> {
|
||||
|
||||
Future<ui.Codec> _load(ImageDecoderCallback decode) async {
|
||||
final cached = await cache.get(key);
|
||||
// A cache hit says nothing about current connectivity either way -- it's not a
|
||||
// network round trip, so it neither counts as a success nor resets a failure run.
|
||||
final bytes = cached ?? await _fetchAndStore();
|
||||
final buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
|
||||
return decode(buffer);
|
||||
}
|
||||
|
||||
Future<Uint8List> _fetchAndStore() async {
|
||||
final response = await client.get(Uri.parse(url), headers: headers);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Tile fetch failed: ${response.statusCode} for $url');
|
||||
try {
|
||||
final response = await client.get(Uri.parse(url), headers: headers);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Tile fetch failed: ${response.statusCode} for $url');
|
||||
}
|
||||
final bytes = response.bodyBytes;
|
||||
// Write-through: viewing a tile online caches it for later, exactly like
|
||||
// flutter_map's own default caching did -- just capped and evictable now.
|
||||
await cache.put(key, bytes);
|
||||
connectivity?.reportSuccess();
|
||||
return bytes;
|
||||
} catch (_) {
|
||||
// UI-02: a cache miss whose network fetch also failed is exactly the "no
|
||||
// connection" signal skeleton mode is watching for -- report it and rethrow so
|
||||
// flutter_map's own error handling for this tile is unchanged.
|
||||
connectivity?.reportFailure();
|
||||
rethrow;
|
||||
}
|
||||
final bytes = response.bodyBytes;
|
||||
// Write-through: viewing a tile online caches it for later, exactly like
|
||||
// flutter_map's own default caching did -- just capped and evictable now.
|
||||
await cache.put(key, bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user