Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Util functions for Splitties permissions
import android.Manifest
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.suspendCancellableCoroutine
import splitties.alertdialog.appcompat.alertDialog
import splitties.alertdialog.appcompat.coroutines.DialogButton
import splitties.alertdialog.appcompat.coroutines.showAndAwait
import splitties.alertdialog.appcompat.messageResource
import splitties.alertdialog.appcompat.titleResource
import splitties.experimental.ExperimentalSplittiesApi
import splitties.permissions.ensureAllPermissions
import splitties.permissions.ensurePermission
import splitties.resources.str
@ExperimentalSplittiesApi suspend fun FragmentActivity.ensureStoragePermissions() {
ensureAllPermissions(
permissionNames = listOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE),
activity = this,
fragmentManager = supportFragmentManager,
lifecycle = lifecycle,
showRationaleBeforeFirstAsk = false,
showRationaleAndContinueOrReturn = {
alertDialog {
titleResource = R.string.need_storage_permissions_title
messageResource = R.string.need_storage_permissions_message
}.showAndAwait(okValue = true, dismissValue = true, cancelValue = false)
},
askOpenSettingsOrReturn = {
alertDialog { messageResource = R.string.open_settings_prompt_message }.showAndAwait(
positiveButton = DialogButton(str(R.string.go_to_settings), true),
negativeButton = DialogButton(str(R.string.cancel), false),
dismissValue = true
)
},
returnOrThrowBlock = { suspendCancellableCoroutine<Nothing> { c -> c.cancel() } }
)
}
@ExperimentalSplittiesApi suspend fun Fragment.ensureStoragePermissions(coroutineScope: CoroutineScope) = activity?.ensureStoragePermissions() ?: coroutineScope.cancel()
@ExperimentalSplittiesApi suspend fun FragmentActivity.ensureCameraPermissions() {
ensurePermission(
permission = Manifest.permission.CAMERA,
activity = this,
fragmentManager = supportFragmentManager,
lifecycle = lifecycle,
showRationaleBeforeFirstAsk = false,
showRationaleAndContinueOrReturn = {
alertDialog {
titleResource = R.string.need_camera_permissions_title
messageResource = R.string.need_camera_permissions_message
}.showAndAwait(okValue = true, dismissValue = true, cancelValue = false)
},
askOpenSettingsOrReturn = {
alertDialog { messageResource = R.string.open_settings_prompt_message }.showAndAwait(
positiveButton = DialogButton(str(R.string.go_to_settings), true),
negativeButton = DialogButton(str(R.string.cancel), false),
dismissValue = true
)
},
returnOrThrowBlock = { suspendCancellableCoroutine<Nothing> { c -> c.cancel() } }
)
}
@ExperimentalSplittiesApi suspend fun Fragment.ensureCameraPermissions(coroutineScope: CoroutineScope) = activity?.ensureCameraPermissions() ?: coroutineScope.cancel()
@ExperimentalSplittiesApi suspend fun FragmentActivity.ensureLocationPermissions() {
ensureAllPermissions(
permissionNames = listOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION),
activity = this,
fragmentManager = supportFragmentManager,
lifecycle = lifecycle,
showRationaleBeforeFirstAsk = false,
showRationaleAndContinueOrReturn = {
alertDialog {
titleResource = R.string.need_location_permissions_title
messageResource = R.string.need_location_permissions_message
}.showAndAwait(okValue = true, dismissValue = true, cancelValue = false)
},
askOpenSettingsOrReturn = {
alertDialog { messageResource = R.string.open_settings_prompt_message }.showAndAwait(
positiveButton = DialogButton(str(R.string.go_to_settings), true),
negativeButton = DialogButton(str(R.string.cancel), false),
dismissValue = true
)
},
returnOrThrowBlock = { suspendCancellableCoroutine<Nothing> { c -> c.cancel() } }
)
}
@ExperimentalSplittiesApi suspend fun Fragment.ensureLocationPermissions(coroutineScope: CoroutineScope) = activity?.ensureLocationPermissions() ?: coroutineScope.cancel()
@ExperimentalSplittiesApi suspend fun FragmentActivity.ensureCallPermissions() {
ensurePermission(
permission = Manifest.permission.CALL_PHONE,
activity = this,
fragmentManager = supportFragmentManager,
lifecycle = lifecycle,
showRationaleBeforeFirstAsk = false,
showRationaleAndContinueOrReturn = {
alertDialog {
titleResource = R.string.need_call_permissions_title
messageResource = R.string.need_call_permissions_message
}.showAndAwait(okValue = true, dismissValue = true, cancelValue = false)
},
askOpenSettingsOrReturn = {
alertDialog { messageResource = R.string.open_settings_prompt_message }.showAndAwait(
positiveButton = DialogButton(str(R.string.go_to_settings), true),
negativeButton = DialogButton(str(R.string.cancel), false),
dismissValue = true
)
},
returnOrThrowBlock = { suspendCancellableCoroutine<Nothing> { c -> c.cancel() } }
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment