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.
minSdk 24
- Accompanist permission library
dependencies {
implementation 'io.github.shohiebsense:permifriend:0.1.0'
}
- Implement your
hiltViewModel
withPermifriendScope
@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.
}
}
- 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)
...
}
- Trigger the permission request by changing the
isPermissionRequestButtonClicked
value to true
@Composable
fun MainScreen(){
...
Button(onClick = { viewModel.isPermissionRequestButtonClicked.value = true }) {
Text("Request permission")
}
}
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.