From 4cc88b145449715fc51550ccaa9e6172faeec753 Mon Sep 17 00:00:00 2001 From: Cyrus Bakhtiari-Haftlang Date: Fri, 8 May 2020 10:04:59 +0200 Subject: [PATCH] Added support for combineLatest(5) Added emptyLiveData --- .../src/main/java/se/ingenuity/lives/Lives.kt | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lives/src/main/java/se/ingenuity/lives/Lives.kt b/lives/src/main/java/se/ingenuity/lives/Lives.kt index 036cf65..08da05a 100644 --- a/lives/src/main/java/se/ingenuity/lives/Lives.kt +++ b/lives/src/main/java/se/ingenuity/lives/Lives.kt @@ -49,6 +49,25 @@ object Lives { ) } + fun combineLatest( + first: LiveData, + second: LiveData, + third: LiveData, + fourth: LiveData, + fifth: LiveData, + combineFunction: (T1, T2, T3, T4, T5) -> R + ): LiveData { + @Suppress("UNCHECKED_CAST") + return combineLatest( + Array5Func(combineFunction), + first as LiveData, + second as LiveData, + third as LiveData, + fourth as LiveData, + fifth as LiveData + ) + } + private fun combineLatest( combiner: (Array) -> R, vararg sources: LiveData @@ -168,4 +187,19 @@ object Lives { return f(a[0] as T1, a[1] as T2, a[2] as T3, a[3] as T4) } } -} \ No newline at end of file + + private class Array5Func( + private val f: (T1, T2, T3, T4, T5) -> R + ) : (Array<*>) -> R { + @Suppress("UNCHECKED_CAST") + override fun invoke(a: Array<*>): R { + require(a.size == 5) { "Array of size 5 expected but got " + a.size } + return f(a[0] as T1, a[1] as T2, a[2] as T3, a[3] as T4, a[4] as T5) + } + } +} + +internal object EmptyLiveData : LiveData() + +@Suppress("UNCHECKED_CAST") +fun emptyLiveData(): LiveData = EmptyLiveData as LiveData \ No newline at end of file