From 5c88802d386dc488301deda00c08de04f30c4212 Mon Sep 17 00:00:00 2001 From: Kush Choudhary Date: Tue, 22 Apr 2025 12:34:28 +0530 Subject: [PATCH] fixed location permission related issues --- .../add_or_update_alarm_controller.dart | 15 +-- .../views/location_activity_tile.dart | 113 ++++++++++-------- .../addOrUpdateAlarm/views/weather_tile.dart | 96 ++++++++------- 3 files changed, 121 insertions(+), 103 deletions(-) diff --git a/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart b/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart index baf81538..587ad432 100644 --- a/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart +++ b/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart @@ -353,7 +353,8 @@ class AddOrUpdateAlarmController extends GetxController { // Cannot request runtime permission because location permission // is denied forever. return false; - } else if (locationPermission == LocationPermission.denied) { + } else if (locationPermission == LocationPermission.denied || + locationPermission == LocationPermission.whileInUse) { bool? shouldAskPermission = await Get.defaultDialog( backgroundColor: themeController.secondaryBackgroundColor.value, barrierDismissible: false, @@ -365,7 +366,7 @@ class AddOrUpdateAlarmController extends GetxController { color: themeController.primaryTextColor.value, ), content: const Text( - 'To ensure timely alarm dismissal, this app requires access to your location. Your location will be accessed in the background at the scheduled alarm time.', + 'To ensure timely alarm dismissal, this app requires access to your location. Please select Allow all the time. Your location will only be accessed in the background at the scheduled alarm time.', ), actions: [ TextButton( @@ -398,17 +399,13 @@ class AddOrUpdateAlarmController extends GetxController { // User declined the permission request. return false; } - // Ask the user for location permission. + // Ask the user for location permission (must be LocationPermission.always). locationPermission = await FlLocation.requestLocationPermission(); if (locationPermission == LocationPermission.denied || - locationPermission == LocationPermission.deniedForever) return false; + locationPermission == LocationPermission.deniedForever || + locationPermission == LocationPermission.whileInUse) return false; } - // Location permission must always be allowed (LocationPermission.always) - // to collect location data in the background. - if (background == true && - locationPermission == LocationPermission.whileInUse) return false; - // Location services has been enabled and permission have been granted. return true; } diff --git a/lib/app/modules/addOrUpdateAlarm/views/location_activity_tile.dart b/lib/app/modules/addOrUpdateAlarm/views/location_activity_tile.dart index 414dafb3..11a83312 100644 --- a/lib/app/modules/addOrUpdateAlarm/views/location_activity_tile.dart +++ b/lib/app/modules/addOrUpdateAlarm/views/location_activity_tile.dart @@ -98,60 +98,71 @@ class LocationTile extends StatelessWidget { if (value == 0) { controller.isLocationEnabled.value = false; } else if (value == 1) { - Get.defaultDialog( - backgroundColor: themeController.secondaryBackgroundColor.value, - title: 'Set location to automatically cancel alarm!', - titleStyle: Theme.of(context).textTheme.bodyMedium, - content: Column( - children: [ - SizedBox( - height: height * 0.65, - width: width * 0.92, - child: FlutterMap( - mapController: controller.mapController, - options: MapOptions( - onTap: (tapPosition, point) { - controller.selectedPoint.value = point; - }, - // screenSize: Size(width * 0.3, height * 0.8), - center: controller.selectedPoint.value, - zoom: 15, - ), - children: [ - TileLayer( - urlTemplate: - 'https://{s}tile.openstreetmap.org/{z}/{x}/{y}.png', + if(await controller.checkAndRequestPermission()){ + Get.defaultDialog( + backgroundColor: themeController.secondaryBackgroundColor.value, + title: 'Set location to automatically cancel alarm!', + titleStyle: Theme.of(context).textTheme.bodyMedium, + content: Column( + children: [ + SizedBox( + height: height * 0.65, + width: width * 0.92, + child: FlutterMap( + mapController: controller.mapController, + options: MapOptions( + onTap: (tapPosition, point) { + controller.selectedPoint.value = point; + }, + // screenSize: Size(width * 0.3, height * 0.8), + center: controller.selectedPoint.value, + zoom: 15, ), - Obx(() => MarkerLayer( - markers: - List.from(controller.markersList))), - ], - ), - ), - const SizedBox(height: 10), - TextButton( - style: ButtonStyle( - backgroundColor: MaterialStateProperty.all(kprimaryColor), - ), - child: Text( - 'Save', - style: Theme.of(context).textTheme.displaySmall!.copyWith( - color: themeController.secondaryTextColor.value, + children: [ + TileLayer( + urlTemplate: + 'https://{s}tile.openstreetmap.org/{z}/{x}/{y}.png', ), + Obx(() => MarkerLayer( + markers: + List.from(controller.markersList))), + ], + ), ), - onPressed: () { - Utils.hapticFeedback(); - Get.back(); - controller.isLocationEnabled.value = true; - }, - ), - ], - ), - ); - - if (controller.isLocationEnabled.value == false) { - await controller.getLocation(); - controller.mapController.move(controller.selectedPoint.value, 15); + const SizedBox(height: 10), + TextButton( + style: ButtonStyle( + backgroundColor: MaterialStateProperty.all(kprimaryColor), + ), + child: Text( + 'Save', + style: Theme.of(context).textTheme.displaySmall!.copyWith( + color: themeController.secondaryTextColor.value, + ), + ), + onPressed: () { + Utils.hapticFeedback(); + Get.back(); + controller.isLocationEnabled.value = true; + }, + ), + ], + ), + ); + + if (controller.isLocationEnabled.value == false) { + await controller.getLocation(); + controller.mapController.move(controller.selectedPoint.value, 15); + } + } + else { + Get.defaultDialog( + titlePadding: const EdgeInsets.symmetric(vertical: 20), + backgroundColor: themeController.secondaryBackgroundColor.value, + title: 'Location Permission Denied!'.tr, + titleStyle: Theme.of(context).textTheme.displaySmall, + content: const Text('Please provide all time location access to use this feature.', textAlign: TextAlign.center,), + ); } } }); diff --git a/lib/app/modules/addOrUpdateAlarm/views/weather_tile.dart b/lib/app/modules/addOrUpdateAlarm/views/weather_tile.dart index fd80c1c9..a9ba1693 100644 --- a/lib/app/modules/addOrUpdateAlarm/views/weather_tile.dart +++ b/lib/app/modules/addOrUpdateAlarm/views/weather_tile.dart @@ -24,50 +24,60 @@ class WeatherTile extends StatelessWidget { child: ListTile( onTap: () async { Utils.hapticFeedback(); - await controller.checkAndRequestPermission(); - Get.defaultDialog( - titlePadding: const EdgeInsets.symmetric(vertical: 20), - backgroundColor: themeController.secondaryBackgroundColor.value, - title: 'Select weather types'.tr, - titleStyle: Theme.of(context).textTheme.displaySmall, - content: SingleChildScrollView( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - WeatherOption( - type: WeatherTypes.sunny, - label: 'Sunny', - controller: controller, - themeController: themeController, - ), - WeatherOption( - type: WeatherTypes.cloudy, - label: 'Cloudy', - controller: controller, - themeController: themeController, - ), - WeatherOption( - type: WeatherTypes.rainy, - label: 'Rainy', - controller: controller, - themeController: themeController, - ), - WeatherOption( - type: WeatherTypes.windy, - label: 'Windy', - controller: controller, - themeController: themeController, - ), - WeatherOption( - type: WeatherTypes.stormy, - label: 'Stormy', - controller: controller, - themeController: themeController, - ), - ], + if(await controller.checkAndRequestPermission()){ + Get.defaultDialog( + titlePadding: const EdgeInsets.symmetric(vertical: 20), + backgroundColor: themeController.secondaryBackgroundColor.value, + title: 'Select weather types'.tr, + titleStyle: Theme.of(context).textTheme.displaySmall, + content: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + WeatherOption( + type: WeatherTypes.sunny, + label: 'Sunny', + controller: controller, + themeController: themeController, + ), + WeatherOption( + type: WeatherTypes.cloudy, + label: 'Cloudy', + controller: controller, + themeController: themeController, + ), + WeatherOption( + type: WeatherTypes.rainy, + label: 'Rainy', + controller: controller, + themeController: themeController, + ), + WeatherOption( + type: WeatherTypes.windy, + label: 'Windy', + controller: controller, + themeController: themeController, + ), + WeatherOption( + type: WeatherTypes.stormy, + label: 'Stormy', + controller: controller, + themeController: themeController, + ), + ], + ), ), - ), - ); + ); + } + else { + Get.defaultDialog( + titlePadding: const EdgeInsets.symmetric(vertical: 20), + backgroundColor: themeController.secondaryBackgroundColor.value, + title: 'Location Permission Denied!'.tr, + titleStyle: Theme.of(context).textTheme.displaySmall, + content: const Text('Please provide all time location access to use this feature.', textAlign: TextAlign.center,), + ); + } }, title: Row( children: [