From 865e7efcea8341b70a7dc32d04e19f749b93a874 Mon Sep 17 00:00:00 2001 From: Adit Lal Date: Wed, 18 Feb 2026 21:48:10 +0530 Subject: [PATCH] Refactor domainFlows to a proper StateFlow Replace mutable side-effect property with a dedicated StateFlow for more robust and predictable reactive state management. --- .../adbcommander/AdbCommanderViewModel.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/features/adbcommander/AdbCommanderViewModel.kt b/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/features/adbcommander/AdbCommanderViewModel.kt index a409edc4..edb8e5ea 100644 --- a/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/features/adbcommander/AdbCommanderViewModel.kt +++ b/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/features/adbcommander/AdbCommanderViewModel.kt @@ -55,16 +55,20 @@ class AdbCommanderViewModel( private val localState = MutableStateFlow(AdbCommanderUiState()) private var flowExecutionJob: Job? = null - private var domainFlows: List = emptyList() + + private val domainFlows: StateFlow> = observeFlowsUseCase() + .flowOn(dispatcherProvider.viewModel) + .stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(5_000), + initialValue = emptyList(), + ) val uiState: StateFlow = combine( localState, observeSavedCommandsUseCase().mapLatest { list -> list.map { it.toUiModel() } }, observeCommandHistoryUseCase().mapLatest { list -> list.map { it.toUiModel() } }, - observeFlowsUseCase().mapLatest { list -> - domainFlows = list - list.map { it.toUiModel() } - }, + domainFlows.mapLatest { list -> list.map { it.toUiModel() } }, ) { local, savedCommands, history, flows -> local.copy( savedCommands = savedCommands, @@ -189,7 +193,7 @@ class AdbCommanderViewModel( // Flow editor fun onShowFlowEditor(flowId: Long? = null) { if (flowId != null) { - val flow = domainFlows.find { it.id == flowId } + val flow = domainFlows.value.find { it.id == flowId } localState.update { it.copy( showFlowEditor = true, @@ -333,7 +337,7 @@ class AdbCommanderViewModel( } fun onExecuteFlow(flowId: Long) { - val flow = domainFlows.find { it.id == flowId } ?: return + val flow = domainFlows.value.find { it.id == flowId } ?: return flowExecutionJob?.cancel() localState.update { it.copy(selectedTab = AdbCommanderTab.Runner) }