From 20d0012bb56d909b7e99b5647ad8aed812ef8e0f Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Wed, 14 Jun 2023 01:50:02 +0700 Subject: [PATCH] TW-140: Setup and create `PermissionHandlerService` --- ios/Podfile | 4 ++ lib/utils/voip/permission_service.dart | 52 +++++++++++++++++++ macos/Flutter/GeneratedPluginRegistrant.swift | 2 + 3 files changed, 58 insertions(+) create mode 100644 lib/utils/voip/permission_service.dart diff --git a/ios/Podfile b/ios/Podfile index 5f9bb93c50..b3ea74368a 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -50,6 +50,10 @@ post_install do |installer| 'PERMISSION_MICROPHONE=1', ## dart: PermissionGroup.contacts 'PERMISSION_CONTACTS=1', + ## dart: PermissionGroup.camera + 'PERMISSION_CAMERA=1', + ## dart: PermissionGroup.photos + 'PERMISSION_PHOTOS=1' ] end end diff --git a/lib/utils/voip/permission_service.dart b/lib/utils/voip/permission_service.dart new file mode 100644 index 0000000000..1e4ad2dbdc --- /dev/null +++ b/lib/utils/voip/permission_service.dart @@ -0,0 +1,52 @@ +import 'dart:io'; +import 'package:permission_handler/permission_handler.dart'; + +class PermissionHandlerService { + static final PermissionHandlerService _instance = PermissionHandlerService._internal(); + + factory PermissionHandlerService() { + return _instance; + } + + PermissionHandlerService._internal(); + + + Future requestPermissionForPhotoActions() { + if (Platform.isIOS) { + return _handlePhotosPermissionIOSAction(); + } else { + return _handlePhotosPermissionAndroidAction(); + } + } + + Future _handlePhotosPermissionIOSAction() async { + final currentStatus = await Permission.photos.status; + return _handlePermission(currentStatus); + } + + Future _handlePhotosPermissionAndroidAction() async { + final currentStatus = await Permission.storage.status; + return _handlePermission(currentStatus); + } + + + Future _handlePermission(PermissionStatus currentStatus) async { + switch (currentStatus) { + case PermissionStatus.denied: + case PermissionStatus.limited: + final newStatus = Platform.isIOS + ? await Permission.mediaLibrary.request() + : await Permission.storage.request(); + return newStatus.isGranted ? PermissionStatus.granted : newStatus; + + case PermissionStatus.granted: + case PermissionStatus.restricted: + case PermissionStatus.permanentlyDenied: + return currentStatus; + } + } + + void goToSettingsForPermissionActions() { + openAppSettings(); + } +} \ No newline at end of file diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index bc1d53d8d2..3db1dd668b 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -24,6 +24,7 @@ import just_audio import macos_ui import package_info_plus_macos import path_provider_foundation +import photo_manager import record_macos import share_plus_macos import shared_preferences_macos @@ -52,6 +53,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { MacOSUiPlugin.register(with: registry.registrar(forPlugin: "MacOSUiPlugin")) FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) + PhotoManagerPlugin.register(with: registry.registrar(forPlugin: "PhotoManagerPlugin")) RecordMacosPlugin.register(with: registry.registrar(forPlugin: "RecordMacosPlugin")) SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))