Skip to content

Latest commit

 

History

History
83 lines (61 loc) · 2.69 KB

README.md

File metadata and controls

83 lines (61 loc) · 2.69 KB

Permifriend

Maven Central License

Power-up your Accompanist Permission by handling runtime permission event. It complements with HiltViewModel. It will save you from the tedious job around Runtime Permissions.

Requirements

  1. minSdk 24
  2. Accompanist permission library

Installation

dependencies {
    implementation 'io.github.shohiebsense:permifriend:0.1.0'
}

Usage

  1. Implement your hiltViewModel with PermifriendScope
@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel(), PermifriendScope  {

    override val isPermissionRequestButtonClicked = mutableStateOf(false)
    override val isPermissionDialogShowing = mutableStateOf(false)

    override fun onPermissionGranted() {
        shouldShowNavigateToSettingDialog.value = false
    }

    override fun showRequestPermissionRationaleDialog() {
      //your app's rationale dialog when user denies permission dialog.
    }

}
  1. Call HandleRuntimePermission(permissionScope, permissionState) inside the composable
@Composable
fun MainScreen(){
    val viewModel =  hiltViewModel<MainViewModel>()
    val cameraPermissionState = rememberPermissionState(android.Manifest.permission.CAMERA)
    val context = LocalContext.current

    HandleRuntimePermission(permifriendScope = viewModel, cameraPermissionState = cameraPermissionState)
    ...
}
  1. Trigger the permission request by changing the isPermissionRequestButtonClicked value to true
@Composable
fun MainScreen(){
  ...
  Button(onClick = { viewModel.isPermissionRequestButtonClicked.value = true }) {
    Text("Request permission")
  }
}

License

Copyright (C) 2022 Shohieb Nasruddin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.