Skip to content

Commit

Permalink
Merge pull request #4 from thefuntasty/feature/livedata-utils-improve
Browse files Browse the repository at this point in the history
Set one name for livedata combine functions
  • Loading branch information
matejcikm authored May 14, 2018
2 parents 065e9cd + f9c8af7 commit 4d5333c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.thefuntasty.mvvmsample.ui.form

import com.thefuntasty.mvvm.ViewState
import com.thefuntasty.mvvm.livedata.DefaultValueLiveData
import com.thefuntasty.mvvm.livedata.liveDataCombineTwo
import com.thefuntasty.mvvm.livedata.combineLiveData

class FormViewState(login: String, password: String) : ViewState {

val login = DefaultValueLiveData(login)
val password = DefaultValueLiveData(password)
val submitEnabled = liveDataCombineTwo(this.login, this.password) { login, password ->
val submitEnabled = combineLiveData(this.login, this.password) { login, password ->
login.isNotEmpty() && password.isNotEmpty()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MediatorLiveData
import android.arch.lifecycle.Transformations

fun <T1, T2, RESULT> liveDataCombineTwo(t1: LiveData<T1>,
fun <T1, T2, RESULT> combineLiveData(t1: LiveData<T1>,
t2: LiveData<T2>,
callback: (T1, T2) -> RESULT): LiveData<RESULT> {
val mediatorLiveData = MediatorLiveData<Pair<T1, T2>>().apply {
Expand All @@ -24,7 +24,7 @@ fun <T1, T2, RESULT> liveDataCombineTwo(t1: LiveData<T1>,
return Transformations.map(mediatorLiveData) { callback(it.first, it.second) }
}

fun <T1, T2, T3, RESULT> liveDataCombineThree(t1: LiveData<T1>,
fun <T1, T2, T3, RESULT> combineLiveData(t1: LiveData<T1>,
t2: LiveData<T2>,
t3: LiveData<T3>,
callback: (T1, T2, T3) -> RESULT): LiveData<RESULT> {
Expand All @@ -49,7 +49,7 @@ fun <T1, T2, T3, RESULT> liveDataCombineThree(t1: LiveData<T1>,
return Transformations.map(mediatorLiveData) { callback(it.first, it.second, it.third) }
}

fun <T1, T2, T3, T4, RESULT> liveDataCombineFour(t1: LiveData<T1>,
fun <T1, T2, T3, T4, RESULT> combineLiveData(t1: LiveData<T1>,
t2: LiveData<T2>,
t3: LiveData<T3>,
t4: LiveData<T4>,
Expand Down Expand Up @@ -80,7 +80,7 @@ fun <T1, T2, T3, T4, RESULT> liveDataCombineFour(t1: LiveData<T1>,
return Transformations.map(mediatorLiveData) { callback(it.first, it.second, it.third, it.fourth) }
}

fun <T1, T2, T3, T4, T5, RESULT> liveDataCombineFive(t1: LiveData<T1>,
fun <T1, T2, T3, T4, T5, RESULT> combineLiveData(t1: LiveData<T1>,
t2: LiveData<T2>,
t3: LiveData<T3>,
t4: LiveData<T4>,
Expand Down Expand Up @@ -115,7 +115,7 @@ fun <T1, T2, T3, T4, T5, RESULT> liveDataCombineFive(t1: LiveData<T1>,
return Transformations.map(mediatorLiveData) { callback(it.first, it.second, it.third, it.fourth, it.fifth) }
}

fun <T1, T2, T3, T4, T5, T6, RESULT> liveDataCombineSix(t1: LiveData<T1>,
fun <T1, T2, T3, T4, T5, T6, RESULT> combineLiveData(t1: LiveData<T1>,
t2: LiveData<T2>,
t3: LiveData<T3>,
t4: LiveData<T4>,
Expand Down Expand Up @@ -155,7 +155,7 @@ fun <T1, T2, T3, T4, T5, T6, RESULT> liveDataCombineSix(t1: LiveData<T1>,
return Transformations.map(mediatorLiveData) { callback(it.first, it.second, it.third, it.fourth, it.fifth, it.sixth) }
}

fun <T1, T2, T3, T4, T5, T6, T7, RESULT> liveDataCombineSeven(t1: LiveData<T1>,
fun <T1, T2, T3, T4, T5, T6, T7, RESULT> combineLiveData(t1: LiveData<T1>,
t2: LiveData<T2>,
t3: LiveData<T3>,
t4: LiveData<T4>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fun <T1, T2, RESULT> combineLiveData(
t1: LiveData<T1>,
t2: LiveData<T2>,
callback: (T1, T2) -> RESULT
): DefaultValueMediatorLiveData<RESULT> = liveDataCombineTwo(t1, t2, callback)
): DefaultValueMediatorLiveData<RESULT> = combineLiveData(t1, t2, callback)
.nonNull(defaultValue)

fun <T1, T2, T3, RESULT> combineLiveData(
Expand All @@ -16,7 +16,7 @@ fun <T1, T2, T3, RESULT> combineLiveData(
t2: LiveData<T2>,
t3: LiveData<T3>,
callback: (T1, T2, T3) -> RESULT
): DefaultValueMediatorLiveData<RESULT> = liveDataCombineThree(t1, t2, t3, callback)
): DefaultValueMediatorLiveData<RESULT> = combineLiveData(t1, t2, t3, callback)
.nonNull(defaultValue)

fun <T1, T2, T3, T4, RESULT> combineLiveData(
Expand All @@ -26,7 +26,7 @@ fun <T1, T2, T3, T4, RESULT> combineLiveData(
t3: LiveData<T3>,
t4: LiveData<T4>,
callback: (T1, T2, T3, T4) -> RESULT
): DefaultValueMediatorLiveData<RESULT> = liveDataCombineFour(t1, t2, t3, t4, callback)
): DefaultValueMediatorLiveData<RESULT> = combineLiveData(t1, t2, t3, t4, callback)
.nonNull(defaultValue)

fun <T1, T2, T3, T4, T5, RESULT> combineLiveData(
Expand All @@ -37,7 +37,7 @@ fun <T1, T2, T3, T4, T5, RESULT> combineLiveData(
t4: LiveData<T4>,
t5: LiveData<T5>,
callback: (T1, T2, T3, T4, T5) -> RESULT
): DefaultValueMediatorLiveData<RESULT> = liveDataCombineFive(t1, t2, t3, t4, t5, callback)
): DefaultValueMediatorLiveData<RESULT> = combineLiveData(t1, t2, t3, t4, t5, callback)
.nonNull(defaultValue)

fun <T1, T2, T3, T4, T5, T6, RESULT> combineLiveData(
Expand All @@ -49,7 +49,7 @@ fun <T1, T2, T3, T4, T5, T6, RESULT> combineLiveData(
t5: LiveData<T5>,
t6: LiveData<T6>,
callback: (T1, T2, T3, T4, T5, T6) -> RESULT
): DefaultValueMediatorLiveData<RESULT> = liveDataCombineSix(t1, t2, t3, t4, t5, t6, callback)
): DefaultValueMediatorLiveData<RESULT> = combineLiveData(t1, t2, t3, t4, t5, t6, callback)
.nonNull(defaultValue)

fun <T1, T2, T3, T4, T5, T6, T7, RESULT> combineLiveData(
Expand All @@ -62,5 +62,5 @@ fun <T1, T2, T3, T4, T5, T6, T7, RESULT> combineLiveData(
t6: LiveData<T6>,
t7: LiveData<T7>,
callback: (T1, T2, T3, T4, T5, T6, T7) -> RESULT
): DefaultValueMediatorLiveData<RESULT> = liveDataCombineSeven(t1, t2, t3, t4, t5, t6, t7, callback)
): DefaultValueMediatorLiveData<RESULT> = combineLiveData(t1, t2, t3, t4, t5, t6, t7, callback)
.nonNull(defaultValue)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LiveDataUtilsTest {
fun testLiveDataCombineTwoNotEmpty() {
val liveData1 = DefaultValueLiveData("")
val liveData2 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineTwo(liveData1, liveData2) { first, second ->
val resultLiveData = combineLiveData(liveData1, liveData2) { first, second ->
first.isNotEmpty() && second.isNotEmpty()
}
resultLiveData.observeForever(observer)
Expand All @@ -34,7 +34,7 @@ class LiveDataUtilsTest {
fun testLiveDataCombineTwoEmpty() {
val liveData1 = DefaultValueLiveData("")
val liveData2 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineTwo(liveData1, liveData2) { first, second ->
val resultLiveData = combineLiveData(liveData1, liveData2) { first, second ->
first.isNotEmpty() && second.isNotEmpty()
}
resultLiveData.observeForever(observer)
Expand All @@ -49,7 +49,7 @@ class LiveDataUtilsTest {
val liveData1 = DefaultValueLiveData("")
val liveData2 = DefaultValueLiveData("")
val liveData3 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineThree(liveData1, liveData2, liveData3) { first, second, third ->
val resultLiveData = combineLiveData(liveData1, liveData2, liveData3) { first, second, third ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty()
}
resultLiveData.observeForever(observer)
Expand All @@ -66,7 +66,7 @@ class LiveDataUtilsTest {
val liveData1 = DefaultValueLiveData("")
val liveData2 = DefaultValueLiveData("")
val liveData3 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineThree(liveData1, liveData2, liveData3) { first, second, third ->
val resultLiveData = combineLiveData(liveData1, liveData2, liveData3) { first, second, third ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty()
}
resultLiveData.observeForever(observer)
Expand All @@ -82,7 +82,7 @@ class LiveDataUtilsTest {
val liveData2 = DefaultValueLiveData("")
val liveData3 = DefaultValueLiveData("")
val liveData4 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineFour(liveData1, liveData2, liveData3, liveData4) { first, second, third, fourth ->
val resultLiveData = combineLiveData(liveData1, liveData2, liveData3, liveData4) { first, second, third, fourth ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty()
}
resultLiveData.observeForever(observer)
Expand All @@ -101,7 +101,7 @@ class LiveDataUtilsTest {
val liveData2 = DefaultValueLiveData("")
val liveData3 = DefaultValueLiveData("")
val liveData4 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineFour(liveData1, liveData2, liveData3, liveData4) { first, second, third, fourth ->
val resultLiveData = combineLiveData(liveData1, liveData2, liveData3, liveData4) { first, second, third, fourth ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty()
}
resultLiveData.observeForever(observer)
Expand All @@ -119,7 +119,7 @@ class LiveDataUtilsTest {
val liveData3 = DefaultValueLiveData("")
val liveData4 = DefaultValueLiveData("")
val liveData5 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineFive(
val resultLiveData = combineLiveData(
liveData1, liveData2, liveData3, liveData4, liveData5) { first, second, third, fourth, fifth ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty() && fifth.isNotEmpty()
}
Expand All @@ -141,7 +141,7 @@ class LiveDataUtilsTest {
val liveData3 = DefaultValueLiveData("")
val liveData4 = DefaultValueLiveData("")
val liveData5 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineFive(
val resultLiveData = combineLiveData(
liveData1, liveData2, liveData3, liveData4, liveData5) { first, second, third, fourth, fifth ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty() && fifth.isNotEmpty()
}
Expand All @@ -162,7 +162,7 @@ class LiveDataUtilsTest {
val liveData4 = DefaultValueLiveData("")
val liveData5 = DefaultValueLiveData("")
val liveData6 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineSix(
val resultLiveData = combineLiveData(
liveData1, liveData2, liveData3, liveData4, liveData5, liveData6) { first, second, third, fourth, fifth, sixth ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty() && fifth.isNotEmpty() && sixth.isNotEmpty()
}
Expand All @@ -186,7 +186,7 @@ class LiveDataUtilsTest {
val liveData4 = DefaultValueLiveData("")
val liveData5 = DefaultValueLiveData("")
val liveData6 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineSix(
val resultLiveData = combineLiveData(
liveData1, liveData2, liveData3, liveData4, liveData5, liveData6) { first, second, third, fourth, fifth, sixth ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty() && fifth.isNotEmpty() && sixth.isNotEmpty()
}
Expand All @@ -209,7 +209,7 @@ class LiveDataUtilsTest {
val liveData5 = DefaultValueLiveData("")
val liveData6 = DefaultValueLiveData("")
val liveData7 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineSeven(
val resultLiveData = combineLiveData(
liveData1, liveData2, liveData3, liveData4, liveData5, liveData6, liveData7) { first, second, third, fourth, fifth, sixth, seventh ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty() && fifth.isNotEmpty() && sixth.isNotEmpty() && seventh.isNotEmpty()
}
Expand All @@ -235,7 +235,7 @@ class LiveDataUtilsTest {
val liveData5 = DefaultValueLiveData("")
val liveData6 = DefaultValueLiveData("")
val liveData7 = DefaultValueLiveData("")
val resultLiveData = liveDataCombineSeven(
val resultLiveData = combineLiveData(
liveData1, liveData2, liveData3, liveData4, liveData5, liveData6, liveData7) { first, second, third, fourth, fifth, sixth, seventh ->
first.isNotEmpty() && second.isNotEmpty() && third.isNotEmpty() && fourth.isNotEmpty() && fifth.isNotEmpty() && sixth.isNotEmpty() && seventh.isNotEmpty()
}
Expand Down

0 comments on commit 4d5333c

Please sign in to comment.