|  |  |  | @ -1,12 +1,23 @@ | 
		
	
		
			
				|  |  |  |  | import 'dart:io'; | 
		
	
		
			
				|  |  |  |  | import 'dart:ui'; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import 'package:geolocator/geolocator.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:gms_check/gms_check.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:google_maps_flutter/google_maps_flutter.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:hmg_patient_app_new/core/app_state.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:hmg_patient_app_new/core/cache_consts.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:hmg_patient_app_new/core/utils/utils.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:hmg_patient_app_new/services/navigation_service.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:permission_handler/permission_handler.dart'; | 
		
	
		
			
				|  |  |  |  | import 'package:huawei_location/huawei_location.dart' as HmsLocation | 
		
	
		
			
				|  |  |  |  |     show | 
		
	
		
			
				|  |  |  |  |         FusedLocationProviderClient, | 
		
	
		
			
				|  |  |  |  |         Location, | 
		
	
		
			
				|  |  |  |  |         LocationSettingsRequest, | 
		
	
		
			
				|  |  |  |  |         LocationRequest; | 
		
	
		
			
				|  |  |  |  | import 'package:location/location.dart' | 
		
	
		
			
				|  |  |  |  |     show Location, PermissionStatus, LocationData; | 
		
	
		
			
				|  |  |  |  | import 'package:permission_handler/permission_handler.dart' | 
		
	
		
			
				|  |  |  |  |     show Permission, PermissionListActions, PermissionStatusGetters; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | class LocationUtils { | 
		
	
		
			
				|  |  |  |  |   NavigationService navigationService; | 
		
	
	
		
			
				
					|  |  |  | @ -16,6 +27,7 @@ class LocationUtils { | 
		
	
		
			
				|  |  |  |  |   bool isShowLocationTimeoutDialog; | 
		
	
		
			
				|  |  |  |  |   bool isHuawei; | 
		
	
		
			
				|  |  |  |  |   final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance; | 
		
	
		
			
				|  |  |  |  |   Future<bool?>? isGMSDevice; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   LocationUtils({ | 
		
	
		
			
				|  |  |  |  |     required this.isShowConfirmDialog, | 
		
	
	
		
			
				
					|  |  |  | @ -23,49 +35,62 @@ class LocationUtils { | 
		
	
		
			
				|  |  |  |  |     required this.appState, | 
		
	
		
			
				|  |  |  |  |     this.isHuawei = false, | 
		
	
		
			
				|  |  |  |  |     this.isShowLocationTimeoutDialog = true, | 
		
	
		
			
				|  |  |  |  |   }); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   void getCurrentLocation({Function(LatLng)? callBack}) async { | 
		
	
		
			
				|  |  |  |  |     Geolocator.isLocationServiceEnabled().then((value) async { | 
		
	
		
			
				|  |  |  |  |       if (value) { | 
		
	
		
			
				|  |  |  |  |         await Geolocator.checkPermission().then((permission) async { | 
		
	
		
			
				|  |  |  |  |           if (permission == LocationPermission.always || permission == LocationPermission.whileInUse) { | 
		
	
		
			
				|  |  |  |  |             Geolocator.getLastKnownPosition().then((value) { | 
		
	
		
			
				|  |  |  |  |               setLocation(value); | 
		
	
		
			
				|  |  |  |  |               if (callBack != null) callBack(LatLng(value?.latitude ?? 24.7101433, value?.longitude ?? 46.6757709)); | 
		
	
		
			
				|  |  |  |  |             }).catchError((err) { | 
		
	
		
			
				|  |  |  |  |               if (isShowConfirmDialog && isShowLocationTimeoutDialog) {} | 
		
	
		
			
				|  |  |  |  |             }); | 
		
	
		
			
				|  |  |  |  |           } | 
		
	
		
			
				|  |  |  |  |   }) { | 
		
	
		
			
				|  |  |  |  |     isGMSDevice = GmsCheck().checkGmsAvailability(); | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |           if (permission == LocationPermission.denied || permission == LocationPermission.deniedForever) { | 
		
	
		
			
				|  |  |  |  |             if (Platform.isAndroid) { | 
		
	
		
			
				|  |  |  |  |               // Utils.showPermissionConsentDialog(context, TranslationBase.of(context).locationPermissionDialog, () async { | 
		
	
		
			
				|  |  |  |  |               final hasPermission = await _handlePermission(); | 
		
	
		
			
				|  |  |  |  |               if (hasPermission) { | 
		
	
		
			
				|  |  |  |  |                 // Geolocator.getCurrentPosition(locationSettings: LocationSettings(accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5))).then((value) { | 
		
	
		
			
				|  |  |  |  |                 Geolocator.getLastKnownPosition().then((value) { | 
		
	
		
			
				|  |  |  |  |                   setLocation(value); | 
		
	
		
			
				|  |  |  |  |                   if (callBack != null) callBack(LatLng(value?.latitude ?? 24.7101433, value?.longitude ?? 46.6757709)); | 
		
	
		
			
				|  |  |  |  |                 }); | 
		
	
		
			
				|  |  |  |  |               } else { | 
		
	
		
			
				|  |  |  |  |                 // if (isShowConfirmDialog) showErrorLocationDialog(false, failureCallBack: () {}); | 
		
	
		
			
				|  |  |  |  |               } | 
		
	
		
			
				|  |  |  |  |               // }); | 
		
	
		
			
				|  |  |  |  |             } else { | 
		
	
		
			
				|  |  |  |  |               if (await Permission.location.request().isGranted) { | 
		
	
		
			
				|  |  |  |  |                 getCurrentLocation(callBack: callBack); | 
		
	
		
			
				|  |  |  |  |               } else { | 
		
	
		
			
				|  |  |  |  |                 setZeroLocation(); | 
		
	
		
			
				|  |  |  |  |                 if (isShowConfirmDialog) showErrorLocationDialog(false, failureCallBack: () {}); | 
		
	
		
			
				|  |  |  |  |               } | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |           } | 
		
	
		
			
				|  |  |  |  |         }).catchError((err) {}); | 
		
	
		
			
				|  |  |  |  |       } else { | 
		
	
		
			
				|  |  |  |  |         if (isShowConfirmDialog) showErrorLocationDialog(false, failureCallBack: () {}); | 
		
	
		
			
				|  |  |  |  |   void getLocation( | 
		
	
		
			
				|  |  |  |  |       {Function(LatLng)? onSuccess, VoidCallback? onFailure}) async { | 
		
	
		
			
				|  |  |  |  |     if (Platform.isIOS) { | 
		
	
		
			
				|  |  |  |  |       getCurrentLocation(onFailure: onFailure, onSuccess: onSuccess); | 
		
	
		
			
				|  |  |  |  |       return; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if (await isGMSDevice ?? true) { | 
		
	
		
			
				|  |  |  |  |       getCurrentLocation(onFailure: onFailure, onSuccess: onSuccess); | 
		
	
		
			
				|  |  |  |  |       return; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     getHMSLocation(onFailure: onFailure, onSuccess: onSuccess); | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   void getCurrentLocation( | 
		
	
		
			
				|  |  |  |  |       {Function(LatLng)? onSuccess, VoidCallback? onFailure}) async { | 
		
	
		
			
				|  |  |  |  |     var location = Location(); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     bool isLocationEnabled = await location.serviceEnabled(); | 
		
	
		
			
				|  |  |  |  |     //if the location service is not enabled, ask the user to enable it | 
		
	
		
			
				|  |  |  |  |     if (!isLocationEnabled) { | 
		
	
		
			
				|  |  |  |  |       isLocationEnabled = await location.requestService(); | 
		
	
		
			
				|  |  |  |  |       if (!isLocationEnabled) { | 
		
	
		
			
				|  |  |  |  |         appState.resetLocation(); | 
		
	
		
			
				|  |  |  |  |         onFailure?.call(); | 
		
	
		
			
				|  |  |  |  |         return; | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     LocationPermission permissionGranted = await Geolocator.checkPermission(); | 
		
	
		
			
				|  |  |  |  |     if (permissionGranted == LocationPermission.denied) { | 
		
	
		
			
				|  |  |  |  |       permissionGranted = await Geolocator.requestPermission(); | 
		
	
		
			
				|  |  |  |  |       if (permissionGranted != LocationPermission.whileInUse && | 
		
	
		
			
				|  |  |  |  |           permissionGranted != LocationPermission.always) { | 
		
	
		
			
				|  |  |  |  |         appState.resetLocation(); | 
		
	
		
			
				|  |  |  |  |         onFailure?.call(); | 
		
	
		
			
				|  |  |  |  |         return; | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  |     }).catchError((err) {}); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     Position? currentLocation = await Geolocator.getLastKnownPosition(); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if(currentLocation?.latitude == null || currentLocation?.longitude == null){ | 
		
	
		
			
				|  |  |  |  |       currentLocation = await Geolocator.getCurrentPosition( | 
		
	
		
			
				|  |  |  |  |           desiredAccuracy: LocationAccuracy.low); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     LatLng locationData = LatLng( | 
		
	
		
			
				|  |  |  |  |         currentLocation?.latitude ?? 0.0, currentLocation?.longitude ?? 0.0); | 
		
	
		
			
				|  |  |  |  |     saveLatLngToAppState(locationData); | 
		
	
		
			
				|  |  |  |  |     onSuccess?.call(locationData); | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   Future<bool> checkIfGPSIsEnabled() async { | 
		
	
	
		
			
				
					|  |  |  | @ -166,4 +191,75 @@ class LocationUtils { | 
		
	
		
			
				|  |  |  |  |     ].request(); | 
		
	
		
			
				|  |  |  |  |     return (result[Permission.location]!.isGranted || result[Permission.locationAlways]!.isGranted); | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   void saveLatLngToAppState(LatLng locationData) { | 
		
	
		
			
				|  |  |  |  |     appState.userLat = locationData.latitude; | 
		
	
		
			
				|  |  |  |  |     appState.userLong = locationData.longitude; | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   void getHMSLocation( | 
		
	
		
			
				|  |  |  |  |       {VoidCallback? onFailure, Function(LatLng p1)? onSuccess}) async { | 
		
	
		
			
				|  |  |  |  |     try { | 
		
	
		
			
				|  |  |  |  |       var location = Location(); | 
		
	
		
			
				|  |  |  |  |       HmsLocation.FusedLocationProviderClient locationService = | 
		
	
		
			
				|  |  |  |  |           HmsLocation.FusedLocationProviderClient()..initFusedLocationService(); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |       bool isLocationEnabled = await Geolocator.isLocationServiceEnabled(); | 
		
	
		
			
				|  |  |  |  |       //if the location service is not enabled, ask the user to enable it | 
		
	
		
			
				|  |  |  |  |       if (!isLocationEnabled) { | 
		
	
		
			
				|  |  |  |  |         HmsLocation.LocationRequest locationRequest = | 
		
	
		
			
				|  |  |  |  |             HmsLocation.LocationRequest() | 
		
	
		
			
				|  |  |  |  |               ..priority = HmsLocation.LocationRequest.PRIORITY_HIGH_ACCURACY; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         HmsLocation.LocationSettingsRequest request = | 
		
	
		
			
				|  |  |  |  |             HmsLocation.LocationSettingsRequest( | 
		
	
		
			
				|  |  |  |  |           alwaysShow: true, | 
		
	
		
			
				|  |  |  |  |           needBle: false, | 
		
	
		
			
				|  |  |  |  |           requests: [locationRequest], | 
		
	
		
			
				|  |  |  |  |         ); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         await locationService.checkLocationSettings(request); | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  |       LocationPermission permissionGranted = await Geolocator.checkPermission(); | 
		
	
		
			
				|  |  |  |  |       if (permissionGranted == LocationPermission.denied) { | 
		
	
		
			
				|  |  |  |  |         permissionGranted = await Geolocator.requestPermission(); | 
		
	
		
			
				|  |  |  |  |         if (permissionGranted != LocationPermission.whileInUse && | 
		
	
		
			
				|  |  |  |  |             permissionGranted != LocationPermission.always) { | 
		
	
		
			
				|  |  |  |  |           appState.resetLocation(); | 
		
	
		
			
				|  |  |  |  |           onFailure?.call(); | 
		
	
		
			
				|  |  |  |  |           return; | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |       HmsLocation.Location data = await locationService.getLastLocation(); | 
		
	
		
			
				|  |  |  |  |       if (data.latitude == null || data.longitude == null) { | 
		
	
		
			
				|  |  |  |  |         appState.resetLocation(); | 
		
	
		
			
				|  |  |  |  |         HmsLocation.LocationRequest request = HmsLocation.LocationRequest() | 
		
	
		
			
				|  |  |  |  |           ..priority = HmsLocation.LocationRequest.PRIORITY_HIGH_ACCURACY | 
		
	
		
			
				|  |  |  |  |           ..interval = 1000 // 1 second | 
		
	
		
			
				|  |  |  |  |           ..numUpdates = 1; | 
		
	
		
			
				|  |  |  |  |         locationService.requestLocationUpdates(request); | 
		
	
		
			
				|  |  |  |  |         locationService.onLocationData?.listen((location) { | 
		
	
		
			
				|  |  |  |  |           data = location; | 
		
	
		
			
				|  |  |  |  |           if (data.latitude == null || data.longitude == null) { | 
		
	
		
			
				|  |  |  |  |             appState.resetLocation(); | 
		
	
		
			
				|  |  |  |  |             onFailure?.call(); | 
		
	
		
			
				|  |  |  |  |             return; | 
		
	
		
			
				|  |  |  |  |           } | 
		
	
		
			
				|  |  |  |  |           var locationData = | 
		
	
		
			
				|  |  |  |  |               LatLng(data.latitude ?? 0.0, data.longitude ?? 0.0); | 
		
	
		
			
				|  |  |  |  |           saveLatLngToAppState(locationData); | 
		
	
		
			
				|  |  |  |  |           onSuccess?.call(locationData); | 
		
	
		
			
				|  |  |  |  |         }); | 
		
	
		
			
				|  |  |  |  |       } else { | 
		
	
		
			
				|  |  |  |  |         var locationData = LatLng(data.latitude ?? 0.0, data.longitude ?? 0.0); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         saveLatLngToAppState(locationData); | 
		
	
		
			
				|  |  |  |  |         onSuccess?.call(locationData); | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  |     } catch (e) { | 
		
	
		
			
				|  |  |  |  |       appState.resetLocation(); | 
		
	
		
			
				|  |  |  |  |       onFailure?.call(); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | } | 
		
	
	
		
			
				
					|  |  |  | 
 |