forked from icerockdev/moko-mvvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
icerockdev#183 removing duplicate code
- Loading branch information
mdubkov
committed
Sep 7, 2022
1 parent
9d9de65
commit b698560
Showing
3 changed files
with
28 additions
and
32 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
mvvm-state-core/src/commonMain/kotlin/dev/icerock/moko/mvvm/state/Merges.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters