error wenn no econtrol API
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'package:get/get.dart';
|
||||
import '../models/econtrol_model.dart';
|
||||
import '../services/geolocation_service.dart';
|
||||
@@ -6,6 +7,7 @@ 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
|
||||
@@ -22,51 +24,76 @@ class GasstationsController extends GetxController {
|
||||
|
||||
Future<void> _loadStationInfosList() async {
|
||||
isLoading.value = true;
|
||||
var geolocationService = GeolocationService();
|
||||
var eControlService = EControlService();
|
||||
errorMessage.value = '';
|
||||
|
||||
await geolocationService.getCurrentLocation();
|
||||
if (geolocationService.hasLocation) {
|
||||
// Parallel Diesel und Benzin laden
|
||||
final fuelData = await eControlService.getBothFuelData(
|
||||
geolocationService.latitude,
|
||||
geolocationService.longitude,
|
||||
);
|
||||
try {
|
||||
var geolocationService = GeolocationService();
|
||||
var eControlService = EControlService();
|
||||
|
||||
List<EControlModel> dieselStations = (fuelData['DIE'] as List)
|
||||
.map((json) => EControlModel.fromJson(json))
|
||||
.toList();
|
||||
// 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.');
|
||||
},
|
||||
);
|
||||
|
||||
List<EControlModel> supStations = (fuelData['SUP'] as List)
|
||||
.map((json) => EControlModel.fromJson(json))
|
||||
.toList();
|
||||
if (geolocationService.hasLocation) {
|
||||
final fuelData = await eControlService.getBothFuelData(
|
||||
geolocationService.latitude,
|
||||
geolocationService.longitude,
|
||||
);
|
||||
|
||||
// Map zur Zusammenführung nach Tankstellen-ID
|
||||
Map<int, EControlModel> combinedMap = {};
|
||||
List<EControlModel> dieselStations = (fuelData['DIE'] as List)
|
||||
.map((json) => EControlModel.fromJson(json))
|
||||
.toList();
|
||||
|
||||
for (var station in dieselStations) {
|
||||
if (station.id != null) {
|
||||
combinedMap[station.id!] = station;
|
||||
}
|
||||
}
|
||||
List<EControlModel> supStations = (fuelData['SUP'] as List)
|
||||
.map((json) => EControlModel.fromJson(json))
|
||||
.toList();
|
||||
|
||||
// Benzin-Preise in dieselben Tankstellen-Objekte mergen
|
||||
for (var supStation in supStations) {
|
||||
if (supStation.id != null) {
|
||||
if (combinedMap.containsKey(supStation.id)) {
|
||||
// Falls Tankstelle schon existiert, füge Benzin-Preis hinzu
|
||||
final existingPrices = combinedMap[supStation.id!]!.prices ?? [];
|
||||
final supPrices = supStation.prices ?? [];
|
||||
combinedMap[supStation.id!]!.prices = [...existingPrices, ...supPrices];
|
||||
} else {
|
||||
combinedMap[supStation.id!] = supStation;
|
||||
Map<int, EControlModel> combinedMap = {};
|
||||
|
||||
for (var station in dieselStations) {
|
||||
if (station.id != null) {
|
||||
combinedMap[station.id!] = station;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eControlData.value = combinedMap.values.toList();
|
||||
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();
|
||||
}
|
||||
isLoading.value = false;
|
||||
update();
|
||||
}
|
||||
|
||||
void retry() {
|
||||
_loadStationInfosList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user