Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

followedBy using T instead of DataResult<T> #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ open class ResponseLiveData<T> : LiveData<DataResult<T>> {
@NonNull
@Experimental
fun <R> followedBy(
@NonNull source: (DataResult<T>) -> ResponseLiveData<R>,
@NonNull source: (T) -> ResponseLiveData<R>,
@NonNull condition: (T) -> Boolean,
@NonNull successOnConditionError: Boolean
): ResponseLiveData<Pair<T, R?>> = withDelegate {
Expand All @@ -229,7 +229,7 @@ open class ResponseLiveData<T> : LiveData<DataResult<T>> {
@NonNull
@Experimental
fun <R> followedBy(
@NonNull source: (DataResult<T>) -> ResponseLiveData<R>,
@NonNull source: (T) -> ResponseLiveData<R>,
@NonNull condition: (T) -> Boolean
): ResponseLiveData<Pair<T, R>> =
followedBy(source, condition, false).map { it.first to it.second!! }
Expand All @@ -244,7 +244,7 @@ open class ResponseLiveData<T> : LiveData<DataResult<T>> {
@NonNull
@Experimental
fun <R> followedBy(
@NonNull source: (DataResult<T>) -> ResponseLiveData<R>
@NonNull source: (T) -> ResponseLiveData<R>
): ResponseLiveData<Pair<T, R>> = followedBy(source) { true }
//endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal interface ResponseLiveDataMergeDelegate {

fun <T, R> followedBy(
source: ResponseLiveData<T>,
next: (DataResult<T>) -> ResponseLiveData<R>,
next: (T) -> ResponseLiveData<R>,
scope: CoroutineScope,
transformDispatcher: CoroutineDispatcher,
condition: (T) -> Boolean,
Expand Down Expand Up @@ -97,7 +97,7 @@ internal class DefaultResponseLiveDataMergeDelegate : ResponseLiveDataMergeDeleg
scope: CoroutineScope,
transformDispatcher: CoroutineDispatcher,
source: ResponseLiveData<T>,
next: (DataResult<T>) -> ResponseLiveData<R>,
next: (T) -> ResponseLiveData<R>,
condition: (T) -> Boolean,
successOnConditionError: Boolean
): ResponseLiveData<Pair<T, R?>> = addSources(
Expand All @@ -107,18 +107,19 @@ internal class DefaultResponseLiveDataMergeDelegate : ResponseLiveDataMergeDeleg
onMerge = { source.value.mergeNotNull(DataResult(null, null, DataResultStatus.LOADING)) }
) {
val sourceValue = source.value ?: return@addSources
val sourceData = sourceValue.data

val conditionMet = sourceValue.data?.let(condition) == true
val conditionMet = sourceData?.let(condition) == true
val nextSource: ResponseLiveData<Pair<T, R?>> = when {
!conditionMet && !successOnConditionError -> {
responseLiveDataOf(IllegalStateException("Pre-condition not met for merge"))
}

!conditionMet && successOnConditionError -> {
responseLiveDataOf(sourceValue.data!! to null)
responseLiveDataOf(sourceData!! to null)
}

else -> source.mergeWith(next.invoke(sourceValue)).map { it }
else -> source.mergeWith(next.invoke(sourceData!!)).map { it }
}

swapSource(nextSource)
Expand Down Expand Up @@ -162,7 +163,7 @@ internal class DefaultResponseLiveDataMergeDelegate : ResponseLiveDataMergeDeleg

override fun <T, R> followedBy(
source: ResponseLiveData<T>,
next: (DataResult<T>) -> ResponseLiveData<R>,
next: (T) -> ResponseLiveData<R>,
scope: CoroutineScope,
transformDispatcher: CoroutineDispatcher,
condition: (T) -> Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ResponseLiveDataTest {
}

@Test
fun `00 - init with param, should init with param value`() = runTest {
fun `01 - init with param, should init with param value`() = runTest {
val value = DataResult("String", null, SUCCESS)
val liveData = ResponseLiveData(value)

Expand All @@ -63,7 +63,7 @@ class ResponseLiveDataTest {
}

@Test
fun `01 - Should keep observer until all events has been handled`() = runTest {
fun `02 - Should keep observer until all events has been handled`() = runTest {
fun singleTrue() {
val liveData = ResponseLiveData<Any>()
liveData.transformDispatcher(Dispatchers.Main.immediate)
Expand Down Expand Up @@ -94,7 +94,7 @@ class ResponseLiveDataTest {
}

@Test
fun `02 - transform`() = runTest {
fun `03 - transform`() = runTest {
val mockTransform: (DataResult<Int>) -> DataResult<String> = mock()
whenever(mockTransform.invoke(any())) doReturn dataResultSuccess("String")

Expand All @@ -120,7 +120,7 @@ class ResponseLiveDataTest {
}

@Test
fun `03 - onError`() = runTest {
fun `04 - onError`() = runTest {
val error = IllegalStateException("error")
val mockOnError: (Throwable) -> Unit = mock()
val liveData = ResponseLiveData<Int>()
Expand Down Expand Up @@ -152,7 +152,7 @@ class ResponseLiveDataTest {
}

@Test
fun `04 - onErrorReturn`() = runTest {
fun `05 - onErrorReturn`() = runTest {
val error = IllegalStateException("error")
val mockOnErrorReturn: (Throwable) -> Int = mock()
whenever(mockOnErrorReturn.invoke(error)) doReturn 123
Expand Down Expand Up @@ -185,7 +185,7 @@ class ResponseLiveDataTest {
}

@Test
fun `05 - mapError`() = runTest {
fun `06 - mapError`() = runTest {
val error = IllegalStateException("error")
val error2 = IllegalStateException("error2")
val mockMapError: (Throwable) -> Throwable = mock()
Expand Down Expand Up @@ -219,7 +219,7 @@ class ResponseLiveDataTest {
}

@Test
fun `06 - map`() = runTest {
fun `07 - map`() = runTest {
val mockMap: (Int) -> String = mock()
whenever(mockMap.invoke(123)) doReturn "String"
val liveData = ResponseLiveData<Int>()
Expand Down Expand Up @@ -251,7 +251,7 @@ class ResponseLiveDataTest {
}

@Test
fun `07 - onNext`() = runTest {
fun `08 - onNext`() = runTest {
val mockOnNext: (Int) -> Unit = mock()
val liveData = ResponseLiveData<Int>()
liveData.transformDispatcher(Dispatchers.Main.immediate)
Expand Down Expand Up @@ -282,7 +282,7 @@ class ResponseLiveDataTest {
}

@Test
fun `08 - mergeWith - plus`() = runTest {
fun `09 - mergeWith - plus`() = runTest {
val liveDataA = ResponseLiveData(dataResultSuccess(123))
val liveDataB = ResponseLiveData(dataResultSuccess("String"))
val liveDataMerge = liveDataA + liveDataB
Expand All @@ -293,7 +293,7 @@ class ResponseLiveDataTest {
}

@Test
fun `09 - mergeWith with tag`() = runTest {
fun `10 - mergeWith with tag`() = runTest {
val liveDataA = ResponseLiveData(dataResultSuccess(123))
val liveDataB = ResponseLiveData(dataResultSuccess("String"))
val liveDataMerge = liveDataA.mergeWith("tagA", "tagB" to liveDataB)
Expand All @@ -312,7 +312,7 @@ class ResponseLiveDataTest {
}

@Test
fun `10 - followedBy - both success`() = runTest {
fun `11 - followedBy - both success`() = runTest {
val liveDataA = ResponseLiveData(dataResultSuccess(123))
liveDataA.transformDispatcher(Dispatchers.Main.immediate)

Expand All @@ -330,7 +330,7 @@ class ResponseLiveDataTest {
}

@Test
fun `11 - followedBy - one success, other loading`() = runTest {
fun `12 - followedBy - one success, other loading`() = runTest {
val liveDataA = ResponseLiveData(dataResultSuccess(123))
liveDataA.transformDispatcher(Dispatchers.Main.immediate)

Expand Down
Loading