import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../domain/wind/wind_math.dart'; import '../../services/wind/wind_service.dart'; import 'current_mission_provider.dart'; import 'map_controller_provider.dart'; final windServiceProvider = Provider((ref) => WindService()); /// Zustand der Kopfleisten-Wind-Pille (Doku 3.8). class HeaderWindState { const HeaderWindState({ this.sample, this.loading = false, this.showPerWaypoint = false, }); final WindSample? sample; final bool loading; /// Umschaltbar per Antippen der Pille (HTML-Demonstrator: /// showPerWaypointWind) - blendet Wind-Marker an jedem Wegpunkt ein/aus. final bool showPerWaypoint; HeaderWindState copyWith({ WindSample? sample, bool? loading, bool? showPerWaypoint, }) { return HeaderWindState( sample: sample ?? this.sample, loading: loading ?? this.loading, showPerWaypoint: showPerWaypoint ?? this.showPerWaypoint, ); } } /// Haelt den fuer die Kopfleiste interpolierten Wind (120 m AGL ueber dem /// Referenzort: erster Wegpunkt, sonst aktuelles Kartenzentrum) - analog /// refreshHeaderWind()/headerWindLocation() im HTML-Demonstrator. class HeaderWindNotifier extends Notifier { @override HeaderWindState build() => const HeaderWindState(); Future refresh() async { final waypoints = ref.read(currentMissionProvider); (double, double)? location; if (waypoints.isNotEmpty) { location = (waypoints.first.lat, waypoints.first.lon); } else { try { final center = ref.read(mapControllerProvider).camera.center; location = (center.latitude, center.longitude); } catch (_) { // Kamera noch nicht bereit (Karte noch nicht gemountet/gelayoutet). location = null; } } if (location == null) return; state = state.copyWith(loading: true); final sample = await ref.read(windServiceProvider).fetchHeaderWind(location.$1, location.$2); state = state.copyWith(loading: false, sample: sample); } void toggleShowPerWaypoint() { state = state.copyWith(showPerWaypoint: !state.showPerWaypoint); } } final headerWindProvider = NotifierProvider(HeaderWindNotifier.new); /// Referenzort fuer Windabfragen ohne festen Bezugspunkt (Wind- /// Validierungspanel aus der Wegpunktliste): erster Wegpunkt, sonst /// aktuelles Kartenzentrum - analog headerWindLocation() im HTML- /// Demonstrator. Null, wenn die Karte noch nicht gemountet/gelayoutet ist. (double, double)? windReferenceLocation(WidgetRef ref) { final waypoints = ref.read(currentMissionProvider); if (waypoints.isNotEmpty) { final first = waypoints.first; return (first.lat, first.lon); } try { final center = ref.read(mapControllerProvider).camera.center; return (center.latitude, center.longitude); } catch (_) { return null; } }