request notification permission on android 13 when showing home content
This commit is contained in:
parent
3444285592
commit
b8689292c3
|
@ -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 {
|
protected suspend fun ensurePermission(permission: String): PermissionResult {
|
||||||
return when {
|
return when {
|
||||||
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted
|
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted
|
||||||
|
|
|
@ -21,5 +21,6 @@ sealed interface HomeScreenState {
|
||||||
|
|
||||||
sealed interface HomeEvent {
|
sealed interface HomeEvent {
|
||||||
object Relaunch : HomeEvent
|
object Relaunch : HomeEvent
|
||||||
|
object OnShowContent : HomeEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,7 @@ class HomeViewModel(
|
||||||
fun start() {
|
fun start() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
state = if (credentialsProvider.isSignedIn()) {
|
state = if (credentialsProvider.isSignedIn()) {
|
||||||
|
_events.emit(HomeEvent.OnShowContent)
|
||||||
initialHomeContent()
|
initialHomeContent()
|
||||||
} else {
|
} else {
|
||||||
SignedOut
|
SignedOut
|
||||||
|
@ -62,6 +63,7 @@ class HomeViewModel(
|
||||||
fun loggedIn() {
|
fun loggedIn() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
state = initialHomeContent()
|
state = initialHomeContent()
|
||||||
|
_events.emit(HomeEvent.OnShowContent)
|
||||||
listenForInviteChanges()
|
listenForInviteChanges()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package app.dapk.st.home
|
package app.dapk.st.home
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.material3.AlertDialog
|
import androidx.compose.material3.AlertDialog
|
||||||
|
@ -27,10 +29,11 @@ class MainActivity : DapkActivity() {
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
val pushPermissionLauncher = registerPushPermission()
|
||||||
homeViewModel.events.onEach {
|
homeViewModel.events.onEach {
|
||||||
when (it) {
|
when (it) {
|
||||||
HomeEvent.Relaunch -> recreate()
|
HomeEvent.Relaunch -> recreate()
|
||||||
|
HomeEvent.OnShowContent -> pushPermissionLauncher?.invoke()
|
||||||
}
|
}
|
||||||
}.launchIn(lifecycleScope)
|
}.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
|
@Composable
|
||||||
private fun BetaUpgradeDialog() {
|
private fun BetaUpgradeDialog() {
|
||||||
AlertDialog(
|
AlertDialog(
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest package="app.dapk.st.notifications"/>
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.dapk.st.notifications">
|
||||||
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||||
|
</manifest>
|
Loading…
Reference in New Issue