129 lines · 5.3 KB · KotlinRaw
package com.cottondb.android.ui.settingsimport android.content.ActivityNotFoundExceptionimport android.content.Intentimport android.provider.Settingsimport androidx.activity.compose.BackHandlerimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.saveable.rememberSaveableimport androidx.compose.runtime.setValueimport androidx.compose.ui.platform.LocalContextimport com.cottondb.android.FeatureModelsimport com.cottondb.android.activity.BinPresentationFactsimport com.cottondb.android.activitybin.ActivityBinStateimport com.cottondb.android.activitybin.BinReadimport com.cottondb.android.boot.BootReadimport com.cottondb.android.navigation.Routeimport java.math.BigInteger/** Settings owns its in-place panels; the app still has one Home-rooted route stack. */@Composablefun SettingsRoute( features: FeatureModels, boot: BootRead, visitKey: String, dataRevision: Long, initialPairingId: String?, onBack: () -> Unit, onJoin: () -> Unit, onLeave: () -> Unit, onError: (String) -> Unit, navigate: (String) -> Unit,) { val model = features.settingsFor(visitKey, boot) val state by model.state.collectAsState() val bin = remember(features) { features.activityBin.bin() } val binState by bin.state.collectAsState() var keyboard by rememberSaveable(visitKey) { mutableStateOf(false) } var keyboardSetupError by remember { mutableStateOf<String?>(null) } var devices by rememberSaveable(visitKey) { mutableStateOf(initialPairingId != null) } val context = LocalContext.current LaunchedEffect(model, boot, dataRevision) { model.updateBoot(boot) model.refresh() } LaunchedEffect(bin, boot.paired, dataRevision) { if (boot.paired) bin.refresh() } val closeDevices: () -> Unit = { if (initialPairingId != null) onBack() else devices = false } BackHandler(devices, closeDevices) BackHandler(keyboard) { keyboard = false } if (keyboard) { val keyboardModel = features.keyboard val keyboardState = keyboardModel?.state?.collectAsState()?.value KeyboardSettingsScreen( typedCapture = keyboardState?.typedCapture == true, problem = keyboardSetupError ?: keyboardState?.problem, onBack = { keyboard = false }, onTypedCaptureChange = { keyboardModel?.setTypedCapture(it) }, enabled = keyboardState?.ready == true && !keyboardState.saving, onEnableKeyboard = { try { context.startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS)); keyboardSetupError = null } catch (_: RuntimeException) { keyboardSetupError = "Keyboard settings could not be opened." } }, onSelectKeyboard = { try { val inputMethods = context.getSystemService(android.view.inputmethod.InputMethodManager::class.java) ?: error("No input method service") inputMethods.showInputMethodPicker() keyboardSetupError = null } catch (_: RuntimeException) { keyboardSetupError = "The keyboard picker could not be opened." } }, ) } else if (devices) { DevicesScreen( model = features.devicesFor(visitKey), writerScope = features.writerScope, databaseName = boot.serverName, onBack = closeDevices, onLeave = onLeave.takeIf { boot.serverUrl != null }, initialPairingId = initialPairingId, dataRevision = dataRevision, clock = features.clock, ) } else { SettingsScreen( state = state, onBack = onBack, onRefresh = model::refresh, onJoin = onJoin, onOpenDevices = { devices = true }, onOpenNotificationSettings = { try { context.startActivity(Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) .putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)) } catch (_: ActivityNotFoundException) { onError("Notification settings could not be opened.") } catch (_: SecurityException) { onError("This phone could not open notification settings.") } }, onSyncNow = { model.syncNow(features.writerScope) }, onOpenBin = { navigate(Route.Bin.path) }, binLabel = binLabel(binState), onOpenPrivacy = { navigate(Route.LegalPrivacy.path) }, onOpenTerms = { navigate(Route.LegalTerms.path) }, onOpenKeyboard = { keyboard = true }, ) }}private fun binLabel(state: ActivityBinState<BinRead>): String { val read = state.read return when { state.readProblem != null -> "could not read" state.loading || read == null -> "" else -> when (val total = BinPresentationFacts.total(read.counts)) { BigInteger.ZERO -> "empty" BigInteger.ONE -> "1 item" else -> "$total items" } }}