adding runtime permission granter

This commit is contained in:
Adam Brown 2022-09-28 17:58:48 +01:00
parent 9dcbd63eae
commit 846cf66fa1
1 changed files with 32 additions and 1 deletions

View File

@ -1,15 +1,19 @@
package app.dapk.st.core package app.dapk.st.core
import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.WindowManager import android.view.WindowManager
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect import androidx.compose.runtime.SideEffect
import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.core.extensions.unsafeLazy
import app.dapk.st.design.components.SmallTalkTheme import app.dapk.st.design.components.SmallTalkTheme
import app.dapk.st.design.components.ThemeConfig import app.dapk.st.design.components.ThemeConfig
import app.dapk.st.navigator.navigator import app.dapk.st.navigator.navigator
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import androidx.activity.compose.setContent as _setContent import androidx.activity.compose.setContent as _setContent
abstract class DapkActivity : ComponentActivity(), EffectScope { abstract class DapkActivity : ComponentActivity(), EffectScope {
@ -27,7 +31,6 @@ abstract class DapkActivity : ComponentActivity(), EffectScope {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
this.themeConfig = ThemeConfig(themeStore.isMaterialYouEnabled()) this.themeConfig = ThemeConfig(themeStore.isMaterialYouEnabled())
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} }
@ -64,4 +67,32 @@ abstract class DapkActivity : ComponentActivity(), EffectScope {
} else } else
super.onBackPressed() super.onBackPressed()
} }
protected suspend fun ensurePermission(permission: String): PermissionResult {
return when {
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted
shouldShowRequestPermissionRationale(permission) -> PermissionResult.ShowRational
else -> {
val isGranted = suspendCancellableCoroutine { continuation ->
val callback: (result: Boolean) -> Unit = { result -> continuation.resume(result) }
val launcher = registerForActivityResult(ActivityResultContracts.RequestPermission(), callback)
launcher.launch(permission)
continuation.invokeOnCancellation { launcher.unregister() }
}
when (isGranted) {
true -> PermissionResult.Granted
false -> PermissionResult.Denied
}
}
}
}
}
sealed interface PermissionResult {
object Granted : PermissionResult
object ShowRational : PermissionResult
object Denied : PermissionResult
} }