diff --git a/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/DapkActivity.kt b/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/DapkActivity.kt index 29ee41b..d297ca6 100644 --- a/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/DapkActivity.kt +++ b/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/DapkActivity.kt @@ -1,15 +1,19 @@ package app.dapk.st.core +import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.view.WindowManager import androidx.activity.ComponentActivity +import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.runtime.Composable import androidx.compose.runtime.SideEffect import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.design.components.SmallTalkTheme import app.dapk.st.design.components.ThemeConfig import app.dapk.st.navigator.navigator +import kotlinx.coroutines.suspendCancellableCoroutine +import kotlin.coroutines.resume import androidx.activity.compose.setContent as _setContent abstract class DapkActivity : ComponentActivity(), EffectScope { @@ -27,7 +31,6 @@ abstract class DapkActivity : ComponentActivity(), EffectScope { super.onCreate(savedInstanceState) this.themeConfig = ThemeConfig(themeStore.isMaterialYouEnabled()) - window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); } @@ -64,4 +67,32 @@ abstract class DapkActivity : ComponentActivity(), EffectScope { } else 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 }