Files
dmc/app/lib/ui/providers/terrain_provider.dart
Constantin LeueandClaude Sonnet 5 35351140ae Persist the ground elevation profile with the mission
The Mini-Altitude-Profile terrain data only ever lived in TerrainNotifier's
in-memory state (one slot, keyed by route). Every app restart, or every
switch away from and back to a mission, forced a full re-fetch of all
AWS Terrarium elevation tiles for the route - the noticeably slow load
the user reported for some missions was this happening on every visit,
not just once.

Adds a nullable terrainProfileJson column to the missions table
(schema v6->v7) and a saveTerrainProfile() repository method, kept
separate from the regular waypoint upsert() so an ordinary autosave never
clobbers an already-cached profile. TerrainNotifier persists a profile
right after a successful fetch (fire-and-forget) and gains restore(),
called from CurrentMissionMetaNotifier whenever a mission is loaded/
started so a previously fetched profile is available immediately -
ensureFor() still validates its routeKey before use, so a stale restored
profile is never shown for a route that has since changed.

Also included in mission export/import (MissionExportData/
ParsedMissionImport) so sharing a mission carries its terrain cache along
instead of forcing the recipient to refetch it.

Sample-count/resolution stays as-is for now (still fixed 30m spacing,
10-2000 samples) - adapting the sampling density to terrain variance
(e.g. coarser sampling over flat ground) is a separate follow-up.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-04 08:10:41 +02:00

78 lines
3.1 KiB
Dart

import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../domain/mission/terrain_math.dart';
import '../../domain/waypoint/flat_waypoint_list.dart';
import '../../services/terrain/terrain_service.dart';
import 'database_provider.dart';
final terrainServiceProvider = Provider<TerrainService>((ref) => TerrainService());
/// Zustand des Gelaendeprofils fuer die aktuelle Route (Doku 3.9).
class TerrainState {
const TerrainState({this.profile, this.loading = false});
final TerrainProfile? profile;
final bool loading;
TerrainState copyWith({TerrainProfile? profile, bool? loading}) {
return TerrainState(
profile: profile ?? this.profile,
loading: loading ?? this.loading,
);
}
}
/// Laedt das Gelaendeprofil nur, wenn sich die Route (routeKeyForTerrain)
/// seit dem letzten Laden geaendert hat - analog dem
/// `terrainSamples.routeKey === key`-Cache im HTML-Demonstrator. Ein
/// reiner Werte-/Aktionswechsel (Hoehe, Geschwindigkeit) an bestehenden
/// Wegpunkten loest daher keinen erneuten Kachel-Download aus.
class TerrainNotifier extends Notifier<TerrainState> {
@override
TerrainState build() => const TerrainState();
/// Uebernimmt ein bereits vorliegendes Profil direkt, ohne einen
/// Netzwerkabruf auszuloesen - genutzt beim Laden einer gespeicherten
/// Mission (Doku 3.9: persistiertes Profil) bzw. zum Zuruecksetzen beim
/// Start/Wechsel einer Mission. ensureFor() prueft dessen routeKey wie
/// gewohnt, bevor es angezeigt wird, ein veraltetes Profil wird also nie
/// faelschlich fuer eine inzwischen andere Route benutzt.
void restore(TerrainProfile? profile) {
state = TerrainState(profile: profile);
}
/// [missionId] persistiert ein frisch abgerufenes Profil an der
/// gespeicherten Mission (Doku 3.9), damit ein erneutes Oeffnen derselben
/// Mission nicht wieder alle Kacheln neu laden muss - null, solange die
/// Mission noch nie gespeichert wurde (dann bleibt nur der In-Memory-Cache
/// dieser Notifier-Instanz).
Future<void> ensureFor(List<Waypoint> waypoints, {int? missionId}) async {
if (waypoints.length < 2) {
if (state.profile != null) state = const TerrainState();
return;
}
final routeKey = routeKeyForTerrain(waypoints);
if (state.profile?.routeKey == routeKey) return;
if (state.loading) return;
state = state.copyWith(loading: true);
final profile = await ref.read(terrainServiceProvider).fetchProfile(waypoints);
// Waehrenddessen koennte sich die Route erneut geaendert haben - nur
// uebernehmen, wenn das Ergebnis noch zur aktuell angefragten Route passt.
if (profile != null && profile.routeKey != routeKeyForTerrain(waypoints)) {
state = state.copyWith(loading: false);
return;
}
state = TerrainState(profile: profile ?? state.profile, loading: false);
if (profile != null && missionId != null) {
unawaited(
ref.read(missionRepositoryProvider).saveTerrainProfile(missionId, profile),
);
}
}
}
final terrainProvider = NotifierProvider<TerrainNotifier, TerrainState>(TerrainNotifier.new);