-
public interface PermissionFlowA 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classPermissionFlow.CompanionCompanion of PermissionFlow to provide initialization of PermissionFlow as well as getting instance.
-
Method Summary
Modifier and Type Method Description abstract StateFlow<PermissionState>getPermissionState(String permission)Returns StateFlow for a given permission abstract StateFlow<MultiplePermissionState>getMultiplePermissionState(String permissions)Returns StateFlow of a combining state for permissions abstract UnitnotifyPermissionsChanged(String permissions)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. abstract UnitstartListening()Starts listening the changes of state of permissions. abstract UnitstopListening()Stops listening to the state changes of permissions throughout the application. -
-
Method Detail
-
getPermissionState
abstract StateFlow<PermissionState> getPermissionState(String permission)
Returns StateFlow for a given permission
- Parameters:
permission- Unique permission identity (for e.g.
-
getMultiplePermissionState
abstract StateFlow<MultiplePermissionState> getMultiplePermissionState(String permissions)
Returns StateFlow of a combining state for permissions
- Parameters:
permissions- List of permissions (for e.g.
-
notifyPermissionsChanged
abstract Unit notifyPermissionsChanged(String permissions)
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.
This can be useful when you are not using result launcher which is provided with this library and manually handling permission request and want to update the state of permission in this library so that flows which are being observed should get an updated state.
If stopListening is called earlier and hasn't started listening again, notifying permission doesn't work. Its new state is automatically calculated after starting listening to states again by calling startListening method.
Example usage:
In this example, we are not using result launcher provided by this library. So we are manually notifying library about state change of a permission.
class MyActivity: AppCompatActivity() { private val permissionFlow = PermissionFlow.getInstance() private val permissionLauncher = registerForActivityResult(RequestPermission()) { isGranted -> permissionFlow.notifyPermissionsChanged(android.Manifest.permission.READ_CONTACTS) } }- Parameters:
permissions- List of permissions
-
startListening
abstract Unit startListening()
Starts listening the changes of state of permissions.
Ideally it automatically starts listening eagerly when application is started and created via dev.shreyaspatil.permissionFlow.initializer.PermissionFlowInitializer. If initializer is disabled, then starts listening lazily when getPermissionEvent or getMultiplePermissionState method is used for the first time. But this can be used to start to listen again after stopping listening with stopListening.
-
stopListening
abstract Unit 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.
-
-
-
-