Files
flutter_tank_web_app/lib/controller/gasstations_controller.dart
T
2026-09-26 23:26:58 +02:00

99 lines
2.9 KiB
Dart

import 'dart:async';
import 'package:get/get.dart';
import '../models/econtrol_model.dart';
import '../services/geolocation_service.dart';
import '../services/econtrol_service.dart';
class GasstationsController extends GetxController {
final isLoading = false.obs;
final isLoadingLocation = false.obs;
final errorMessage = ''.obs;
final eControlData = <EControlModel>[].obs;
@override
void onInit() {
_loadStationInfosList();
super.onInit();
}
@override
void onReady() {}
@override
void onClose() {}
Future<void> _loadStationInfosList() async {
isLoading.value = true;
errorMessage.value = '';
try {
var geolocationService = GeolocationService();
var eControlService = EControlService();
// Standort-Abfrage mit Timeout versehen (z. B. 8 Sekunden),
// falls der Browser/User bei der Rechtefreigabe blockiert oder hängt
await geolocationService
.getCurrentLocation()
.timeout(
const Duration(seconds: 8),
onTimeout: () {
throw TimeoutException('Standortabfrage hat zu lange gedauert.');
},
);
if (geolocationService.hasLocation) {
final fuelData = await eControlService.getBothFuelData(
geolocationService.latitude,
geolocationService.longitude,
);
List<EControlModel> dieselStations = (fuelData['DIE'] as List)
.map((json) => EControlModel.fromJson(json))
.toList();
List<EControlModel> supStations = (fuelData['SUP'] as List)
.map((json) => EControlModel.fromJson(json))
.toList();
Map<int, EControlModel> combinedMap = {};
for (var station in dieselStations) {
if (station.id != null) {
combinedMap[station.id!] = station;
}
}
for (var supStation in supStations) {
if (supStation.id != null) {
if (combinedMap.containsKey(supStation.id)) {
final existingPrices = combinedMap[supStation.id!]!.prices ?? [];
final supPrices = supStation.prices ?? [];
combinedMap[supStation.id!]!.prices = [...existingPrices, ...supPrices];
} else {
combinedMap[supStation.id!] = supStation;
}
}
}
eControlData.value = combinedMap.values.toList();
} else {
errorMessage.value =
'Standort konnte nicht ermittelt werden. Bitte prüfen Sie Ihre Browser-Freigabe.';
}
} on TimeoutException {
errorMessage.value =
'Standortabfrage zeitlich überschritten. Bitte erlauben Sie den Standortzugriff im Browser.';
} catch (e) {
errorMessage.value =
'Fehler beim Laden der Daten: ${e.toString()}';
} finally {
// Stellt sicher, dass das Lade-Icon auf jeden Fall verschwindet
isLoading.value = false;
update();
}
}
void retry() {
_loadStationInfosList();
}
}