ProfilesScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/profiles/ProfilesScreen.kt
1package eu.friedcutlet.yokan.ui.screens.settings.profiles23import androidx.compose.foundation.layout.*4import androidx.compose.foundation.lazy.items5import androidx.compose.material3.*6import androidx.compose.runtime.*7import androidx.compose.ui.Modifier8import androidx.compose.ui.unit.dp9import eu.friedcutlet.yokan.app.profiles.*10import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold11import org.jetbrains.compose.resources.stringResource12import yokan.composeapp.generated.resources.*13import eu.friedcutlet.yokan.domain.ProfileId1415internal data class ProfileControls(16 val state: ProfilesState,17 val switchTo: (ProfileId) -> Unit,18 val create: () -> Unit,19 val rename: (ProfileRecord) -> Unit,20 val delete: (ProfileRecord) -> Unit,21 val retry: (ProfileId) -> Unit,22 val refresh: () -> Unit,23 val nameChanged: (String) -> Unit,24 val confirm: () -> Unit,25 val dismiss: () -> Unit,26)27internal val LocalProfileControls = staticCompositionLocalOf<ProfileControls> { error("Profile controls unavailable") }2829@Composable30internal fun ProfilesScreen(onBack: () -> Unit) {31 val controls = LocalProfileControls.current32 val state = controls.state33 SettingsScaffold(stringResource(Res.string.profiles_title), onBack) {34 item(key = "profile-guidance") { Text(stringResource(Res.string.profiles_guidance), modifier = Modifier.padding(16.dp)) }35 item(key = "profile-capabilities") { Text(stringResource(Res.string.profiles_capabilities), style = MaterialTheme.typography.bodySmall, modifier = Modifier.padding(16.dp)) }36 state.error?.let { error -> item(key = "profile-error") { Text(error, color = MaterialTheme.colorScheme.error, modifier = Modifier.padding(16.dp)) } }37 if (!state.available) item(key = "profile-refresh") {38 TextButton(onClick = controls.refresh, enabled = !state.busy) { Text(stringResource(Res.string.profiles_retry)) }39 }40 items(state.registry.profiles, key = { it.id.value }, contentType = { "profile" }) { record ->41 Column(Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp)) {42 Text(record.name, style = MaterialTheme.typography.titleMedium)43 if (record.id == state.activeId) Text(stringResource(Res.string.profiles_active), color = MaterialTheme.colorScheme.primary)44 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {45 when (record.status) {46 ProfileStatus.Ready -> {47 if (record.id != state.activeId) TextButton(onClick = { controls.switchTo(record.id) }, enabled = state.available && !state.busy) { Text(stringResource(Res.string.profiles_switch)) }48 if (record.id != ProfileId.Default) TextButton(onClick = { controls.rename(record) }, enabled = !state.busy) { Text(stringResource(Res.string.profiles_rename)) }49 }50 ProfileStatus.Provisioning -> TextButton(onClick = { controls.retry(record.id) }, enabled = !state.busy) { Text(stringResource(Res.string.profiles_retry)) }51 ProfileStatus.Deleting -> Text(stringResource(Res.string.profiles_deletion_pending))52 }53 if (record.id != ProfileId.Default) TextButton(onClick = { controls.delete(record) }, enabled = !state.busy) { Text(stringResource(Res.string.profiles_delete)) }54 }55 }56 }57 item(key = "profile-create") { Button(onClick = controls.create, enabled = state.available && !state.busy, modifier = Modifier.padding(16.dp)) { Text(stringResource(Res.string.profiles_create)) } }58 if (state.busy) item(key = "profile-busy") { LinearProgressIndicator(Modifier.fillMaxWidth()) }59 }60 when (val dialog = state.dialog) {61 is ProfileDialog.Name -> AlertDialog(62 onDismissRequest = { if (!state.busy) controls.dismiss() },63 title = { Text(stringResource(if (dialog.id == null) Res.string.profiles_create else Res.string.profiles_rename)) },64 text = { Column { OutlinedTextField(dialog.draft, controls.nameChanged, label = { Text(stringResource(Res.string.profiles_name)) }, singleLine = true, enabled = !state.busy); state.error?.let { Text(it, color = MaterialTheme.colorScheme.error) } } },65 confirmButton = { TextButton(onClick = controls.confirm, enabled = !state.busy && dialog.draft.isNotBlank()) { Text(stringResource(Res.string.action_save)) } },66 dismissButton = { TextButton(onClick = controls.dismiss, enabled = !state.busy) { Text(stringResource(Res.string.action_cancel)) } },67 )68 is ProfileDialog.Delete -> AlertDialog(69 onDismissRequest = { if (!state.busy) controls.dismiss() },70 title = { Text(stringResource(Res.string.profiles_delete_named, dialog.profile.name)) },71 text = { Column { Text(stringResource(Res.string.profiles_delete_guidance)); state.error?.let { Text(it, color = MaterialTheme.colorScheme.error) } } },72 confirmButton = { TextButton(onClick = controls.confirm, enabled = !state.busy) { Text(stringResource(Res.string.profiles_delete)) } },73 dismissButton = { TextButton(onClick = controls.dismiss, enabled = !state.busy) { Text(stringResource(Res.string.action_cancel)) } },74 )75 null -> Unit76 }77}7879@Composable80internal fun ProfileQuickSwitch() {81 val controls = LocalProfileControls.current82 val state = controls.state83 if (!state.registry.hasAdditionalProfiles) return84 var expanded by remember { mutableStateOf(false) }85 Box(Modifier.padding(horizontal = 16.dp)) {86 TextButton(onClick = { expanded = true }, enabled = state.available && !state.busy) {87 Text(stringResource(Res.string.profiles_current, state.registry.available(state.activeId)?.name.orEmpty()))88 }89 DropdownMenu(expanded, { expanded = false }) {90 state.registry.profiles.filter { it.status == ProfileStatus.Ready }.forEach { record ->91 key(record.id.value) { DropdownMenuItem(text = { Text(record.name) }, onClick = { expanded = false; controls.switchTo(record.id) }) }92 }93 }94 }95}96