Cotton code
163 lines · 8.9 KB · KotlinRaw
package com.cottondb.androidimport android.os.Bundleimport android.os.Buildimport androidx.activity.SystemBarStyleimport androidx.activity.enableEdgeToEdgeimport androidx.fragment.app.FragmentActivityimport androidx.activity.compose.setContentimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Textimport androidx.compose.runtime.getValueimport androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.unit.dpimport com.cottondb.android.boot.DeviceResetStateimport com.cottondb.android.boot.BootStateimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.ui.boot.CottonBootGateimport com.cottondb.android.ui.navigation.AppNavigationclass MainActivity : FragmentActivity() {    private val notificationPath = mutableStateOf<String?>(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.Composableprivate 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.Composableprivate fun RuntimeNotice(message: String) = Box(Modifier.fillMaxSize().padding(20.dp), contentAlignment = Alignment.BottomCenter) {    Text(message, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary)}@androidx.compose.runtime.Composableprivate 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") }    }}