GeneralSettingsScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/general/GeneralSettingsScreen.kt
1package eu.friedcutlet.yokan.ui.screens.settings.general23import androidx.compose.foundation.BorderStroke4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.ColumnScope6import androidx.compose.foundation.layout.Row7import androidx.compose.foundation.layout.Spacer8import androidx.compose.foundation.layout.fillMaxWidth9import androidx.compose.foundation.layout.height10import androidx.compose.foundation.layout.padding11import androidx.compose.foundation.layout.width12import androidx.compose.foundation.shape.RoundedCornerShape13import androidx.compose.material.icons.Icons14import androidx.compose.material.icons.filled.Restore15import androidx.compose.material3.Icon16import androidx.compose.material3.MaterialTheme17import androidx.compose.material3.OutlinedButton18import androidx.compose.material3.Slider19import androidx.compose.material3.Surface20import androidx.compose.material3.Text21import androidx.compose.runtime.Composable22import androidx.compose.runtime.getValue23import androidx.compose.runtime.mutableStateOf24import androidx.compose.runtime.remember25import androidx.compose.runtime.setValue26import androidx.compose.ui.Alignment27import androidx.compose.ui.Modifier28import androidx.compose.ui.hapticfeedback.HapticFeedbackType29import androidx.compose.ui.platform.LocalHapticFeedback30import androidx.compose.ui.text.font.FontWeight31import androidx.compose.ui.unit.dp32import androidx.compose.ui.unit.sp33import eu.friedcutlet.yokan.database.DbAlgorithmWeights34import eu.friedcutlet.yokan.database.DbSettings35import eu.friedcutlet.yokan.database.DefaultUserAgent36import eu.friedcutlet.yokan.services.http.CustomDohProviderId37import eu.friedcutlet.yokan.services.http.DohProviderOption38import eu.friedcutlet.yokan.services.http.DohProviders39import eu.friedcutlet.yokan.services.http.platformSupportsCustomDns40import eu.friedcutlet.yokan.ui.components.ChoiceDialog41import eu.friedcutlet.yokan.ui.formatDecimal42import eu.friedcutlet.yokan.ui.screens.settings.SettingTile43import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold44import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile45import eu.friedcutlet.yokan.ui.screens.settings.TextInputDialog46import org.jetbrains.compose.resources.stringResource47import yokan.composeapp.generated.resources.Res48import yokan.composeapp.generated.resources.action_reset49import yokan.composeapp.generated.resources.default_user_agent50import yokan.composeapp.generated.resources.tile_dns_over_https5152@Composable53internal fun GeneralSettingsScreen(54 title: String,55 settings: DbSettings,56 onUpdate: ((DbSettings) -> DbSettings) -> Unit,57 onBack: () -> Unit,58) {59 val weights = settings.algorithmWeights ?: DbAlgorithmWeights()60 var dohProviderDialog by remember { mutableStateOf(false) }61 var customDohDialog by remember { mutableStateOf(false) }62 var userAgentDialog by remember { mutableStateOf(false) }63 var cfProxyDialog by remember { mutableStateOf(false) }64 val currentDohProvider = if (settings.dohProviderId == CustomDohProviderId) {65 DohProviderOption(CustomDohProviderId, "Custom", settings.customDohUrl)66 } else {67 DohProviders.firstOrNull { it.id == settings.dohProviderId } ?: DohProviders[0]68 }69 val dohProviderSubtitle = if (settings.dohProviderId == CustomDohProviderId) {70 if (settings.customDohUrl.isNotBlank()) "Custom · ${settings.customDohUrl}" else "Custom"71 } else {72 currentDohProvider.name73 }7475 if (dohProviderDialog) {76 val allOptions = DohProviders + DohProviderOption(CustomDohProviderId, "Custom", settings.customDohUrl)77 ChoiceDialog(78 title = "DNS provider",79 options = allOptions,80 selected = currentDohProvider,81 label = { opt ->82 if (opt.id == CustomDohProviderId && settings.customDohUrl.isNotBlank()) {83 "Custom (${settings.customDohUrl})"84 } else {85 opt.name86 }87 },88 onSelect = { provider ->89 dohProviderDialog = false90 if (provider.id == CustomDohProviderId) {91 customDohDialog = true92 } else {93 onUpdate { it.copy(dohProviderId = provider.id, dohEnabled = true) }94 }95 },96 onDismiss = { dohProviderDialog = false },97 )98 }99 if (customDohDialog) {100 TextInputDialog(101 title = "Custom DoH URL",102 initialValue = settings.customDohUrl,103 placeholder = "https://example.com/dns-query",104 helperText = "Must be an https DoH (JSON) endpoint",105 onConfirm = { raw ->106 val trimmed = raw.trim()107 val url = if (trimmed.isNotBlank() && !trimmed.startsWith("http://") && !trimmed.startsWith("https://")) {108 "https://$trimmed"109 } else {110 trimmed111 }112 onUpdate {113 it.copy(114 customDohUrl = url,115 dohProviderId = CustomDohProviderId,116 dohEnabled = true,117 )118 }119 },120 onDismiss = { customDohDialog = false },121 )122 }123 if (userAgentDialog) {124 TextInputDialog(125 title = stringResource(Res.string.default_user_agent),126 initialValue = settings.userAgent,127 placeholder = DefaultUserAgent,128 singleLine = false,129 onConfirm = { value -> onUpdate { it.copy(userAgent = value.trim().ifBlank { DefaultUserAgent }) } },130 onDismiss = { userAgentDialog = false },131 )132 }133 if (cfProxyDialog) {134 TextInputDialog(135 title = "Cloudflare bypass proxy",136 initialValue = settings.cloudflareBypassProxyUrl,137 placeholder = "http://localhost:8191/v1",138 helperText = "Any FlareSolverr-compatible solver (FlareSolverr, Byparr); a bare host:port gets /v1. Leave empty to disable.",139 onConfirm = { value -> onUpdate { it.copy(cloudflareBypassProxyUrl = value.trim()) } },140 onDismiss = { cfProxyDialog = false },141 )142 }143144 SettingsScaffold(title, onBack) {145 // DoH expansion header: taps the row to toggle, and when enabled exposes a "DNS provider" sub-tile.146 // Darwin and browser engines resolve hosts themselves, so those platforms hide it.147 if (platformSupportsCustomDns) {148 item(key = "doh") {149 SwitchTile(150 stringResource(Res.string.tile_dns_over_https),151 if (settings.dohEnabled) dohProviderSubtitle else null,152 settings.dohEnabled,153 { value -> onUpdate { it.copy(dohEnabled = value) } },154 )155 }156 }157 if (platformSupportsCustomDns && settings.dohEnabled) {158 item {159 SettingTile(160 "DNS provider",161 dohProviderSubtitle,162 onClick = {163 if (settings.dohProviderId == CustomDohProviderId && settings.customDohUrl.isBlank()) {164 customDohDialog = true165 } else {166 dohProviderDialog = true167 }168 },169 )170 }171 }172 item(key = "user-agent") { SettingTile(stringResource(Res.string.default_user_agent), settings.userAgent.ifBlank { DefaultUserAgent }, onClick = { userAgentDialog = true }) }173 item {174 SettingTile(175 "Cloudflare bypass proxy",176 settings.cloudflareBypassProxyUrl.ifBlank { "Disabled" },177 onClick = { cfProxyDialog = true },178 )179 }180 item {181 BorderedGroup(182 title = "Recommendations weights",183 onReset = { onUpdate { it.copy(algorithmWeights = DbAlgorithmWeights()) } },184 ) {185 WeightSlider("Genre", weights.genre / 100f, { value -> onUpdate { it.copy(algorithmWeights = weights.copy(genre = (value * 100).toInt())) } })186 WeightSlider("Setting", weights.setting / 100f, { value -> onUpdate { it.copy(algorithmWeights = weights.copy(setting = (value * 100).toInt())) } })187 WeightSlider("Synopsis", weights.synopsis / 100f, { value -> onUpdate { it.copy(algorithmWeights = weights.copy(synopsis = (value * 100).toInt())) } })188 WeightSlider("Theme", weights.theme / 100f, { value -> onUpdate { it.copy(algorithmWeights = weights.copy(theme = (value * 100).toInt())) } })189 }190 }191 }192}193194@Composable195private fun BorderedGroup(196 title: String,197 onReset: () -> Unit,198 content: @Composable ColumnScope.() -> Unit,199) {200 Surface(201 modifier = Modifier202 .fillMaxWidth()203 .padding(20.dp),204 shape = RoundedCornerShape(5.dp),205 border = BorderStroke(3.dp, MaterialTheme.colorScheme.primary),206 ) {207 Column(modifier = Modifier.padding(10.dp)) {208 Row(verticalAlignment = Alignment.CenterVertically) {209 Text(title, fontWeight = FontWeight.Bold)210 Spacer(Modifier.width(20.dp))211 OutlinedButton(onClick = onReset) {212 Icon(Icons.Filled.Restore, contentDescription = null)213 Spacer(Modifier.width(8.dp))214 Text(stringResource(Res.string.action_reset))215 }216 }217 Spacer(Modifier.height(10.dp))218 content()219 }220 }221}222223@Composable224private fun WeightSlider(title: String, value: Float, onChange: (Float) -> Unit) {225 val hapticFeedback = LocalHapticFeedback.current226 Column(227 modifier = Modifier.padding(8.dp),228 horizontalAlignment = Alignment.Start,229 ) {230 Text(title)231 Text(232 formatDecimal(value.toDouble(), 2),233 fontSize = 11.sp,234 color = MaterialTheme.colorScheme.secondary,235 )236 Spacer(Modifier.height(20.dp))237 Slider(238 value = value,239 onValueChange = { nextValue ->240 if ((nextValue * 100).toInt() != (value * 100).toInt()) {241 hapticFeedback.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick)242 }243 onChange(nextValue)244 },245 )246 }247}248