From 4756b6801b7c3cd7d84ee601206db9b8cea5e4e7 Mon Sep 17 00:00:00 2001 From: M57 <41645758+Hy4ri@users.noreply.github.com> Date: Thu, 25 Jun 2026 20:29:55 +0300 Subject: [PATCH] perf(#203): parallelize API calls in ModelViewModel for faster loading (#361) Co-authored-by: M57 --- .../hermescontrol/ui/model/ModelViewModel.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/m57/hermescontrol/ui/model/ModelViewModel.kt b/app/src/main/java/com/m57/hermescontrol/ui/model/ModelViewModel.kt index 9b9ca8a8..066465b3 100644 --- a/app/src/main/java/com/m57/hermescontrol/ui/model/ModelViewModel.kt +++ b/app/src/main/java/com/m57/hermescontrol/ui/model/ModelViewModel.kt @@ -10,6 +10,7 @@ import com.m57.hermescontrol.data.remote.NetworkResult import com.m57.hermescontrol.data.remote.safeApiCall import com.m57.hermescontrol.ui.common.ToastHost import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow @@ -32,19 +33,23 @@ class ModelViewModel : ViewModel(), ToastHost { fun loadModelOptions() { _uiState.update { it.copy(isLoading = true, errorMessage = null) } viewModelScope.launch { - val responseResult = - withContext(Dispatchers.IO) { + val responseResultDeferred = + async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getModelOptions() } } - val activeProfileNameResResult = - withContext(Dispatchers.IO) { + val activeProfileNameResResultDeferred = + async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getActiveProfile() } } - val profilesResResult = - withContext(Dispatchers.IO) { + val profilesResResultDeferred = + async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getProfiles() } } + val responseResult = responseResultDeferred.await() + val activeProfileNameResResult = activeProfileNameResResultDeferred.await() + val profilesResResult = profilesResResultDeferred.await() + when (responseResult) { is NetworkResult.Success -> { val activeName =