2023-02-28 16:22:18 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
|
|
class GalleryPermissionNotifier extends StateNotifier<PermissionStatus> {
|
|
|
|
GalleryPermissionNotifier()
|
2023-06-26 15:27:32 +00:00
|
|
|
: super(PermissionStatus.denied) // Denied is the intitial state
|
2023-02-28 16:22:18 +00:00
|
|
|
{
|
|
|
|
// Sets the initial state
|
|
|
|
getGalleryPermissionStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasPermission => state.isGranted || state.isLimited;
|
|
|
|
|
|
|
|
/// Requests the gallery permission
|
|
|
|
Future<PermissionStatus> requestGalleryPermission() async {
|
2023-06-26 15:27:32 +00:00
|
|
|
PermissionStatus result;
|
2023-02-28 16:22:18 +00:00
|
|
|
// Android 32 and below uses Permission.storage
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
|
|
|
if (androidInfo.version.sdkInt <= 32) {
|
|
|
|
// Android 32 and below need storage
|
|
|
|
final permission = await Permission.storage.request();
|
2023-06-26 15:27:32 +00:00
|
|
|
result = permission;
|
2023-02-28 16:22:18 +00:00
|
|
|
} else {
|
|
|
|
// Android 33 need photo & video
|
|
|
|
final photos = await Permission.photos.request();
|
|
|
|
if (!photos.isGranted) {
|
|
|
|
// Don't ask twice for the same permission
|
2023-06-26 15:27:32 +00:00
|
|
|
state = photos;
|
2023-02-28 16:22:18 +00:00
|
|
|
return photos;
|
|
|
|
}
|
|
|
|
final videos = await Permission.videos.request();
|
|
|
|
|
|
|
|
// Return the joint result of those two permissions
|
|
|
|
final PermissionStatus status;
|
2024-08-14 23:55:52 +00:00
|
|
|
if ((photos.isGranted && videos.isGranted) ||
|
|
|
|
(photos.isLimited && videos.isLimited)) {
|
2023-02-28 16:22:18 +00:00
|
|
|
status = PermissionStatus.granted;
|
|
|
|
} else if (photos.isDenied || videos.isDenied) {
|
|
|
|
status = PermissionStatus.denied;
|
|
|
|
} else if (photos.isPermanentlyDenied || videos.isPermanentlyDenied) {
|
|
|
|
status = PermissionStatus.permanentlyDenied;
|
|
|
|
} else {
|
|
|
|
status = PermissionStatus.denied;
|
|
|
|
}
|
|
|
|
|
2023-06-26 15:27:32 +00:00
|
|
|
result = status;
|
|
|
|
}
|
|
|
|
if (result == PermissionStatus.granted &&
|
|
|
|
androidInfo.version.sdkInt >= 29) {
|
|
|
|
result = await Permission.accessMediaLocation.request();
|
2023-02-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// iOS can use photos
|
|
|
|
final photos = await Permission.photos.request();
|
2023-06-26 15:27:32 +00:00
|
|
|
result = photos;
|
2023-02-28 16:22:18 +00:00
|
|
|
}
|
2023-06-26 15:27:32 +00:00
|
|
|
state = result;
|
|
|
|
return result;
|
2023-02-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks the current state of the gallery permissions without
|
|
|
|
/// requesting them again
|
|
|
|
Future<PermissionStatus> getGalleryPermissionStatus() async {
|
2023-06-26 15:27:32 +00:00
|
|
|
PermissionStatus result;
|
2023-02-28 16:22:18 +00:00
|
|
|
// Android 32 and below uses Permission.storage
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
|
|
|
if (androidInfo.version.sdkInt <= 32) {
|
|
|
|
// Android 32 and below need storage
|
|
|
|
final permission = await Permission.storage.status;
|
2023-06-26 15:27:32 +00:00
|
|
|
result = permission;
|
2023-02-28 16:22:18 +00:00
|
|
|
} else {
|
|
|
|
// Android 33 needs photo & video
|
|
|
|
final photos = await Permission.photos.status;
|
|
|
|
final videos = await Permission.videos.status;
|
|
|
|
|
|
|
|
// Return the joint result of those two permissions
|
|
|
|
final PermissionStatus status;
|
2024-08-14 23:55:52 +00:00
|
|
|
if ((photos.isGranted && videos.isGranted) ||
|
|
|
|
(photos.isLimited && videos.isLimited)) {
|
2023-02-28 16:22:18 +00:00
|
|
|
status = PermissionStatus.granted;
|
|
|
|
} else if (photos.isDenied || videos.isDenied) {
|
|
|
|
status = PermissionStatus.denied;
|
|
|
|
} else if (photos.isPermanentlyDenied || videos.isPermanentlyDenied) {
|
|
|
|
status = PermissionStatus.permanentlyDenied;
|
|
|
|
} else {
|
|
|
|
status = PermissionStatus.denied;
|
|
|
|
}
|
|
|
|
|
2023-06-26 15:27:32 +00:00
|
|
|
result = status;
|
|
|
|
}
|
|
|
|
if (state == PermissionStatus.granted &&
|
|
|
|
androidInfo.version.sdkInt >= 29) {
|
|
|
|
result = await Permission.accessMediaLocation.status;
|
2023-02-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// iOS can use photos
|
|
|
|
final photos = await Permission.photos.status;
|
2023-06-26 15:27:32 +00:00
|
|
|
result = photos;
|
2023-02-28 16:22:18 +00:00
|
|
|
}
|
2023-06-26 15:27:32 +00:00
|
|
|
state = result;
|
|
|
|
return result;
|
2023-02-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 15:27:32 +00:00
|
|
|
final galleryPermissionNotifier =
|
|
|
|
StateNotifierProvider<GalleryPermissionNotifier, PermissionStatus>(
|
|
|
|
(ref) => GalleryPermissionNotifier(),
|
|
|
|
);
|