|
1 | 1 | package dev.typetype.android.feature.channel |
2 | 2 |
|
| 3 | +import android.Manifest |
| 4 | +import android.content.pm.PackageManager |
| 5 | +import android.os.Build |
| 6 | +import androidx.activity.compose.rememberLauncherForActivityResult |
| 7 | +import androidx.activity.result.contract.ActivityResultContracts |
3 | 8 | import androidx.compose.foundation.background |
4 | 9 | import androidx.compose.foundation.layout.Arrangement |
5 | 10 | import androidx.compose.foundation.layout.Box |
@@ -32,12 +37,14 @@ import androidx.compose.ui.Alignment |
32 | 37 | import androidx.compose.ui.Modifier |
33 | 38 | import androidx.compose.ui.draw.clip |
34 | 39 | import androidx.compose.ui.layout.ContentScale |
| 40 | +import androidx.compose.ui.platform.LocalContext |
35 | 41 | import androidx.compose.ui.res.pluralStringResource |
36 | 42 | import androidx.compose.ui.res.stringResource |
37 | 43 | import androidx.compose.ui.text.font.FontWeight |
38 | 44 | import androidx.compose.ui.text.style.TextOverflow |
39 | 45 | import androidx.compose.ui.unit.dp |
40 | 46 | import androidx.compose.ui.unit.sp |
| 47 | +import androidx.core.content.ContextCompat |
41 | 48 | import coil3.compose.AsyncImage |
42 | 49 | import dev.typetype.android.R |
43 | 50 | import dev.typetype.android.core.ui.share.LocalServerBaseUrl |
@@ -89,6 +96,22 @@ private fun ChannelHeaderContent( |
89 | 96 | expanded: Boolean, |
90 | 97 | ) { |
91 | 98 | val serverBaseUrl = LocalServerBaseUrl.current |
| 99 | + val context = LocalContext.current |
| 100 | + val notificationPermissionLauncher = rememberLauncherForActivityResult( |
| 101 | + contract = ActivityResultContracts.RequestPermission(), |
| 102 | + ) { granted -> |
| 103 | + if (granted) onToggleNotifications() |
| 104 | + } |
| 105 | + val requestNotifications: () -> Unit = { |
| 106 | + val notificationsGranted = Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || |
| 107 | + ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == |
| 108 | + PackageManager.PERMISSION_GRANTED |
| 109 | + if (notificationsEnabled || notificationsGranted) { |
| 110 | + onToggleNotifications() |
| 111 | + } else { |
| 112 | + notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) |
| 113 | + } |
| 114 | + } |
92 | 115 | Column(modifier = Modifier.fillMaxWidth()) { |
93 | 116 | if (channel.bannerUrl.isNullOrBlank()) { |
94 | 117 | ChannelBackButton(onNavigateBack = onNavigateBack) |
@@ -136,7 +159,7 @@ private fun ChannelHeaderContent( |
136 | 159 | } |
137 | 160 | if (notificationsAvailable) { |
138 | 161 | IconButton( |
139 | | - onClick = onToggleNotifications, |
| 162 | + onClick = requestNotifications, |
140 | 163 | enabled = !notificationsInFlight, |
141 | 164 | ) { |
142 | 165 | Icon( |
|
0 commit comments