You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_Patient_App/lib/uitl/location_util.dart

188 lines
7.4 KiB
Dart

5 years ago
import 'dart:io';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
2 years ago
import 'package:diplomaticquarterapp/services/permission/permission_service.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:geolocator/geolocator.dart';
5 years ago
import 'package:google_maps_flutter/google_maps_flutter.dart';
2 years ago
import 'package:huawei_location/huawei_location.dart';
5 years ago
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
class LocationUtils {
AppSharedPreferences sharedPref = new AppSharedPreferences();
bool isShowConfirmDialog;
BuildContext context;
bool isHuawei;
final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
2 years ago
LocationUtils({required this.isShowConfirmDialog, required this.context, this.isHuawei = false});
2 years ago
void getCurrentLocation({Function(LatLng)? callBack}) async {
if (Platform.isAndroid && isHuawei) {
2 years ago
_getHMSCurrentLocation(callBack!);
} else {
3 years ago
Geolocator.isLocationServiceEnabled().then((value) async {
5 years ago
if (value) {
3 years ago
await Geolocator.checkPermission().then((permission) async {
if (permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
3 years ago
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low, timeLimit: Duration(seconds: 10)).then((value) {
setLocation(value);
if (callBack != null) callBack(LatLng(value.latitude, value.longitude));
5 years ago
});
}
if (permission == LocationPermission.denied || permission == LocationPermission.deniedForever) {
if (Platform.isAndroid) {
2 years ago
Utils.showPermissionConsentDialog(context, TranslationBase.of(context).locationPermissionDialog, () async {
final hasPermission = await _handlePermission();
if (hasPermission) {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 10)).then((value) {
setLocation(value);
if (callBack != null) callBack(LatLng(value.latitude, value.longitude));
});
} else {
if (isShowConfirmDialog) showErrorLocationDialog(false);
}
});
} else {
2 years ago
if (await Permission.location.request().isGranted) {
3 years ago
getCurrentLocation(callBack: callBack);
} else {
setZeroLocation();
if (isShowConfirmDialog) showErrorLocationDialog(false);
}
5 years ago
}
5 years ago
}
}).catchError((err) {
print(err);
});
} else {
if (isShowConfirmDialog) showErrorLocationDialog(false);
}
}).catchError((err) {
print(err);
});
}
}
Future<bool> _handlePermission() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await _geolocatorPlatform.isLocationServiceEnabled();
if (!serviceEnabled) {
return false;
}
permission = await _geolocatorPlatform.checkPermission();
if (permission == LocationPermission.denied) {
permission = await _geolocatorPlatform.requestPermission();
if (permission == LocationPermission.denied) {
return false;
}
}
if (permission == LocationPermission.deniedForever) {
return false;
}
return true;
}
2 years ago
late LocationCallback _locationCallback;
_getHMSCurrentLocation(Function(LatLng) callBack) async {
2 years ago
PermissionStatus permissionHandler = await Permission.location.request();
// print(statuses[Permission.location]);
5 years ago
int _locationUpdateCbId = 0;
doIt() {
5 years ago
FusedLocationProviderClient locationService = FusedLocationProviderClient();
LocationRequest locationRequest = LocationRequest();
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
locationRequest.interval = 1000;
List<LocationRequest> locationRequestList = <LocationRequest>[locationRequest];
LocationSettingsRequest locationSettingsRequest = LocationSettingsRequest(requests: locationRequestList);
locationService.checkLocationSettings(locationSettingsRequest).then((settings) async {
_locationUpdateCbId = await locationService.requestLocationUpdatesCb(
locationRequest,
LocationCallback(onLocationResult: (locationResult) {
2 years ago
Location location = locationResult.lastLocation!;
locationService.removeLocationUpdatesCb(_locationUpdateCbId);
2 years ago
callBack(LatLng(location.latitude!, location.longitude!));
2 years ago
setLocation(
Position(
2 years ago
latitude: location.latitude!,
longitude: location.longitude!,
altitude: location.altitude!,
timestamp: DateTime.now(),
2 years ago
accuracy: 1.0,
heading: 0.0,
speed: 0.0,
speedAccuracy: 1,
Merge branch 'development_v3.3' of http://34.17.52.79/Haroon6138/diplomatic-quarter into dev_v3.13.6 # Conflicts: # lib/config/config.dart # lib/core/model/labs/patient_lab_orders.dart # lib/core/model/labs/request_patient_lab_special_result.dart # lib/core/model/labs/request_send_lab_report_email.dart # lib/core/model/radiology/final_radiology.dart # lib/core/model/radiology/request_send_rad_report_email.dart # lib/core/model/rate/appoitment_rated.dart # lib/core/service/client/base_app_client.dart # lib/core/service/medical/labs_service.dart # lib/core/service/medical/radiology_service.dart # lib/core/viewModels/medical/labs_view_model.dart # lib/core/viewModels/medical/radiology_view_model.dart # lib/models/Authentication/check_activation_code_request.dart # lib/models/FamilyFiles/GetAllSharedRecordsByStatusReq.dart # lib/pages/BookAppointment/BookSuccess.dart # lib/pages/BookAppointment/QRCode.dart # lib/pages/DrawerPages/family/my-family.dart # lib/pages/DrawerPages/notifications/notification_details_page.dart # lib/pages/MyAppointments/AppointmentDetails.dart # lib/pages/MyAppointments/MyAppointments.dart # lib/pages/MyAppointments/widgets/AppointmentActions.dart # lib/pages/ToDoList/ToDo.dart # lib/pages/landing/landing_page.dart # lib/pages/livecare/widgets/clinic_list.dart # lib/pages/login/confirm-login.dart # lib/pages/login/login.dart # lib/pages/medical/labs/laboratory_result_page.dart # lib/pages/medical/radiology/radiology_details_page.dart # lib/services/authentication/auth_provider.dart # lib/splashPage.dart # lib/uitl/push-notification-handler.dart # lib/widgets/drawer/app_drawer_widget.dart
2 years ago
// altitudeAccuracy: 0.0,
// headingAccuracy: 0.0,
2 years ago
// altitudeAccuracy: 0,
// headingAccuracy: 0,
2 years ago
// Added by Aamir
),
);
}, onLocationAvailability: (locationAvailability) {
4 years ago
print("onLocationAvailability: $locationAvailability");
}));
}).catchError((error) {
if (error.code == "LOCATION_SETTINGS_NOT_AVAILABLE") {
5 years ago
// Location service not enabled.
}
});
}
2 years ago
if (await permissionHandler.isGranted) {
5 years ago
doIt();
} else {
5 years ago
bool has = await requestPermissions();
if (has)
5 years ago
doIt();
else if (isShowConfirmDialog) showErrorLocationDialog(false);
5 years ago
}
}
showErrorLocationDialog(bool isPermissionError) {
ConfirmDialog dialog = new ConfirmDialog(
context: context,
2 years ago
confirmMessage: TranslationBase.of(context).locationDialogMessage,
okText: TranslationBase.of(context).confirm,
cancelText: TranslationBase.of(context).cancel_nocaps,
okFunction: () => {ConfirmDialog.closeAlertDialog(context), if (isPermissionError) Geolocator.openAppSettings() else Geolocator.openLocationSettings(), Navigator.of(context).canPop()},
cancelFunction: () => {});
return dialog.showAlertDialog(context);
}
void setLocation(Position position) {
this.sharedPref.setDouble(USER_LAT, position != null ? position.latitude : 0.0);
this.sharedPref.setDouble(USER_LONG, position != null ? position.longitude : 0.0);
ProjectViewModel projectViewModel = Provider.of(context, listen: false);
projectViewModel.setLatitudeLongitude(position.latitude, position.longitude);
}
void setZeroLocation() {
this.sharedPref.setDouble(USER_LAT, 0.0);
this.sharedPref.setDouble(USER_LONG, 0.0);
}
5 years ago
Future<bool> requestPermissions() async {
var result = await [
Permission.location,
].request();
2 years ago
return (result[Permission.location]!.isGranted || result[Permission.locationAlways]!.isGranted);
5 years ago
}
}