Skip to content

Commit

Permalink
icerockdev#183 removing duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubkov committed Sep 7, 2022
1 parent 9d9de65 commit b698560
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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 <T1, E, T2, OT> mergeState(
firstState: ResourceState<T1, E>,
secondState: ResourceState<T2, E>,
function: (T1, T2) -> OT
): ResourceState<OT, E> = 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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,22 +14,7 @@ fun <T1, E, T2, OT> Flow<ResourceState<T1, E>>.concatData(
function: (T1, T2) -> OT
): Flow<ResourceState<OT, E>> =
combine(
this,
flow
this, flow
) { firstState, secondState ->
val state: ResourceState<OT, E> = 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T1, E, T2, OT> LiveData<ResourceState<T1, E>>.concatData(
liveData: LiveData<ResourceState<T2, E>>,
function: (T1, T2) -> OT
): LiveData<ResourceState<OT, E>> =
mediatorOf(this, liveData) { firstState, secondState ->
val state: ResourceState<OT, E> = 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)
}

0 comments on commit b698560

Please sign in to comment.