Files
flutter_tank_web_app/lib/controller/gasstations_controller.dart
T
2026-09-25 21:17:36 +02:00

72 lines
2.1 KiB
Dart

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 eControlData = <EControlModel>[].obs;
@override
void onInit() {
_loadStationInfosList();
super.onInit();
}
@override
void onReady() {}
@override
void onClose() {}
Future<void> _loadStationInfosList() async {
isLoading.value = true;
var geolocationService = GeolocationService();
var eControlService = EControlService();
await geolocationService.getCurrentLocation();
if (geolocationService.hasLocation) {
// Parallel Diesel und Benzin laden
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 zur Zusammenführung nach Tankstellen-ID
Map<int, EControlModel> combinedMap = {};
for (var station in dieselStations) {
if (station.id != null) {
combinedMap[station.id!] = station;
}
}
// 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;
}
}
}
eControlData.value = combinedMap.values.toList();
}
isLoading.value = false;
update();
}
}