From 775b7ce0e7edf0e20cc1d00596dfb00a8ca65883 Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 6 Jun 2025 17:16:20 +0200 Subject: [PATCH] revert some things ig? --- lib/screens/addCam/add_cam_main.dart | 143 +++++++++--- lib/screens/addCam/widgets/karte.dart | 55 ++--- lib/screens/excursion/excursion_main.dart | 268 ++++++++++------------ 3 files changed, 256 insertions(+), 210 deletions(-) diff --git a/lib/screens/addCam/add_cam_main.dart b/lib/screens/addCam/add_cam_main.dart index 1a5edd1..0edaec8 100644 --- a/lib/screens/addCam/add_cam_main.dart +++ b/lib/screens/addCam/add_cam_main.dart @@ -112,11 +112,82 @@ class _AddCamMainState extends State { return puff; } + bool isLoadingPosition = false; + + Future _initializePosition() async { + try { + final position = await GeolocatorService.deteterminePosition(); + if (mounted) { + setState(() { + currentPosition = position; + isLoadingPosition = false; + // Update the text controllers with new position + rmap["DECLAT"]!["controller"]!.text = position.latitude.toString(); + rmap["DECLNG"]!["controller"]!.text = position.longitude.toString(); + }); + } + return position; + } catch (error) { + if (!mounted) { + return currentPosition; + } + + if (error is LocationDisabledException) { + SnackBarHelper.showSnackBarMessage( + context, + AppLocalizations.of(context)!.locationDisabled, + ); + } else if (error is LocationForbiddenException) { + SnackBarHelper.showSnackBarMessage( + context, + AppLocalizations.of(context)!.locationForbidden, + ); + } + + setState(() { + isLoadingPosition = false; + }); + return currentPosition; + } + } + @override void initState() { - // updates the currentPosition var after the _determine position has finished. Means user view updates with his live location + super.initState(); + + isTemplate = widget.isTemplate; + + // If a template is edited this fills in the existing values + if (widget.existingData?.isNotEmpty ?? false) { + for (var key in widget.existingData!.keys) { + rmap[key]!["controller"]!.text = + widget.existingData?[key].toString() ?? ""; + } + } else { + // If it is not a template set default values + rmap["Datum"]!["controller"]!.text = DateTime.now().toString(); + rmap["Status"]!["controller"]!.text = "aktiv"; + rmap["FotoFilm"]!["controller"]!.text = "Foto"; + rmap["MEZ"]!["controller"]!.text = "Sommerzeit"; + rmap["Platzung"]!["controller"]!.text = ""; + } + + // Set initial default position + rmap["DECLAT"]!["controller"]!.text = currentPosition.latitude.toString(); + rmap["DECLNG"]!["controller"]!.text = currentPosition.longitude.toString(); + + // Try to get current position GeolocatorService.deteterminePosition() - .then((result) => currentPosition = result) + .then((result) { + if (mounted) { + setState(() { + currentPosition = result; + // Update coordinates after getting the position + rmap["DECLAT"]!["controller"]!.text = result.latitude.toString(); + rmap["DECLNG"]!["controller"]!.text = result.longitude.toString(); + }); + } + }) .catchError((error) { if (error is LocationDisabledException) { if (mounted) { @@ -135,28 +206,6 @@ class _AddCamMainState extends State { } return currentPosition; }); - // select initial werte - rmap["DECLAT"]!["controller"]!.text = currentPosition.latitude.toString(); - rmap["DECLNG"]!["controller"]!.text = currentPosition.longitude.toString(); - - isTemplate = widget.isTemplate; - - // If a template is edited this fills in the existing values - if (widget.existingData?.isNotEmpty ?? false) { - for (var key in widget.existingData!.keys) { - rmap[key]!["controller"]!.text = - widget.existingData?[key].toString() ?? ""; - } - } else { - // If it is not a template set default values - rmap["Datum"]!["controller"]!.text = DateTime.now().toString(); - rmap["Status"]!["controller"]!.text = "aktiv"; - rmap["FotoFilm"]!["controller"]!.text = "Foto"; - rmap["MEZ"]!["controller"]!.text = "Sommerzeit"; - rmap["Platzung"]!["controller"]!.text = ""; - } - - super.initState(); } @override @@ -291,6 +340,24 @@ class _AddCamMainState extends State { const SizedBox(width: 15), ElevatedButton( onPressed: () async { + setState(() { + isLoadingPosition = true; + }); + + try { + await _initializePosition(); + } catch (e) { + // Error already handled in _initializePosition + if (mounted) { + setState(() { + isLoadingPosition = false; + }); + return; + } + } + + if (!mounted) return; + final result = await Navigator.of(context).push( MaterialPageRoute( builder: (context) { @@ -304,7 +371,8 @@ class _AddCamMainState extends State { }, ), ); - if (result != null) { + + if (result != null && mounted) { setState(() { currentPosition = Position( latitude: result.latitude, @@ -318,10 +386,33 @@ class _AddCamMainState extends State { speed: 0.0, speedAccuracy: 0.0, ); + rmap["DECLAT"]!["controller"]!.text = result.latitude.toString(); + rmap["DECLNG"]!["controller"]!.text = result.longitude.toString(); + isLoadingPosition = false; + }); + } else if (mounted) { + setState(() { + isLoadingPosition = false; }); } }, - child: Text(AppLocalizations.of(context)!.openMap), + child: isLoadingPosition + ? Row( + mainAxisSize: MainAxisSize.min, + children: [ + SizedBox( + width: 16, + height: 16, + child: CircularProgressIndicator( + strokeWidth: 2, + color: Theme.of(context).colorScheme.onPrimary, + ), + ), + const SizedBox(width: 8), + Text(AppLocalizations.of(context)!.openMap), + ], + ) + : Text(AppLocalizations.of(context)!.openMap), ), ], ), diff --git a/lib/screens/addCam/widgets/karte.dart b/lib/screens/addCam/widgets/karte.dart index 0cb4003..a20ae31 100644 --- a/lib/screens/addCam/widgets/karte.dart +++ b/lib/screens/addCam/widgets/karte.dart @@ -41,6 +41,7 @@ class KarteState extends State { Icons.location_on, color: Colors.red, )); + saveVisible = true; } @override @@ -51,42 +52,26 @@ class KarteState extends State { actions: [ Visibility( visible: saveVisible, - child: Row( - children: [ - Text(AppLocalizations.of(context)!.saveMap), - TextButton( - onPressed: () async { - // try { - // List placemarks = - // await placemarkFromCoordinates( - // currentMarker!.point.latitude, - // currentMarker!.point.longitude); - // - // widget.beiOrtC.text = placemarks.first.locality!; - // widget.ortInfoC.text = placemarks.first.street!; - // } catch (e) { - // // - // // !!! Localization - // if (context.mounted) { - // SnackBarHelper.showSnackBarMessage( - // context, "Geocoding error"); - // } - // debugPrint(e.toString()); - // } - - if (currentMarker != null) { - setState(() { - widget.decLatC.text = - currentMarker!.point.latitude.toString(); - widget.decLngC.text = - currentMarker!.point.longitude.toString(); - }); - } - if (context.mounted) Navigator.pop(context); - }, - child: const Icon(Icons.save), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 8.0), + child: ElevatedButton.icon( + onPressed: () async { + if (currentMarker != null) { + setState(() { + widget.decLatC.text = + currentMarker!.point.latitude.toString(); + widget.decLngC.text = + currentMarker!.point.longitude.toString(); + }); + } + if (context.mounted) Navigator.pop(context, currentMarker?.point); + }, + icon: const Icon(Icons.save), + label: Text(AppLocalizations.of(context)!.saveMap), + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), ), - ], + ), ), ), ], diff --git a/lib/screens/excursion/excursion_main.dart b/lib/screens/excursion/excursion_main.dart index 6ac95d5..517f6dd 100644 --- a/lib/screens/excursion/excursion_main.dart +++ b/lib/screens/excursion/excursion_main.dart @@ -43,9 +43,18 @@ class ExcursionMain extends StatefulWidget { class _ExcursionMainState extends State { int currentStep = 0; late bool isTemplate; - Position? currentPosition; - bool isLoadingPosition = true; - late Future _positionFuture; + Position currentPosition = Position( + longitude: 10.0, + latitude: 51.0, + timestamp: DateTime.now(), + accuracy: 0.0, + altitude: 0.0, + heading: 0.0, + speed: 0.0, + speedAccuracy: 0.0, + altitudeAccuracy: 0.0, + headingAccuracy: 0.0, + ); bool bimaExtended = false; @@ -114,8 +123,38 @@ class _ExcursionMainState extends State { @override void initState() { - super.initState(); - _positionFuture = _initializePosition(); + GeolocatorService.deteterminePosition( + alwaysOnNeeded: false, + ).then((result) => currentPosition = result).catchError((error) async { + if (error is LocationDisabledException) { + if (mounted) { + SnackBarHelper.showSnackBarMessage( + context, + AppLocalizations.of(context)!.locationDisabled, + ); + } + } else if (error is LocationForbiddenException) { + if (mounted) { + SnackBarHelper.showSnackBarMessage( + context, + AppLocalizations.of(context)!.locationForbidden, + ); + } + } else if (error is NeedAlwaysLocation) { + if (mounted) { + bool reload = + await AddEntriesDialogHelper.locationSettingsDialog(context); + if (reload) { + GeolocatorService.deteterminePosition() + .then((res) => currentPosition = res) + .catchError((error) { + return currentPosition; + }); + } + } + } + return currentPosition; + }); if (widget.existingData?.isNotEmpty ?? false) { for (var key in widget.existingData!.keys) { @@ -133,87 +172,8 @@ class _ExcursionMainState extends State { } isTemplate = widget.isTemplate; - } - Future _initializePosition() async { - try { - final position = await GeolocatorService.deteterminePosition( - alwaysOnNeeded: true, - ); - if (mounted) { - setState(() { - currentPosition = position; - isLoadingPosition = false; - }); - } - return position; - } catch (error) { - if (!mounted) { - return _getDefaultPosition(); - } - - if (error is LocationDisabledException) { - SnackBarHelper.showSnackBarMessage( - context, - AppLocalizations.of(context)!.locationDisabled, - ); - } else if (error is LocationForbiddenException) { - SnackBarHelper.showSnackBarMessage( - context, - AppLocalizations.of(context)!.locationForbidden, - ); - } else if (error is NeedAlwaysLocation) { - AddEntriesDialogHelper.locationSettingsDialog(context); - } - - // Return default position on any error - final defaultPosition = _getDefaultPosition(); - if (mounted) { - setState(() { - currentPosition = defaultPosition; - isLoadingPosition = false; - }); - } - return defaultPosition; - } - } - - Position _getDefaultPosition() { - return Position( - longitude: 10.0, - latitude: 51.0, - timestamp: DateTime.now(), - accuracy: 0.0, - altitude: 0.0, - heading: 0.0, - speed: 0.0, - speedAccuracy: 0.0, - altitudeAccuracy: 0.0, - headingAccuracy: 0.0, - ); - } - - Future _refreshPosition() async { - setState(() { - isLoadingPosition = true; - }); - _positionFuture = _initializePosition(); - try { - final position = await _positionFuture; - if (mounted) { - setState(() { - currentPosition = position; - isLoadingPosition = false; - }); - } - } catch (e) { - // Error already handled in _initializePosition - if (mounted) { - setState(() { - isLoadingPosition = false; - }); - } - } + super.initState(); } @override @@ -392,7 +352,76 @@ class _ExcursionMainState extends State { content: Column( children: [ // ---------- Tracking - _buildTrackingButtons(), + ElevatedButton( + onPressed: () async { + // Check for always permission before starting tracking + LocationPermission permission = await Geolocator.checkPermission(); + if (permission != LocationPermission.always) { + if (mounted) { + bool? shouldContinue = await showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text(AppLocalizations.of(context)!.trackingPermissionTitle), + content: Text(AppLocalizations.of(context)!.trackingPermissionContent), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: Text(AppLocalizations.of(context)!.cancel), + ), + TextButton( + onPressed: () async { + await Geolocator.openAppSettings(); + Navigator.of(context).pop(true); + }, + child: Text(AppLocalizations.of(context)!.openSettings), + ), + ], + ), + ); + + if (shouldContinue != true) { + return; + } + + // Wait for user to change settings and return + // Try checking the permission multiple times + for (int i = 0; i < 5; i++) { + await Future.delayed(const Duration(seconds: 1)); + if (!mounted) return; + + permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.always) { + break; + } + + // If this is the last attempt and we still don't have permission + if (i == 4 && permission != LocationPermission.always) { + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(AppLocalizations.of(context)!.permissionNotGranted), + duration: const Duration(seconds: 3), + ), + ); + } + return; + } + } + } + } + + await Navigator.push(context, MaterialPageRoute( + builder: (context) { + return Tracking( + weg: rmap["Weg"]!["controller"]!, + startPosition: currentPosition, + ); + }, + )); + setState(() {}); + }, + child: Text(AppLocalizations.of(context)!.trackingAnAusschalten), + ), const SizedBox(height: 10), // ---------- Weather @@ -512,9 +541,9 @@ class _ExcursionMainState extends State { child: Text(AppLocalizations.of(context)!.nein), ), TextButton( - onPressed: () { - saveTemplate(getFieldsText(), DatabasesEnum.excursion); - Navigator.of(context).pop(1); + onPressed: () { + saveTemplate(getFieldsText(), DatabasesEnum.excursion); + Navigator.of(context).pop(1); }, child: Text(AppLocalizations.of(context)!.leaveAndSaveTemplate), ), @@ -644,64 +673,5 @@ class _ExcursionMainState extends State { ), ); } - - // Add a refresh position button next to the tracking button - Widget _buildTrackingButtons() { - return FutureBuilder( - future: _positionFuture, - builder: (context, snapshot) { - final bool isLoading = - snapshot.connectionState == ConnectionState.waiting && - !snapshot.hasData; - final Position position = - snapshot.data ?? currentPosition ?? _getDefaultPosition(); - - return Row( - children: [ - Expanded( - child: ElevatedButton( - onPressed: isLoading - ? null - : () async { - await Navigator.push(context, MaterialPageRoute( - builder: (context) { - return Tracking( - weg: rmap["Weg"]!["controller"]!, - startPosition: position, - ); - }, - )); - setState(() {}); - }, - child: isLoading - ? Row( - mainAxisSize: MainAxisSize.min, - children: [ - SizedBox( - width: 16, - height: 16, - child: CircularProgressIndicator( - strokeWidth: 2, - color: Theme.of(context).colorScheme.onPrimary, - ), - ), - const SizedBox(width: 8), - Text(AppLocalizations.of(context)! - .trackingAnAusschalten), - ], - ) - : Text(AppLocalizations.of(context)!.trackingAnAusschalten), - ), - ), - const SizedBox(width: 8), - IconButton( - onPressed: isLoading ? null : _refreshPosition, - icon: const Icon(Icons.refresh), - tooltip: 'Position aktualisieren', - ), - ], - ); - }, - ); - } } +