From b8689292c3b11818ee5adab01e182d9a37b88670 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 12 Oct 2022 22:59:31 +0100 Subject: [PATCH] request notification permission on android 13 when showing home content --- .../main/kotlin/app/dapk/st/core/DapkActivity.kt | 10 ++++++++++ .../src/main/kotlin/app/dapk/st/home/HomeState.kt | 1 + .../main/kotlin/app/dapk/st/home/HomeViewModel.kt | 2 ++ .../main/kotlin/app/dapk/st/home/MainActivity.kt | 13 ++++++++++++- features/notifications/src/main/AndroidManifest.xml | 4 +++- 5 files changed, 28 insertions(+), 2 deletions(-) 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 4a9cc24..4c75c4f 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 @@ -75,6 +75,16 @@ abstract class DapkActivity : ComponentActivity(), EffectScope { } } + protected fun registerForPermission(permission: String, callback: () -> Unit = {}): () -> Unit { + val resultCallback: (result: Boolean) -> Unit = { result -> + if (result) { + callback() + } + } + val launcher = registerForActivityResult(ActivityResultContracts.RequestPermission(), resultCallback) + return { launcher.launch(permission) } + } + protected suspend fun ensurePermission(permission: String): PermissionResult { return when { checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted diff --git a/features/home/src/main/kotlin/app/dapk/st/home/HomeState.kt b/features/home/src/main/kotlin/app/dapk/st/home/HomeState.kt index c7d74a3..22ec6aa 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/HomeState.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/HomeState.kt @@ -21,5 +21,6 @@ sealed interface HomeScreenState { sealed interface HomeEvent { object Relaunch : HomeEvent + object OnShowContent : HomeEvent } diff --git a/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt b/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt index 456f0a0..6de8f9a 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt @@ -39,6 +39,7 @@ class HomeViewModel( fun start() { viewModelScope.launch { state = if (credentialsProvider.isSignedIn()) { + _events.emit(HomeEvent.OnShowContent) initialHomeContent() } else { SignedOut @@ -62,6 +63,7 @@ class HomeViewModel( fun loggedIn() { viewModelScope.launch { state = initialHomeContent() + _events.emit(HomeEvent.OnShowContent) listenForInviteChanges() } } diff --git a/features/home/src/main/kotlin/app/dapk/st/home/MainActivity.kt b/features/home/src/main/kotlin/app/dapk/st/home/MainActivity.kt index c308346..22567c4 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/MainActivity.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/MainActivity.kt @@ -1,5 +1,7 @@ package app.dapk.st.home +import android.Manifest +import android.os.Build import android.os.Bundle import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.AlertDialog @@ -27,10 +29,11 @@ class MainActivity : DapkActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - + val pushPermissionLauncher = registerPushPermission() homeViewModel.events.onEach { when (it) { HomeEvent.Relaunch -> recreate() + HomeEvent.OnShowContent -> pushPermissionLauncher?.invoke() } }.launchIn(lifecycleScope) @@ -45,6 +48,14 @@ class MainActivity : DapkActivity() { } } + private fun registerPushPermission(): (() -> Unit)? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + registerForPermission(Manifest.permission.POST_NOTIFICATIONS) + } else { + null + } + } + @Composable private fun BetaUpgradeDialog() { AlertDialog( diff --git a/features/notifications/src/main/AndroidManifest.xml b/features/notifications/src/main/AndroidManifest.xml index e76fe66..ebdd9bb 100644 --- a/features/notifications/src/main/AndroidManifest.xml +++ b/features/notifications/src/main/AndroidManifest.xml @@ -1,2 +1,4 @@ - \ No newline at end of file + + + \ No newline at end of file