SyncDialogs.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/sync/SyncDialogs.kt
1package eu.friedcutlet.yokan.ui.screens.settings.sync23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.PaddingValues6import androidx.compose.foundation.layout.fillMaxWidth7import androidx.compose.foundation.shape.RoundedCornerShape8import androidx.compose.foundation.text.KeyboardActions9import androidx.compose.foundation.text.KeyboardOptions10import androidx.compose.material.icons.Icons11import androidx.compose.material.icons.filled.Close12import androidx.compose.material.icons.filled.Visibility13import androidx.compose.material.icons.filled.VisibilityOff14import androidx.compose.material3.AlertDialog15import androidx.compose.material3.Button16import androidx.compose.material3.DropdownMenuItem17import androidx.compose.material3.ExperimentalMaterial3Api18import androidx.compose.material3.ExposedDropdownMenuAnchorType19import androidx.compose.material3.ExposedDropdownMenuBox20import androidx.compose.material3.ExposedDropdownMenuDefaults21import androidx.compose.material3.Icon22import androidx.compose.material3.IconButton23import androidx.compose.material3.MaterialTheme24import androidx.compose.material3.OutlinedTextField25import androidx.compose.material3.Text26import androidx.compose.material3.TextButton27import androidx.compose.runtime.Composable28import androidx.compose.runtime.getValue29import androidx.compose.runtime.mutableStateOf30import androidx.compose.runtime.remember31import androidx.compose.runtime.setValue32import androidx.compose.ui.Modifier33import androidx.compose.ui.text.font.FontWeight34import androidx.compose.ui.text.input.ImeAction35import androidx.compose.ui.text.input.KeyboardType36import androidx.compose.ui.text.input.PasswordVisualTransformation37import androidx.compose.ui.text.input.VisualTransformation38import androidx.compose.ui.unit.dp39import eu.friedcutlet.yokan.services.sync.CloudProviderDescriptor40import eu.friedcutlet.yokan.database.SyncServerProvider41import eu.friedcutlet.yokan.ui.components.compactDialogWidth42import org.jetbrains.compose.resources.stringResource43import yokan.composeapp.generated.resources.Res44import yokan.composeapp.generated.resources.action_cancel45import yokan.composeapp.generated.resources.password46import yokan.composeapp.generated.resources.ui_server_url47import yokan.composeapp.generated.resources.ui_username_optional48import yokan.composeapp.generated.resources.sync_email49import yokan.composeapp.generated.resources.sync_username5051/** One row of the server dropdown; only servers the user added can be removed. */52internal data class HubServerChoice(val name: String, val url: String, val removable: Boolean)5354@OptIn(ExperimentalMaterial3Api::class)55@Composable56internal fun SyncLoginDialog(57 provider: SyncServerProvider,58 initialServer: String,59 initialEmail: String,60 servers: List<HubServerChoice>,61 /** Null where the platform browser cannot report the pages it loads (the web build). */62 onBrowserLogin: ((server: String) -> Unit)?,63 onRemoveServer: (url: String) -> Unit,64 onDismiss: () -> Unit,65 onSubmit: (server: String, email: String, password: String) -> Unit,66) {67 var server by remember { mutableStateOf(initialServer.ifBlank { servers.firstOrNull()?.url.orEmpty() }) }68 var email by remember(provider) { mutableStateOf(initialEmail) }69 var password by remember { mutableStateOf("") }70 var serversOpen by remember { mutableStateOf(false) }71 val canSubmit = server.isNotBlank() && email.isNotBlank() && password.isNotBlank()72 fun submitLogin() {73 if (!canSubmit) return74 onSubmit(server, email, password)75 onDismiss()76 }77 AlertDialog(78 onDismissRequest = onDismiss,79 modifier = Modifier.compactDialogWidth(),80 shape = MaterialTheme.shapes.extraLarge,81 title = {82 Text(83 "Sign in to sync server",84 style = MaterialTheme.typography.titleMedium,85 fontWeight = FontWeight.SemiBold,86 )87 },88 text = {89 Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {90 // Editable: pick a listed server or type any URL (kept in the list after a successful sign-in).91 ExposedDropdownMenuBox(expanded = serversOpen && servers.isNotEmpty(), onExpandedChange = { serversOpen = it }) {92 OutlinedTextField(93 value = server,94 onValueChange = { server = it },95 label = { Text(stringResource(Res.string.ui_server_url)) },96 singleLine = true,97 shape = MaterialTheme.shapes.large,98 trailingIcon = { if (servers.isNotEmpty()) ExposedDropdownMenuDefaults.TrailingIcon(expanded = serversOpen) },99 modifier = Modifier.fillMaxWidth().menuAnchor(ExposedDropdownMenuAnchorType.PrimaryEditable),100 )101 ExposedDropdownMenu(expanded = serversOpen && servers.isNotEmpty(), onDismissRequest = { serversOpen = false }) {102 servers.forEach { choice ->103 DropdownMenuItem(104 text = {105 Column {106 Text(choice.name)107 if (choice.name != choice.url) {108 Text(choice.url, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)109 }110 }111 },112 onClick = {113 server = choice.url114 serversOpen = false115 },116 trailingIcon = if (choice.removable) {117 { IconButton(onClick = { onRemoveServer(choice.url) }) { Icon(Icons.Filled.Close, contentDescription = "Remove ${choice.url}") } }118 } else {119 null120 },121 )122 }123 }124 }125 OutlinedTextField(126 value = email,127 onValueChange = { email = it },128 label = { Text(if (provider == SyncServerProvider.Mangayomi) stringResource(Res.string.sync_username) else stringResource(Res.string.sync_email)) },129 singleLine = true,130 shape = MaterialTheme.shapes.large,131 modifier = Modifier.fillMaxWidth(),132 )133 var passwordVisible by remember { mutableStateOf(false) }134 OutlinedTextField(135 value = password,136 onValueChange = { password = it },137 label = { Text(stringResource(Res.string.password)) },138 singleLine = true,139 shape = MaterialTheme.shapes.large,140 modifier = Modifier.fillMaxWidth(),141 visualTransformation = if (passwordVisible)142 VisualTransformation.None143 else144 PasswordVisualTransformation(),145 keyboardOptions = KeyboardOptions(146 keyboardType = KeyboardType.Password,147 autoCorrectEnabled = false,148 imeAction = ImeAction.Done,149 ),150 keyboardActions = KeyboardActions(onDone = { submitLogin() }),151 trailingIcon = {152 IconButton(onClick = { passwordVisible = !passwordVisible }) {153 Icon(154 imageVector = if (passwordVisible) Icons.Filled.VisibilityOff else Icons.Filled.Visibility,155 contentDescription = if (passwordVisible) "Hide password" else "Show password",156 )157 }158 },159 )160 if (onBrowserLogin != null) {161 TextButton(162 onClick = {163 onBrowserLogin(server)164 onDismiss()165 },166 enabled = server.isNotBlank(),167 ) { Text("Sign in in browser instead") }168 }169 }170 },171 confirmButton = {172 Button(173 onClick = { submitLogin() },174 enabled = canSubmit,175 shape = RoundedCornerShape(50),176 contentPadding = PaddingValues(horizontal = 20.dp, vertical = 8.dp),177 ) { Text("Log in", style = MaterialTheme.typography.labelLarge) }178 },179 dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } },180 )181}182183@Composable184internal fun CloudProviderConnectDialog(185 provider: CloudProviderDescriptor,186 initialAccount: String,187 onDismiss: () -> Unit,188 onSubmit: (accountLabel: String?, accessToken: String) -> Unit,189) {190 var account by remember(provider.id) { mutableStateOf(initialAccount) }191 var accessToken by remember(provider.id) { mutableStateOf("") }192 val canSubmit = accessToken.isNotBlank()193 fun submit() {194 if (!canSubmit) return195 onSubmit(account.ifBlank { null }, accessToken)196 onDismiss()197 }198 AlertDialog(199 onDismissRequest = onDismiss,200 modifier = Modifier.compactDialogWidth(),201 shape = MaterialTheme.shapes.extraLarge,202 title = {203 Text(204 "Connect ${provider.displayName}",205 style = MaterialTheme.typography.titleMedium,206 fontWeight = FontWeight.SemiBold,207 )208 },209 text = {210 Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {211 Text(212 provider.authInstructions,213 style = MaterialTheme.typography.bodySmall,214 color = MaterialTheme.colorScheme.onSurfaceVariant,215 )216 OutlinedTextField(217 value = account,218 onValueChange = { account = it },219 label = { Text(stringResource(Res.string.ui_username_optional)) },220 singleLine = true,221 shape = MaterialTheme.shapes.large,222 modifier = Modifier.fillMaxWidth(),223 )224 OutlinedTextField(225 value = accessToken,226 onValueChange = { accessToken = it.trim() },227 label = { Text("OAuth access token") },228 singleLine = true,229 shape = MaterialTheme.shapes.large,230 modifier = Modifier.fillMaxWidth(),231 )232 }233 },234 confirmButton = {235 Button(236 onClick = { submit() },237 enabled = canSubmit,238 shape = RoundedCornerShape(50),239 contentPadding = PaddingValues(horizontal = 20.dp, vertical = 8.dp),240 ) { Text("Connect", style = MaterialTheme.typography.labelLarge) }241 },242 dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } },243 )244}245