This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // register permission on top of Fragment or Activity like below | |
| private val cameraPermission = registerPermission { | |
| onCameraPermissionResult(it) | |
| } | |
| private val storagePermission = registerPermission { | |
| onStoragePermissionResult(it) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun Permission.launchSinglePermission(permission: String) { | |
| this.result.launch(arrayOf(permission)) | |
| } | |
| fun Permission.launchMultiplePermission(permissionList: Array<String>) { | |
| this.result.launch(permissionList) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private fun getPermissionState(activity: Activity, result: Map<String, Boolean>): PermissionState { | |
| val deniedPermissions = result.filterValues { !it }.keys | |
| return when { | |
| deniedPermissions.isEmpty() -> PermissionState.Granted | |
| deniedPermissions.any { permission -> | |
| !shouldShowRequestPermissionRationale( | |
| /* activity = */ | |
| activity, | |
| /* permission = */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun Fragment.registerPermission(onPermissionResult: (PermissionState) -> Unit): Permission { | |
| return Permission( | |
| this.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { | |
| onPermissionResult(getPermissionState(activity, it)) | |
| } | |
| ) | |
| } | |
| fun AppCompatActivity.registerPermission(onPermissionResult: (PermissionState) -> Unit): Permission { | |
| return Permission( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @JvmInline | |
| value class Permission(val result: ActivityResultLauncher<Array<String>>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed class PermissionState { | |
| object Granted : PermissionState() | |
| object Denied : PermissionState() | |
| object PermanentlyDenied : PermissionState() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Activity and the permission is needed | |
| val isPermanentluDenied= shouldShowRequestPermissionRationale(activity, android.Manifest.permission.CAMERA) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Define a permission request | |
| private val cameraPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> | |
| if(granted){ | |
| } | |
| } | |
| //launch it | |
| cameraPermission.launch(android.Manifest.permission.CAMERA) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <uses-permission android:name="android.permission.CAMERA"/> | |
| <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
| <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FlowObserver<T> ( | |
| lifecycleOwner: LifecycleOwner, | |
| private val flow: Flow<T>, | |
| private val collector: suspend (T) -> Unit | |
| ) { | |
| private var job: Job? = null | |
| init { | |
| lifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { |