new location with locationIQ Model and service
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_tank_web_app/pages/home_view.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
|
||||
import '../models/locationiq_model.dart';
|
||||
import '../models/tank_model.dart';
|
||||
import '../services/appwrite_service.dart';
|
||||
import '../services/location_iq_service.dart';
|
||||
import '../pages/home_view.dart';
|
||||
|
||||
class EditController extends GetxController {
|
||||
final AppwriteService appwriteService = AppwriteService();
|
||||
@@ -23,6 +25,7 @@ class EditController extends GetxController {
|
||||
final isLoadingLocation = false.obs;
|
||||
|
||||
TankModel? editingTankModel;
|
||||
LocationIQ? currentLocationIQ;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -36,7 +39,7 @@ class EditController extends GetxController {
|
||||
} else {
|
||||
isNewEntry.value = true;
|
||||
_setDefaultDate();
|
||||
_requestLocation();
|
||||
_requestLocationIQ();
|
||||
}
|
||||
|
||||
// Add listeners for automatic calculation
|
||||
@@ -59,7 +62,79 @@ class EditController extends GetxController {
|
||||
'${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Future<void> _requestLocation() async {
|
||||
// Future<void> _requestLocation() async {
|
||||
// bool serviceEnabled;
|
||||
// LocationPermission permission;
|
||||
|
||||
// try {
|
||||
// isLoadingLocation.value = true;
|
||||
|
||||
// // 1. Prüfen, ob Standortdienste aktiviert sind
|
||||
// serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
// if (!serviceEnabled) {
|
||||
// return Future.error('Standortdienste sind deaktiviert.');
|
||||
// }
|
||||
|
||||
// // 2. Berechtigungen prüfen
|
||||
// permission = await Geolocator.checkPermission();
|
||||
// if (permission == LocationPermission.denied) {
|
||||
// permission = await Geolocator.requestPermission();
|
||||
// if (permission == LocationPermission.denied) {
|
||||
// return Future.error('Berechtigung verweigert.');
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 3. Position abrufen
|
||||
// Position position = await Geolocator.getCurrentPosition(
|
||||
// locationSettings: const LocationSettings(
|
||||
// accuracy: LocationAccuracy.high,
|
||||
// ),
|
||||
// );
|
||||
|
||||
// // 4. Standort über Backend-Proxy abrufen
|
||||
// var lat = position.latitude;
|
||||
// var lon = position.longitude;
|
||||
|
||||
// print('📍 Verwende Backend-Proxy für Geocoding...');
|
||||
// String location = await appwriteService.geocodeLocation(lat, lon);
|
||||
// locationController.text = location;
|
||||
|
||||
// // Info anzeigen basierend auf Ergebnis
|
||||
// if (location.startsWith('Lat:')) {
|
||||
// Get.snackbar(
|
||||
// 'Hinweis',
|
||||
// 'Adresse konnte nicht abgerufen werden. Koordinaten gespeichert.',
|
||||
// snackPosition: SnackPosition.BOTTOM,
|
||||
// backgroundColor: Colors.orange[100],
|
||||
// colorText: Colors.orange[900],
|
||||
// duration: const Duration(seconds: 3),
|
||||
// );
|
||||
// } else {
|
||||
// Get.snackbar(
|
||||
// 'Erfolg',
|
||||
// 'Standort: $location',
|
||||
// snackPosition: SnackPosition.BOTTOM,
|
||||
// backgroundColor: Colors.green[100],
|
||||
// colorText: Colors.green[900],
|
||||
// duration: const Duration(seconds: 2),
|
||||
// );
|
||||
// }
|
||||
// } catch (e) {
|
||||
// Get.snackbar(
|
||||
// "Fehler",
|
||||
// "Standort konnte nicht abgerufen werden: $e",
|
||||
// snackPosition: SnackPosition.BOTTOM,
|
||||
// backgroundColor: Colors.red[100],
|
||||
// colorText: Colors.red[900],
|
||||
// );
|
||||
// print("Fehler beim Abrufen des Standorts: $e");
|
||||
// locationController.text = '';
|
||||
// } finally {
|
||||
// isLoadingLocation.value = false;
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> _requestLocationIQ() async {
|
||||
bool serviceEnabled;
|
||||
LocationPermission permission;
|
||||
|
||||
@@ -92,30 +167,11 @@ class EditController extends GetxController {
|
||||
var lat = position.latitude;
|
||||
var lon = position.longitude;
|
||||
|
||||
print('📍 Verwende Backend-Proxy für Geocoding...');
|
||||
String location = await appwriteService.geocodeLocation(lat, lon);
|
||||
locationController.text = location;
|
||||
|
||||
// Info anzeigen basierend auf Ergebnis
|
||||
if (location.startsWith('Lat:')) {
|
||||
Get.snackbar(
|
||||
'Hinweis',
|
||||
'Adresse konnte nicht abgerufen werden. Koordinaten gespeichert.',
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
backgroundColor: Colors.orange[100],
|
||||
colorText: Colors.orange[900],
|
||||
duration: const Duration(seconds: 3),
|
||||
);
|
||||
} else {
|
||||
Get.snackbar(
|
||||
'Erfolg',
|
||||
'Standort: $location',
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
backgroundColor: Colors.green[100],
|
||||
colorText: Colors.green[900],
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
print('📍 Verwende LocationIQ für Geocoding...');
|
||||
final LocationIQService locationIQService = LocationIQService();
|
||||
await locationIQService.fetchLocationIQ(lat, lon);
|
||||
currentLocationIQ = locationIQService.locationIQ;
|
||||
locationController.text = currentLocationIQ?.address?.shortAddress ?? '';
|
||||
} catch (e) {
|
||||
Get.snackbar(
|
||||
"Fehler",
|
||||
@@ -131,6 +187,9 @@ class EditController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void _calculateTotal() {
|
||||
final liters = double.tryParse(litersController.text) ?? 0.0;
|
||||
final pricePerLiter = double.tryParse(pricePerLiterController.text) ?? 0.0;
|
||||
|
||||
Reference in New Issue
Block a user