109 lines
3.3 KiB
Dart
109 lines
3.3 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;
|
|
double? currentLatitude;
|
|
double? currentLongitude;
|
|
|
|
@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) {
|
|
currentLatitude = geolocationService.latitude;
|
|
currentLongitude = geolocationService.longitude;
|
|
// for test only später wieder deaktivieren statische koordinaten
|
|
// currentLatitude = 47.938783246743355;
|
|
// currentLongitude = 13.762770926423814;
|
|
// ende Test only
|
|
|
|
final fuelData = await eControlService.getBothFuelData(
|
|
currentLatitude!,
|
|
currentLongitude!,
|
|
);
|
|
|
|
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();
|
|
}
|
|
}
|