112 lines
3.7 KiB
Dart
112 lines
3.7 KiB
Dart
import 'dart:async';
|
|
import 'package:SEFER/constant/box_name.dart';
|
|
import 'package:SEFER/controller/auth/captin/login_captin_controller.dart';
|
|
import 'package:background_location/background_location.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import '../../main.dart';
|
|
|
|
class LocationBackgroundController extends GetxController {
|
|
@override
|
|
Future<void> onInit() async {
|
|
super.onInit();
|
|
await requestLocationPermission();
|
|
await configureBackgroundLocation();
|
|
}
|
|
|
|
Future<void> requestLocationPermission() async {
|
|
var status = await Permission.locationAlways.status;
|
|
print('Initial status: $status');
|
|
|
|
if (status == PermissionStatus.denied ||
|
|
status == PermissionStatus.restricted) {
|
|
// Show dialog to inform the driver about background GPS location usage
|
|
await Get.dialog(
|
|
AlertDialog(
|
|
title: Text('Location Permission'.tr),
|
|
content: Text(
|
|
'We use GPS location in the background to enable you to receive orders.'
|
|
.tr),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () async {
|
|
Get.back(); // Close the dialog
|
|
// Request permission
|
|
status = await Permission.locationAlways.request();
|
|
print('Requested status: $status');
|
|
_handlePermissionStatus(status);
|
|
},
|
|
child: Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
} else {
|
|
_handlePermissionStatus(status);
|
|
}
|
|
}
|
|
|
|
void _handlePermissionStatus(PermissionStatus status) async {
|
|
status = await Permission.locationAlways.status;
|
|
if (!status.isGranted) {
|
|
// Open app settings if permission is permanently denied
|
|
openAppSettings();
|
|
box.write(BoxName.locationPermission, 'true');
|
|
Get.find<LoginCaptinController>().update();
|
|
} else if (status.isGranted) {
|
|
// Permission granted
|
|
box.write(BoxName.locationPermission, 'true');
|
|
Get.find<LoginCaptinController>().update();
|
|
}
|
|
}
|
|
|
|
Future<void> configureBackgroundLocation() async {
|
|
await BackgroundLocation.setAndroidNotification(
|
|
title: "Sefer Driver ",
|
|
message: "Tracking location...",
|
|
icon: "app_icon",
|
|
);
|
|
|
|
BackgroundLocation.setAndroidConfiguration(1000);
|
|
BackgroundLocation.startLocationService();
|
|
BackgroundLocation.getLocationUpdates((location) {
|
|
// Handle location updates here
|
|
print("New location: ${location.latitude}, ${location.longitude}");
|
|
});
|
|
}
|
|
|
|
startBackLocation() async {
|
|
Timer.periodic(const Duration(seconds: 5), (timer) {
|
|
getBackgroundLocation();
|
|
});
|
|
}
|
|
|
|
getBackgroundLocation() async {
|
|
var status = await Permission.locationAlways.status;
|
|
if (status.isGranted) {
|
|
await BackgroundLocation.startLocationService(
|
|
distanceFilter: 20, forceAndroidLocationManager: true);
|
|
BackgroundLocation.setAndroidConfiguration(
|
|
Duration.microsecondsPerSecond); // Set interval to 5 seconds
|
|
|
|
BackgroundLocation.getLocationUpdates((location1) {
|
|
print('''\n
|
|
Latitude: ${location1.latitude.toString()}
|
|
Longitude: ${location1.longitude.toString()}
|
|
Altitude: ${location1.altitude.toString()}
|
|
Accuracy: ${location1.accuracy.toString()}
|
|
Bearing: ${location1.bearing.toString()}
|
|
Speed: ${location1.speed.toString()}
|
|
|
|
|
|
''');
|
|
});
|
|
} else {
|
|
await Permission.locationAlways.request();
|
|
print('Location permission not granted');
|
|
}
|
|
}
|
|
}
|