-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
582 additions
and
67 deletions.
There are no files selected for viewing
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
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
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
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 @@ | ||
/build |
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,6 @@ | ||
# lce-coroutines | ||
Coroutines/Flow support for LCE. | ||
|
||
If coming over from RxJava, here is the typical mapping | ||
|
||
- `switchMapContent` -> `flatMapLatestContent` |
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,25 @@ | ||
apply plugin: 'kotlin' | ||
apply plugin: 'org.jetbrains.dokka' | ||
|
||
apply from: rootProject.file('.buildscript/configure-signing.gradle') | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
} | ||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
|
||
dependencies { | ||
api(libraries.coroutines) | ||
api(project(":lce")) | ||
|
||
testImplementation(libraries.truth) | ||
testImplementation(libraries.junit) | ||
} |
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,3 @@ | ||
POM_ARTIFACT_ID=lce-coroutines | ||
POM_NAME=LCE type Flow Support | ||
POM_PACKAGING=jar |
145 changes: 145 additions & 0 deletions
145
lce-coroutines/src/main/java/com/laimiux/lce/flow/ContentEvents.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,145 @@ | ||
@file:OptIn(ExperimentalCoroutinesApi::class) | ||
|
||
package com.laimiux.lce.flow | ||
|
||
import com.laimiux.lce.CT | ||
import com.laimiux.lce.LC | ||
import com.laimiux.lce.LCE | ||
import com.laimiux.lce.UC | ||
import com.laimiux.lce.UCE | ||
import com.laimiux.lce.UCT | ||
import com.laimiux.lce.fold | ||
import com.laimiux.lce.foldTypes | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flatMapMerge | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
@JvmName("onlyContentEventsUCE") | ||
fun <C : Any, E> Flow<UCE<C, E>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onError = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsUCT") | ||
fun <C : Any> Flow<UCT<C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onError = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsCT") | ||
fun <C : Any> Flow<CT<C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onError = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsLC") | ||
fun <L, C : Any> Flow<LC<L, C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsUC") | ||
fun <C : Any> Flow<UC<C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentLCE") | ||
inline fun <L, C, E, NewC> Flow<LCE<L, C, E>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<LCE<L, NewC, E>> | ||
): Flow<LCE<L, NewC, E>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentUCE") | ||
inline fun <C, E, NewC> Flow<UCE<C, E>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<UCE<NewC, E>> | ||
): Flow<UCE<NewC, E>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentUCT") | ||
inline fun <C, NewC> Flow<UCT<C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<UCT<NewC>> | ||
): Flow<UCT<NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentCT") | ||
inline fun <C, NewC> Flow<CT<C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<CT<NewC>> | ||
): Flow<CT<NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentLC") | ||
inline fun <L, C, NewC> Flow<LC<L, C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<LC<L, NewC>> | ||
): Flow<LC<L, NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentUC") | ||
inline fun <C, NewC> Flow<UC<C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<UC<NewC>> | ||
): Flow<UC<NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) } | ||
) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
lce-coroutines/src/main/java/com/laimiux/lce/flow/Convert.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,26 @@ | ||
@file:OptIn(ExperimentalCoroutinesApi::class) | ||
|
||
package com.laimiux.lce.flow | ||
|
||
import com.laimiux.lce.CT | ||
import com.laimiux.lce.UCT | ||
import com.laimiux.lce.asCT | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
/** | ||
* Converts [UCT] type to [CT]. Essentially it ignores loading events. | ||
*/ | ||
fun <C> Flow<UCT<C>>.toCT(): Flow<CT<C>> { | ||
return flatMapLatest { | ||
val ct = it.asCT() | ||
if (ct != null) { | ||
flowOf(ct) | ||
} else { | ||
emptyFlow() | ||
} | ||
} | ||
} |
Oops, something went wrong.