perf(#203): parallelize API calls in ModelViewModel for faster loading (#361)

Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
M57andM57 authored and GitHub committed 2026-06-25 20:29:55 +03:00
1 parent 4ba74c2efa
commit 4756b6801b
1 file changed
+11 -6
@@ -10,6 +10,7 @@ import com.m57.hermescontrol.data.remote.NetworkResult
import com.m57.hermescontrol.data.remote.safeApiCall import com.m57.hermescontrol.data.remote.safeApiCall
import com.m57.hermescontrol.ui.common.ToastHost import com.m57.hermescontrol.ui.common.ToastHost
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
@@ -32,19 +33,23 @@ class ModelViewModel : ViewModel(), ToastHost {
fun loadModelOptions() { fun loadModelOptions() {
_uiState.update { it.copy(isLoading = true, errorMessage = null) } _uiState.update { it.copy(isLoading = true, errorMessage = null) }
viewModelScope.launch { viewModelScope.launch {
val responseResult = val responseResultDeferred =
withContext(Dispatchers.IO) { async(Dispatchers.IO) {
safeApiCall { ApiClient.hermesApi.getModelOptions() } safeApiCall { ApiClient.hermesApi.getModelOptions() }
} }
val activeProfileNameResResult = val activeProfileNameResResultDeferred =
withContext(Dispatchers.IO) { async(Dispatchers.IO) {
safeApiCall { ApiClient.hermesApi.getActiveProfile() } safeApiCall { ApiClient.hermesApi.getActiveProfile() }
} }
val profilesResResult = val profilesResResultDeferred =
withContext(Dispatchers.IO) { async(Dispatchers.IO) {
safeApiCall { ApiClient.hermesApi.getProfiles() } safeApiCall { ApiClient.hermesApi.getProfiles() }
} }
val responseResult = responseResultDeferred.await()
val activeProfileNameResResult = activeProfileNameResResultDeferred.await()
val profilesResResult = profilesResResultDeferred.await()
when (responseResult) { when (responseResult) {
is NetworkResult.Success -> { is NetworkResult.Success -> {
val activeName = val activeName =