Skip to content

Instantly share code, notes, and snippets.

@premacck
Created February 2, 2021 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save premacck/1bfabec71a1077c19f8f541770e7250e to your computer and use it in GitHub Desktop.
Save premacck/1bfabec71a1077c19f8f541770e7250e to your computer and use it in GitHub Desktop.
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