1package soy.iko.opencode.ui.server
3import androidx.lifecycle.ViewModel
4import androidx.lifecycle.viewModelScope
5import soy.iko.opencode.data.model.ServerProfile
6import soy.iko.opencode.di.AppContainer
7import kotlinx.coroutines.flow.MutableStateFlow
8import kotlinx.coroutines.flow.StateFlow
9import kotlinx.coroutines.flow.asStateFlow
10import kotlinx.coroutines.flow.first
11import kotlinx.coroutines.launch
14data class ServerEditState(
15 val id: String? = null,
16 val label: String = "",
17 val baseUrl: String = "",
18 val username: String = "",
19 val password: String = "",
20 val loaded: Boolean = false,
22 val canSave: Boolean get() = baseUrl.isNotBlank()
23 val isNew: Boolean get() = id == null
26class ServerEditViewModel(
27 private val container: AppContainer,
28 private val profileId: String?,
31 private val _state = MutableStateFlow(ServerEditState(id = profileId))
32 val state: StateFlow<ServerEditState> = _state.asStateFlow()
35 viewModelScope.launch {
36 if (profileId != null) {
37 val existing = container.profileStore.profiles.first().firstOrNull { it.id == profileId }
38 if (existing != null) {
39 _state.value = ServerEditState(
41 label = existing.label,
42 baseUrl = existing.baseUrl,
43 username = existing.username.orEmpty(),
44 password = existing.password.orEmpty(),
50 _state.value = _state.value.copy(loaded = true)
54 fun update(transform: (ServerEditState) -> ServerEditState) {
55 _state.value = transform(_state.value)
58 fun save(onDone: () -> Unit) {
60 if (!s.canSave) return
61 viewModelScope.launch {
62 val existingLastUsed = if (s.id != null) {
63 container.profileStore.profiles.first()
64 .firstOrNull { it.id == s.id }?.lastUsed ?: 0L
66 container.profileStore.save(
68 id = s.id ?: UUID.randomUUID().toString(),
69 label = s.label.trim(),
70 baseUrl = s.baseUrl.trim(),
71 username = s.username.trim().takeIf { it.isNotBlank() },
72 password = s.password.trim().takeIf { it.isNotEmpty() },
73 lastUsed = existingLastUsed,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list