70 lines · 3.6 KB · KotlinRaw
package com.cottondb.android.ui.dayimport android.Manifestimport android.content.Intentimport android.net.Uriimport android.os.Buildimport android.provider.Settingsimport androidx.activity.compose.rememberLauncherForActivityResultimport androidx.activity.result.contract.ActivityResultContractsimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.getValueimport androidx.compose.runtime.rememberCoroutineScopeimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.unit.dpimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.platform.ScheduledNotificationsimport com.cottondb.android.reminders.NotificationPermissionimport kotlinx.coroutines.CancellationExceptionimport kotlinx.coroutines.launch/** Permission requests follow an explicit tap, never app boot or an alarm write. */@Composablefun AlarmPermissions(notifications: ScheduledNotifications, report: (String) -> Unit) { val state by notifications.state.collectAsState() val context = LocalContext.current val scope = rememberCoroutineScope() val permission = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { notifications.request() } val settings = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { notifications.request() } val current = state Column(Modifier.padding(horizontal = 20.dp)) { when { current == null -> Text("Checking alarm access…", style = CottonTextStyles.caption) current.error != null -> Text(current.error, style = CottonTextStyles.caption) !current.prefs.enabled -> Text("Notifications are turned off in Cotton.", style = CottonTextStyles.caption) } if (current != null && current.permission != NotificationPermission.Granted) { Text("Allow notifications so this phone can show and stop alarms.", style = CottonTextStyles.caption) TextButton(onClick = { scope.launch { try { if (Build.VERSION.SDK_INT >= 33 && current.permission == NotificationPermission.Undetermined) { notifications.recordPermissionRequest() permission.launch(Manifest.permission.POST_NOTIFICATIONS) } else { settings.launch(Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)) } } catch (cancelled: CancellationException) { throw cancelled } catch (failure: Exception) { report(failure.message ?: "Notification settings could not be opened.") } } }) { Text("Allow notifications") } } else if (current != null && !current.exactAlarms && Build.VERSION.SDK_INT >= 31) { Text("Allow alarms so they can ring at the time you choose.", style = CottonTextStyles.caption) TextButton(onClick = { try { settings.launch(Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:${context.packageName}"))) } catch (failure: Exception) { report(failure.message ?: "Alarm settings could not be opened.") } }) { Text("Allow alarms") } } }}