diff --git a/mvvm-state-core/src/commonMain/kotlin/dev/icerock/moko/mvvm/state/Merges.kt b/mvvm-state-core/src/commonMain/kotlin/dev/icerock/moko/mvvm/state/Merges.kt new file mode 100644 index 0000000..bd2c7ff --- /dev/null +++ b/mvvm-state-core/src/commonMain/kotlin/dev/icerock/moko/mvvm/state/Merges.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.mvvm.state + +fun mergeState( + firstState: ResourceState, + secondState: ResourceState, + function: (T1, T2) -> OT +): ResourceState = when { + (firstState is ResourceState.Loading || secondState is ResourceState.Loading) -> ResourceState.Loading() + (firstState is ResourceState.Failed) -> ResourceState.Failed(firstState.error) + (secondState is ResourceState.Failed) -> ResourceState.Failed(secondState.error) + (firstState is ResourceState.Empty || secondState is ResourceState.Empty) -> ResourceState.Empty() + (firstState is ResourceState.Success && secondState is ResourceState.Success) -> ResourceState.Success( + function( + firstState.data, + secondState.data + ) + ) + else -> ResourceState.Empty() +} diff --git a/mvvm-state-flow/src/commonMain/kotlin/dev/icerock/moko/mvvm/flow/FlowStateMerges.kt b/mvvm-state-flow/src/commonMain/kotlin/dev/icerock/moko/mvvm/flow/FlowStateMerges.kt index de4b74c..7244140 100644 --- a/mvvm-state-flow/src/commonMain/kotlin/dev/icerock/moko/mvvm/flow/FlowStateMerges.kt +++ b/mvvm-state-flow/src/commonMain/kotlin/dev/icerock/moko/mvvm/flow/FlowStateMerges.kt @@ -5,6 +5,7 @@ package dev.icerock.moko.mvvm.flow import dev.icerock.moko.mvvm.state.ResourceState +import dev.icerock.moko.mvvm.state.mergeState import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine @@ -13,22 +14,7 @@ fun Flow>.concatData( function: (T1, T2) -> OT ): Flow> = combine( - this, - flow + this, flow ) { firstState, secondState -> - val state: ResourceState = when { - (firstState is ResourceState.Loading || secondState is ResourceState.Loading) -> ResourceState.Loading() - (firstState is ResourceState.Failed) -> ResourceState.Failed(firstState.error) - (secondState is ResourceState.Failed) -> ResourceState.Failed(secondState.error) - (firstState is ResourceState.Empty || secondState is ResourceState.Empty) -> ResourceState.Empty() - (firstState is ResourceState.Success && secondState is ResourceState.Success) -> ResourceState.Success( - function( - firstState.data, - secondState.data - ) - ) - else -> ResourceState.Empty() - } - - state + mergeState(firstState, secondState, function) } diff --git a/mvvm-state-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/StateLiveDataMerges.kt b/mvvm-state-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/StateLiveDataMerges.kt index 33fd100..a7237b8 100644 --- a/mvvm-state-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/StateLiveDataMerges.kt +++ b/mvvm-state-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/StateLiveDataMerges.kt @@ -5,25 +5,12 @@ package dev.icerock.moko.mvvm.livedata import dev.icerock.moko.mvvm.state.ResourceState +import dev.icerock.moko.mvvm.state.mergeState fun LiveData>.concatData( liveData: LiveData>, function: (T1, T2) -> OT ): LiveData> = mediatorOf(this, liveData) { firstState, secondState -> - val state: ResourceState = when { - (firstState is ResourceState.Loading || secondState is ResourceState.Loading) -> ResourceState.Loading() - (firstState is ResourceState.Failed) -> ResourceState.Failed(firstState.error) - (secondState is ResourceState.Failed) -> ResourceState.Failed(secondState.error) - (firstState is ResourceState.Empty || secondState is ResourceState.Empty) -> ResourceState.Empty() - (firstState is ResourceState.Success && secondState is ResourceState.Success) -> ResourceState.Success( - function( - firstState.data, - secondState.data - ) - ) - else -> ResourceState.Empty() - } - - state + mergeState(firstState, secondState, function) }