Skip to content

Commit

Permalink
followedBy using T instead of DataResult<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro-Bachiega committed Jun 22, 2024
1 parent ef33e65 commit a4c9682
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
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

0 comments on commit a4c9682

Please sign in to comment.