package com.cottondb.android.ui.day import android.Manifest import android.content.Intent import android.net.Uri import android.os.Build import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.platform.ScheduledNotifications import com.cottondb.android.reminders.NotificationPermission import kotlinx.coroutines.CancellationException import kotlinx.coroutines.launch /** Permission requests follow an explicit tap, never app boot or an alarm write. */ @Composable fun 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") } } } }