Package 

Interface PermissionFlow


  • 
    public 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[android.Manifest.permission.READ_CONTACTS].collect { isGranted ->
                if (isGranted) {
                    // 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.

    • Method Summary

      Modifier and Type Method Description
      abstract StateFlow<Boolean> get(String permission) Returns StateFlow for a given permission
      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 get method.
      abstract Unit startListening() Starts listening the changes of state of permissions.
      abstract Unit stopListening() Stops listening to the state changes of permissions throughout the application.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

         abstract StateFlow<Boolean> get(String permission)

        Returns StateFlow for a given permission

        Parameters:
        permission - Unique permission identity (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 get 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 lazily when get 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 get method will not be updated after stopping listening. To start to listen again, use startListening method.