PermissionFlow

interface PermissionFlow

A utility class which provides a functionality for observing state of a permission (whether it's granted or not) with reactive coroutine stream i.e. StateFlow.

This takes care of listening to permission state change from any screen throughout the application so that you can listen to permission in any layer of architecture within app.

To retrieve the instance, use getInstance method but make sure to initialize it with init method before retrieving instance. Otherwise, it'll throw IllegalStateException

Example usage:

1. Initialization

class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
PermissionFlow.init(this)
}
}

2. Observing permission

val permissionFlow = PermissionFlow.getInstance()

fun observeContactsPermission() {
coroutineScope.launch {
permissionFlow.getPermissionState(android.Manifest.permission.READ_CONTACTS)
.collect { state ->
if (state.isGranted) {
// Do something
} else {
if (state.isRationaleRequired) {
// Do something
}
}
}
}
}

3. Launching permission

class MyActivity: AppCompatActivity() {
private val permissionLauncher = registerForPermissionFlowRequestsResult()

fun askContactPermission() {
permissionLauncher.launch(android.Manifest.permission.READ_CONTACTS)
}
}

This utility tries to listen to permission state change which may not happen within a application (e.g. user trying to allow permission from app settings), but doesn't guarantee that you'll always get a updated state at the accurate instant.

Types

Link copied to clipboard
object Companion

Companion of PermissionFlow to provide initialization of PermissionFlow as well as getting instance.

Functions

Link copied to clipboard
abstract fun getMultiplePermissionState(vararg permissions: String): StateFlow<MultiplePermissionState>

Returns StateFlow of a combining state for permissions

Link copied to clipboard
abstract fun getPermissionState(permission: String): StateFlow<PermissionState>

Returns StateFlow for a given permission

Link copied to clipboard
abstract fun notifyPermissionsChanged(vararg permissions: String)

This helps to check if specified permissions are changed and it verifies it and updates the state of permissions which are being observed via getMultiplePermissionState method.

Link copied to clipboard
abstract fun startListening()

Starts listening the changes of state of permissions.

Link copied to clipboard
abstract fun stopListening()

Stops listening to the state changes of permissions throughout the application. This means the state of permission retrieved with getMultiplePermissionState method will not be updated after stopping listening. To start to listen again, use startListening method.