package com.cottondb.android.ui.settings import android.content.ActivityNotFoundException import android.content.Intent import android.provider.Settings import androidx.activity.compose.BackHandler import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import com.cottondb.android.FeatureModels import com.cottondb.android.activity.BinPresentationFacts import com.cottondb.android.activitybin.ActivityBinState import com.cottondb.android.activitybin.BinRead import com.cottondb.android.boot.BootRead import com.cottondb.android.navigation.Route import java.math.BigInteger /** Settings owns its in-place panels; the app still has one Home-rooted route stack. */ @Composable fun 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(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): 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" } } }