From 2b3393fe3301701123381e58da8f49af38c14e55 Mon Sep 17 00:00:00 2001 From: Mark Dubkov Date: Sun, 26 Nov 2023 22:04:39 +0300 Subject: [PATCH] feat: refactoring --- gradle/libs.versions.toml | 2 +- mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHolder.kt | 6 +++--- mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHost.kt | 2 +- .../kotlin/app/meetacy/vm/mvi/StateHostedViewModel.kt | 6 ++++-- .../commonTest/kotlin/app/meetacy/vm/mvi/SomeViewModel.kt | 1 + 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9173504..c499806 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,7 @@ androidGradle = "7.4.2" androidLifecycleVersion = "2.6.2" kotlinxCoroutines = "1.7.3" -mvm = "0.0.7" +mvm = "0.0.8" [libraries] diff --git a/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHolder.kt b/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHolder.kt index db8db48..4aa9ac3 100644 --- a/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHolder.kt +++ b/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHolder.kt @@ -8,7 +8,7 @@ public abstract class StateHolder { public abstract val effects: CFlow public abstract val states: CStateFlow - public abstract suspend fun accept(intent: Intent) - public abstract suspend fun accept(effect: TEffect) - public abstract fun accept(newState: TState) + internal abstract suspend fun accept(intent: Intent) + internal abstract suspend fun perform(effect: TEffect) + internal abstract fun accept(newState: TState) } \ No newline at end of file diff --git a/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHost.kt b/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHost.kt index 108f6a0..767aa27 100644 --- a/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHost.kt +++ b/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHost.kt @@ -26,7 +26,7 @@ public fun StateHost.holder( } } - override suspend fun accept(effect: TEffect) { + override suspend fun perform(effect: TEffect) { _effects.send(effect) } diff --git a/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHostedViewModel.kt b/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHostedViewModel.kt index fc52735..bc27faa 100644 --- a/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHostedViewModel.kt +++ b/mvi/src/commonMain/kotlin/app/meetacy/vm/mvi/StateHostedViewModel.kt @@ -9,6 +9,8 @@ import kotlinx.coroutines.launch public abstract class StateHostedViewModel: ViewModel(), StateHost { + protected val state: TState get() = holder.states.value + protected fun viewModelScopeLaunch(block: suspend CoroutineScope.() -> Unit) { viewModelScope.launch(block = block) } @@ -23,7 +25,7 @@ public abstract class StateHostedViewModel: ViewModel(), StateH holder.accept(holder.states.value.transform()) } - protected fun accept(effect: TEffect) { - viewModelScope.launch { holder.accept(effect) } + protected fun perform(effect: TEffect) { + viewModelScope.launch { holder.perform(effect) } } } diff --git a/mvi/src/commonTest/kotlin/app/meetacy/vm/mvi/SomeViewModel.kt b/mvi/src/commonTest/kotlin/app/meetacy/vm/mvi/SomeViewModel.kt index 473dfb4..f1f2d0f 100644 --- a/mvi/src/commonTest/kotlin/app/meetacy/vm/mvi/SomeViewModel.kt +++ b/mvi/src/commonTest/kotlin/app/meetacy/vm/mvi/SomeViewModel.kt @@ -5,6 +5,7 @@ import app.meetacy.vm.extension.launchIn class SomeViewModel: StateHostedViewModel() { override val holder: StateHolder = holder(State()) + data class State(val isLoading: Boolean = true) sealed interface Effect