feat : permission and location service sensor on\off in inspection feature

This commit is contained in:
2025-04-30 12:48:55 +03:30
parent 4d94e576b9
commit 5770116def
6 changed files with 191 additions and 47 deletions

View File

@@ -1,16 +1,83 @@
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:inspection/presentation/action/view.dart';
import 'package:inspection/presentation/filter/view.dart';
import 'package:inspection/presentation/profile/view.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_core/data/utils.dart';
enum ErrorLocationType { serviceDisabled, permissionDenied, none }
class RootLogic extends GetxController {
RxInt currentIndex = 0.obs;
List<Widget> pages = [SupervisionFilterPage(), ActionPage(), ProfilePage()];
RxList<ErrorLocationType> errorLocationType = RxList();
Stream<bool> listenToLocationServiceStatus() {
return Geolocator.getServiceStatusStream().map((status) {
return status == ServiceStatus.enabled;
});
}
Future<bool> locationServiceEnabled() async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return false;
}
return true;
}
Future<bool> checkPermission({bool request = false}) async {
try {
final LocationPermission permission = await Geolocator.checkPermission();
switch (permission) {
case LocationPermission.denied:
final LocationPermission requestResult = await Geolocator.requestPermission();
return requestResult != LocationPermission.denied &&
requestResult != LocationPermission.deniedForever;
case LocationPermission.deniedForever:
return request ? await Geolocator.openAppSettings() : false;
case LocationPermission.always:
case LocationPermission.whileInUse:
return true;
default:
return false;
}
} catch (e) {
eLog(e);
return await Geolocator.openLocationSettings();
}
}
@override
void onReady() {
super.onReady();
locationServiceEnabled().then((value) {
if (!value) {
errorLocationType.add(ErrorLocationType.serviceDisabled);
}
});
checkPermission().then((value) {
if (!value) {
errorLocationType.add(ErrorLocationType.permissionDenied);
}
});
listenToLocationServiceStatus().listen((event) {
if (!event) {
errorLocationType.add(ErrorLocationType.serviceDisabled);
} else {
errorLocationType.remove(ErrorLocationType.serviceDisabled);
}
});
}
void changePage(int index) {

View File

@@ -12,35 +12,135 @@ class RootPage extends GetView<RootLogic> {
return Scaffold(
body: Stack(
children: [
ObxValue((errorType) {
if (errorType.isNotEmpty) {
if (errorType.contains(ErrorLocationType.serviceDisabled)) {
Future.microtask(() {
Get.defaultDialog(
title: 'خطا',
content: const Text('سرویس مکان‌یابی غیرفعال است'),
cancel: ROutlinedElevated(
text: 'بررسی مجدد',
width: 120,
textStyle: AppFonts.yekan16,
onPressed: () async {
var service = await controller.locationServiceEnabled();
eLog(service);
if (service) {
controller.errorLocationType.remove(
ErrorLocationType.serviceDisabled,
);
Get.back();
}
// Don't call Get.back() if service is still disabled
},
),
confirm: RElevated(
text: 'روشن کردن',
textStyle: AppFonts.yekan16,
width: 120,
onPressed: () async {
var res = await Geolocator.openLocationSettings();
if (res) {
var service =
await controller.locationServiceEnabled();
if (service) {
controller.errorLocationType.remove(
ErrorLocationType.serviceDisabled,
);
Get.back();
}
}
},
),
contentPadding: EdgeInsets.all(8),
onWillPop: () async {
return controller.errorLocationType.isEmpty;
},
barrierDismissible: false,
);
});
} else {
Future.microtask(() {
Get.defaultDialog(
title: 'خطا',
content: const Text(
' دسترسی به سرویس مکان‌یابی غیرفعال است',
),
cancel: ROutlinedElevated(
text: 'بررسی مجدد',
width: 120,
textStyle: AppFonts.yekan16,
onPressed: () async {
await controller.checkPermission();
},
),
confirm: RElevated(
text: 'اجازه دادن',
textStyle: AppFonts.yekan16,
width: 120,
onPressed: () async {
var res = await controller.checkPermission(
request: true,
);
if (res) {
controller.errorLocationType.remove(
ErrorLocationType.permissionDenied,
);
Get.back();
}
},
),
contentPadding: EdgeInsets.all(8),
onWillPop: () async {
return controller.errorLocationType.isEmpty;
},
barrierDismissible: false,
);
});
}
}
return const SizedBox.shrink();
}, controller.errorLocationType),
ObxValue(
(currentIndex) => IndexedStack(
index: currentIndex.value,
children: controller.pages,
),
controller.currentIndex,
),
Align(
alignment: Alignment.bottomCenter,
child: WaveBottomNavigation(
items: [
WaveBottomNavigationItem(
title: 'خانه',
icon:Assets.vecMapSvg,
),
WaveBottomNavigationItem(title: 'خانه', icon: Assets.vecMapSvg),
WaveBottomNavigationItem(
title: 'عملیات',
icon:Assets.vecUserSvg,
icon: Assets.vecUserSvg,
),
WaveBottomNavigationItem(
title: 'افزودن',
icon:Assets.vecAddSvg,
icon: Assets.vecAddSvg,
),
WaveBottomNavigationItem(
title: 'آمار',
icon:Assets.vecDiagramSvg,
), WaveBottomNavigationItem(
icon: Assets.vecDiagramSvg,
),
WaveBottomNavigationItem(
title: 'تماس',
icon:Assets.vecCallSvg,
), WaveBottomNavigationItem(
icon: Assets.vecCallSvg,
),
WaveBottomNavigationItem(
title: 'مکان ',
icon:Assets.vecGpsSvg,
), WaveBottomNavigationItem(
icon: Assets.vecGpsSvg,
),
WaveBottomNavigationItem(
title: 'تاریخ',
icon:Assets.vecCalendarSvg,
icon: Assets.vecCalendarSvg,
),
],
onPageChanged: (index) {