Fix login screen bug by adding indirection between DataStore and viewModel

This commit is contained in:
2026-01-17 17:30:03 +01:00
parent f979d1b68f
commit 625453692b
2 changed files with 14 additions and 16 deletions

View File

@@ -51,7 +51,7 @@ fun LoginScreen(
val TextSecondary = Color(0xFF9EA3A8) val TextSecondary = Color(0xFF9EA3A8)
// Observe ViewModel state // Observe ViewModel state
val serverUrl by viewModel.url.collectAsState("") val serverUrl by viewModel.url.collectAsState()
val username by viewModel.username.collectAsState() val username by viewModel.username.collectAsState()
val password by viewModel.password.collectAsState() val password by viewModel.password.collectAsState()
@@ -118,7 +118,7 @@ fun LoginScreen(
PurefinComplexTextField( PurefinComplexTextField(
label = "Server URL", label = "Server URL",
value = serverUrl, value = serverUrl,
onValueChange = { coroutineScope.launch { viewModel.setUrl(it) } }, onValueChange = { viewModel.setUrl(it) },
placeholder = "http://192.168.1.100:8096", placeholder = "http://192.168.1.100:8096",
leadingIcon = Icons.Default.Storage leadingIcon = Icons.Default.Storage
) )

View File

@@ -6,12 +6,10 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import hu.bbara.purefin.client.JellyfinApiClient import hu.bbara.purefin.client.JellyfinApiClient
import hu.bbara.purefin.session.UserSessionRepository import hu.bbara.purefin.session.UserSessionRepository
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
@@ -23,18 +21,17 @@ class LoginViewModel @Inject constructor(
val username: StateFlow<String> = _username.asStateFlow() val username: StateFlow<String> = _username.asStateFlow()
private val _password = MutableStateFlow("") private val _password = MutableStateFlow("")
val password: StateFlow<String> = _password.asStateFlow() val password: StateFlow<String> = _password.asStateFlow()
private val _url = MutableStateFlow("")
val url: StateFlow<String> = _url.asStateFlow()
val url: StateFlow<String> = userSessionRepository.session.map { init {
it.url viewModelScope.launch {
}.onStart { } _url.value = userSessionRepository.serverUrl.first()
.stateIn( }
scope = viewModelScope, }
started = SharingStarted.WhileSubscribed(5000),
initialValue = ""
)
suspend fun setUrl(url: String) { fun setUrl(url: String) {
userSessionRepository.setServerUrl(url) _url.value = url
} }
fun setUsername(username: String) { fun setUsername(username: String) {
@@ -52,6 +49,7 @@ class LoginViewModel @Inject constructor(
} }
suspend fun login(): Boolean { suspend fun login(): Boolean {
userSessionRepository.setServerUrl(url.value)
return jellyfinApiClient.login(url.value, username.value, password.value) return jellyfinApiClient.login(url.value, username.value, password.value)
} }