import 'dart:async'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:geolocator/geolocator.dart'; import 'package:rogapp/widgets/debug_widget.dart'; class LocationController extends GetxController { // Reactive variable to hold the current position Rx currentPosition = Rx(null); // Subscription to the position stream StreamSubscription? positionStream; bool isStreamPaused = false; @override void onInit() { super.onInit(); // Start listening to location updates when the controller is initialized startPositionStream(); } void startPositionStream() async { // Check for location service and permissions before starting the stream bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // Use GetX's context to show a dialog Get.dialog( AlertDialog( title: const Text('Location Services Disabled'), content: const Text( 'Please enable location services to continue using the app.'), actions: [ TextButton( child: const Text('OK'), onPressed: () { // Close the dialog Get.back(); // Optionally, you can direct the user to the settings page // Geolocator.openLocationSettings(); }, ), ], ), barrierDismissible: false, // User must tap button to close dialog ); return; } LocationPermission permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { // Show a dialog if permissions are still denied Get.dialog( AlertDialog( title: const Text('Location Permission Denied'), content: const Text( 'This app requires location permissions to function properly. Please enable location permissions in your device settings.'), actions: [ TextButton( child: const Text('OK'), onPressed: () { // Close the dialog Get.back(); // Optionally, direct the user to the app settings // Geolocator.openAppSettings(); }, ), ], ), barrierDismissible: false, ); return; } } if (permission == LocationPermission.deniedForever) { // Show a dialog if permissions are permanently denied Get.dialog( AlertDialog( title: const Text('Location Permission Needed'), content: const Text( 'Location permissions have been permanently denied. Please open app settings to enable location permissions.'), actions: [ TextButton( child: const Text('Open Settings'), onPressed: () { // Close the dialog and open app settings Get.back(); Geolocator.openAppSettings(); }, ), ], ), barrierDismissible: false, ); return; } // Set up the location options const locationOptions = LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 10); // Start listening to the position stream // positionStream = // Geolocator.getPositionStream(locationSettings: locationOptions).listen( // (Position position) { // currentPosition.value = position; // }, onError: (e) { // // Handle errors or exceptions in the stream // // You might want to log this error or use a state to show error messages // }); StreamSubscription positionStream = Geolocator.getPositionStream(locationSettings: locationOptions) .listen((Position? position) { // print(position == null // ? 'Unknown' // : '${position.latitude.toString()}, ${position.longitude.toString()}'); if (position?.accuracy != null && position!.accuracy <= 15) { currentPosition.value = position; } // LogManager().addLog( // "GPS : ${position!.latitude.toString()}, ${position.longitude.toString()} - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}"); }); // Resume stream if it was paused previously if (isStreamPaused) { isStreamPaused = false; positionStream.resume(); } } // Method to stop the position stream void stopPositionStream() { if (positionStream != null) { positionStream!.pause(); isStreamPaused = true; } } // Method to resume the position stream void resumePositionStream() { if (positionStream != null && isStreamPaused) { positionStream!.resume(); isStreamPaused = false; } } @override void onClose() { // Cancel the position stream subscription when the controller is closed positionStream?.cancel(); super.onClose(); } }