temporary update
This commit is contained in:
@ -2,23 +2,87 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
|
||||
class PermissionController {
|
||||
static bool? _locationPermissionGranted;
|
||||
|
||||
static bool _isRequestingPermission = false;
|
||||
static Completer<bool>? _permissionCompleter;
|
||||
/*
|
||||
bool? _isRequestingPermission=false;
|
||||
final Completer<PermissionController> _permissionCompleter = Completer<PermissionController>();
|
||||
*/
|
||||
|
||||
static Future<bool> checkAndRequestPermissions() async {
|
||||
if (_isRequestingPermission) {
|
||||
return _permissionCompleter!.future;
|
||||
static Future<bool> checkAndRequestPermissions_new() async {
|
||||
try {
|
||||
if (_locationPermissionGranted != null) {
|
||||
return _locationPermissionGranted!;
|
||||
}
|
||||
|
||||
var serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
Get.snackbar(
|
||||
'エラー', '位置情報サービスが無効です。設定から有効にしてください。');
|
||||
|
||||
// 位置情報サービスが無効の場合、ユーザーに有効化を促す
|
||||
return false;
|
||||
}
|
||||
|
||||
debugPrint("====> Geolocator.checkPermission");
|
||||
var permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
Get.snackbar('エラー', '位置情報の許可が拒否されました。');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
Get.snackbar('エラー',
|
||||
'位置情報の許可が永久に拒否されました。設定から許可してください。');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
_locationPermissionGranted = true;
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('Error checking permissions: $e');
|
||||
Get.snackbar('エラー', '権限の確認中にエラーが発生しました。');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void resetPermissionCache() {
|
||||
_locationPermissionGranted = null;
|
||||
}
|
||||
|
||||
static Future<bool> _requestLocationPermission() async {
|
||||
BuildContext? context;
|
||||
int attempts = 0;
|
||||
const maxAttempts = 10;
|
||||
|
||||
debugPrint("====> _requestLocationPermission");
|
||||
|
||||
// コンテキストが利用可能になるまで待機するロジック
|
||||
while (Get.context == null && attempts < maxAttempts) {
|
||||
context = Get.context;
|
||||
if (context == null) {
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
attempts++;
|
||||
}
|
||||
}
|
||||
|
||||
_isRequestingPermission = true;
|
||||
_permissionCompleter = Completer<bool>();
|
||||
if (Get.context == null) {
|
||||
print('Context is still null after waiting, cannot proceed with permission check');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
bool hasPermissions = await _checkLocationPermissions();
|
||||
@ -36,18 +100,16 @@ class PermissionController {
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
}
|
||||
return hasPermissions;
|
||||
|
||||
_isRequestingPermission = false;
|
||||
_permissionCompleter!.complete(hasPermissions);
|
||||
} catch (e) {
|
||||
print('Error in permission request: $e');
|
||||
_isRequestingPermission = false;
|
||||
_permissionCompleter!.complete(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
return _permissionCompleter!.future;
|
||||
}
|
||||
|
||||
|
||||
static Future<bool> _checkLocationPermissions() async {
|
||||
final locationPermission = await Permission.location.status;
|
||||
final whenInUsePermission = await Permission.locationWhenInUse.status;
|
||||
@ -118,20 +180,30 @@ class PermissionController {
|
||||
(whenInUsePermission == PermissionStatus.granted || alwaysPermission == PermissionStatus.granted);
|
||||
}
|
||||
|
||||
static Future<bool> checkAndRequestPermissions_old() async {
|
||||
static Future<bool> checkAndRequestPermissions() async {
|
||||
|
||||
/*
|
||||
if (_isRequestingPermission) {
|
||||
return _permissionCompleter!.future;
|
||||
}
|
||||
|
||||
_isRequestingPermission = true;
|
||||
_permissionCompleter = Completer<bool>();
|
||||
*/
|
||||
|
||||
try {
|
||||
if (_locationPermissionGranted != null) {
|
||||
return _locationPermissionGranted!;
|
||||
}
|
||||
|
||||
bool hasPermissions = await _checkLocationPermissions();
|
||||
if (!hasPermissions) {
|
||||
bool userAgreed = await showLocationDisclosure();
|
||||
if (userAgreed) {
|
||||
hasPermissions = await _requestAllLocationPermissions();
|
||||
_locationPermissionGranted = true;
|
||||
debugPrint("Finish checkAndRequestPermissions...");
|
||||
return true;
|
||||
} else {
|
||||
print('User did not agree to location usage');
|
||||
hasPermissions = false;
|
||||
@ -140,15 +212,18 @@ class PermissionController {
|
||||
}
|
||||
}
|
||||
|
||||
_isRequestingPermission = false;
|
||||
_permissionCompleter!.complete(hasPermissions);
|
||||
//_isRequestingPermission = false;
|
||||
//_permissionCompleter!.complete(hasPermissions);
|
||||
|
||||
|
||||
} catch( e ) {
|
||||
print('Error in permission request: $e');
|
||||
_isRequestingPermission = false;
|
||||
_permissionCompleter!.complete(false);
|
||||
//_isRequestingPermission = false;
|
||||
//_permissionCompleter!.complete(false);
|
||||
}
|
||||
debugPrint("Finish checkAndRequestPermissions...");
|
||||
return _permissionCompleter!.future;
|
||||
//return _permissionCompleter!.future;
|
||||
return false;
|
||||
}
|
||||
|
||||
static Future<void> requestAllLocationPermissions() async {
|
||||
@ -171,10 +246,10 @@ class PermissionController {
|
||||
print('Context is null, cannot show dialog');
|
||||
return false;
|
||||
}
|
||||
if (Get.isDialogOpen ?? false) {
|
||||
print('A dialog is already open');
|
||||
return false;
|
||||
}
|
||||
//if (Get.isDialogOpen ?? false) {
|
||||
// print('A dialog is already open');
|
||||
// return false;
|
||||
//}
|
||||
|
||||
try {
|
||||
final result = await Get.dialog<bool>(
|
||||
|
||||
Reference in New Issue
Block a user