package com.cottondb.android import android.os.Bundle import android.os.Build import androidx.activity.SystemBarStyle import androidx.activity.enableEdgeToEdge import androidx.fragment.app.FragmentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.collectAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.cottondb.android.boot.DeviceResetState import com.cottondb.android.boot.BootState import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.design.CottonTheme import com.cottondb.android.generated.CottonPalette import com.cottondb.android.ui.boot.CottonBootGate import com.cottondb.android.ui.navigation.AppNavigation class MainActivity : FragmentActivity() { private val notificationPath = mutableStateOf(null) override fun onNewIntent(intent: android.content.Intent) { super.onNewIntent(intent) setIntent(intent) acceptNotification(intent) } private fun acceptNotification(intent: android.content.Intent) { val route = com.cottondb.android.reminders.notificationRoute(intent.getStringExtra("kind").orEmpty(), intent.getStringExtra("id").orEmpty()) notificationPath.value = when (intent.action) { "com.cottondb.android.KEYBOARD_TAKE" -> "/keyboard-take" "com.cottondb.android.MEETING_RECORDING" -> "/meetings/record" else -> route?.path } } override fun onSaveInstanceState(outState: Bundle) { outState.putString("notificationPath", notificationPath.value) super.onSaveInstanceState(outState) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState == null) acceptNotification(intent) else notificationPath.value = savedInstanceState.getString("notificationPath") enableEdgeToEdge( statusBarStyle = SystemBarStyle.light(android.graphics.Color.TRANSPARENT, android.graphics.Color.TRANSPARENT), navigationBarStyle = SystemBarStyle.light(android.graphics.Color.TRANSPARENT, android.graphics.Color.TRANSPARENT), ) val runtime = (application as CottonApplication).runtime setContent { CottonTheme { val runtimeState by runtime.state.collectAsState() val eventError by runtime.eventError.collectAsState() when (val state = runtimeState) { RuntimeState.Opening -> StartupPaper("Opening Cotton…") is RuntimeState.Error -> StartupError(state.message, runtime::open) is RuntimeState.Ready -> { val boot by state.boot.state.collectAsState() val join by state.join.state.collectAsState() val home by state.home.state.collectAsState() val reset by state.reset.state.collectAsState() val syncError by state.boot.syncError.collectAsState() val syncStatus by state.sync.state.collectAsState() val showJoin = remember(state) { mutableStateOf(false) } val confirmingErase = remember { mutableStateOf(false) } androidx.compose.runtime.LaunchedEffect(boot) { if ((boot as? BootState.Refused)?.refusal?.erase == true) state.reset.erase() } if (confirmingErase.value) { androidx.compose.material3.AlertDialog( onDismissRequest = { confirmingErase.value = false }, title = { Text("Erase Cotton from this phone?") }, text = { Text("Everything Cotton holds on this phone goes: your database, the photos and files it downloaded, and this phone's identity. Nothing on your other devices is touched.") }, confirmButton = { androidx.compose.material3.TextButton(onClick = { confirmingErase.value = false state.reset.erase() }) { Text("Erase") } }, dismissButton = { androidx.compose.material3.TextButton(onClick = { confirmingErase.value = false }) { Text("Cancel") } }, ) } when (val progress = reset) { is DeviceResetState.Leaving -> StartupPaper("Leaving this database…") is DeviceResetState.Erasing -> StartupPaper("Erasing Cotton from this phone…") is DeviceResetState.Error -> StartupError(progress.message, state.reset::retry) DeviceResetState.Idle -> CottonBootGate( boot = boot, join = join, showJoin = showJoin.value, deviceName = Build.MODEL.takeIf { it.isNotBlank() } ?: "This Android phone", appVersion = BuildConfig.VERSION_NAME, onShowJoin = { showJoin.value = true }, onStartLocal = state.boot::startLocal, onJoin = runtime::startJoin, onCancelJoin = { showJoin.value = false; runtime.cancelJoin() }, onRetry = state.boot::refresh, onSetName = state.join::setName, onErase = { confirmingErase.value = true }, onLeave = state.reset::leave, readyContent = { androidx.compose.runtime.key(state) { AppNavigation( boot = (boot as BootState.Ready).boot, onJoin = { showJoin.value = true }, onLeave = state.reset::leave, alarmPermissions = { com.cottondb.android.ui.day.AlarmPermissions(state.notifications, runtime::reportPlatformError) }, notificationRoute = notificationPath.value?.let(com.cottondb.android.navigation.Route::parse), onNotificationConsumed = { notificationPath.value = null }, onKeyboardReturn = { moveTaskToBack(true) }, home = home, features = state.features, onRefreshHome = state.home::refresh, onSaveArrangement = state.home::saveArrangement, onUndoDone = state.home::undo, ) } }, ) } val keyboardProblem = state.features.keyboard?.state?.collectAsState()?.value?.problem (eventError ?: keyboardProblem ?: syncError ?: (syncStatus as? com.cottondb.android.boot.SyncState.Error)?.message)?.let { RuntimeNotice(it) } } } } } } } @androidx.compose.runtime.Composable private fun StartupPaper(message: String) = Box( Modifier.fillMaxSize().background(CottonPalette.groundMuted).padding(28.dp), contentAlignment = Alignment.Center, ) { Text(message, style = CottonTextStyles.body, color = CottonPalette.inkBody) } @androidx.compose.runtime.Composable private fun RuntimeNotice(message: String) = Box(Modifier.fillMaxSize().padding(20.dp), contentAlignment = Alignment.BottomCenter) { Text(message, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } @androidx.compose.runtime.Composable private fun StartupError(message: String, onRetry: () -> Unit) = Box( Modifier.fillMaxSize().background(CottonPalette.groundMuted).padding(28.dp), contentAlignment = Alignment.Center, ) { androidx.compose.foundation.layout.Column { Text(message, style = CottonTextStyles.body, color = CottonPalette.inkBody) androidx.compose.material3.TextButton(onClick = onRetry) { Text("Try again") } } }