Add back in Transformations extensions that use arch core Function
To ensure backwards compatibility, we still need to have
a version of the Transformations extension functions that
allow the use of arch core's Functions, although this is
an outdated practice and developers should be using
kotlin function.
RelNote: "`Transformations` is now in Kotlin. Although
backwards compatiblity exists, developers should switch
from using arch core's `Function` for `map` and `switchMap`
and should instead utilize kotlin native functions."
Bug: 262883415
Test: all tests pass
Change-Id: I8e14f0c95d1b509620dfe6b94318e314f84d9f4b
diff --git a/lifecycle/lifecycle-livedata/api/current.ignore b/lifecycle/lifecycle-livedata/api/current.ignore
index 1fa5a57..f3f2baa 100644
--- a/lifecycle/lifecycle-livedata/api/current.ignore
+++ b/lifecycle/lifecycle-livedata/api/current.ignore
@@ -1,9 +1,7 @@
// Baseline format: 1.0
ChangedType: androidx.lifecycle.Transformations#distinctUntilChanged(androidx.lifecycle.LiveData<X>):
Method androidx.lifecycle.Transformations.distinctUntilChanged has changed return type from androidx.lifecycle.LiveData<X!> to androidx.lifecycle.LiveData<X>
-
-
-RemovedMethod: androidx.lifecycle.Transformations#map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y>):
- Removed method androidx.lifecycle.Transformations.map(androidx.lifecycle.LiveData<X>,androidx.arch.core.util.Function<X,Y>)
-RemovedMethod: androidx.lifecycle.Transformations#switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>>):
- Removed method androidx.lifecycle.Transformations.switchMap(androidx.lifecycle.LiveData<X>,androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>>)
+ChangedType: androidx.lifecycle.Transformations#map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y>):
+ Method androidx.lifecycle.Transformations.map has changed return type from androidx.lifecycle.LiveData<Y!> to androidx.lifecycle.LiveData<Y>
+ChangedType: androidx.lifecycle.Transformations#switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>>):
+ Method androidx.lifecycle.Transformations.switchMap has changed return type from androidx.lifecycle.LiveData<Y!> to androidx.lifecycle.LiveData<Y>
diff --git a/lifecycle/lifecycle-livedata/api/current.txt b/lifecycle/lifecycle-livedata/api/current.txt
index 5f6dcb4..9b1bf6c 100644
--- a/lifecycle/lifecycle-livedata/api/current.txt
+++ b/lifecycle/lifecycle-livedata/api/current.txt
@@ -11,7 +11,9 @@
public final class Transformations {
method @CheckResult @MainThread public static <X> androidx.lifecycle.LiveData<X> distinctUntilChanged(androidx.lifecycle.LiveData<X>);
method @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> map(androidx.lifecycle.LiveData<X>, kotlin.jvm.functions.Function1<X,Y> transform);
+ method @Deprecated @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y> mapFunction);
method @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> switchMap(androidx.lifecycle.LiveData<X>, kotlin.jvm.functions.Function1<X,androidx.lifecycle.LiveData<Y>> transform);
+ method @Deprecated @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>> switchMapFunction);
}
}
diff --git a/lifecycle/lifecycle-livedata/api/public_plus_experimental_current.txt b/lifecycle/lifecycle-livedata/api/public_plus_experimental_current.txt
index 5f6dcb4..9b1bf6c 100644
--- a/lifecycle/lifecycle-livedata/api/public_plus_experimental_current.txt
+++ b/lifecycle/lifecycle-livedata/api/public_plus_experimental_current.txt
@@ -11,7 +11,9 @@
public final class Transformations {
method @CheckResult @MainThread public static <X> androidx.lifecycle.LiveData<X> distinctUntilChanged(androidx.lifecycle.LiveData<X>);
method @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> map(androidx.lifecycle.LiveData<X>, kotlin.jvm.functions.Function1<X,Y> transform);
+ method @Deprecated @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y> mapFunction);
method @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> switchMap(androidx.lifecycle.LiveData<X>, kotlin.jvm.functions.Function1<X,androidx.lifecycle.LiveData<Y>> transform);
+ method @Deprecated @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>> switchMapFunction);
}
}
diff --git a/lifecycle/lifecycle-livedata/api/restricted_current.ignore b/lifecycle/lifecycle-livedata/api/restricted_current.ignore
index d819143..43f65eb 100644
--- a/lifecycle/lifecycle-livedata/api/restricted_current.ignore
+++ b/lifecycle/lifecycle-livedata/api/restricted_current.ignore
@@ -3,9 +3,7 @@
Method androidx.lifecycle.ComputableLiveData.getLiveData has changed return type from androidx.lifecycle.LiveData<T!> to androidx.lifecycle.LiveData<T>
ChangedType: androidx.lifecycle.Transformations#distinctUntilChanged(androidx.lifecycle.LiveData<X>):
Method androidx.lifecycle.Transformations.distinctUntilChanged has changed return type from androidx.lifecycle.LiveData<X!> to androidx.lifecycle.LiveData<X>
-
-
-RemovedMethod: androidx.lifecycle.Transformations#map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y>):
- Removed method androidx.lifecycle.Transformations.map(androidx.lifecycle.LiveData<X>,androidx.arch.core.util.Function<X,Y>)
-RemovedMethod: androidx.lifecycle.Transformations#switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>>):
- Removed method androidx.lifecycle.Transformations.switchMap(androidx.lifecycle.LiveData<X>,androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>>)
+ChangedType: androidx.lifecycle.Transformations#map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y>):
+ Method androidx.lifecycle.Transformations.map has changed return type from androidx.lifecycle.LiveData<Y!> to androidx.lifecycle.LiveData<Y>
+ChangedType: androidx.lifecycle.Transformations#switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>>):
+ Method androidx.lifecycle.Transformations.switchMap has changed return type from androidx.lifecycle.LiveData<Y!> to androidx.lifecycle.LiveData<Y>
diff --git a/lifecycle/lifecycle-livedata/api/restricted_current.txt b/lifecycle/lifecycle-livedata/api/restricted_current.txt
index a0b6878..bb61b39 100644
--- a/lifecycle/lifecycle-livedata/api/restricted_current.txt
+++ b/lifecycle/lifecycle-livedata/api/restricted_current.txt
@@ -20,7 +20,9 @@
public final class Transformations {
method @CheckResult @MainThread public static <X> androidx.lifecycle.LiveData<X> distinctUntilChanged(androidx.lifecycle.LiveData<X>);
method @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> map(androidx.lifecycle.LiveData<X>, kotlin.jvm.functions.Function1<X,Y> transform);
+ method @Deprecated @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> map(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,Y> mapFunction);
method @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> switchMap(androidx.lifecycle.LiveData<X>, kotlin.jvm.functions.Function1<X,androidx.lifecycle.LiveData<Y>> transform);
+ method @Deprecated @CheckResult @MainThread public static <X, Y> androidx.lifecycle.LiveData<Y> switchMap(androidx.lifecycle.LiveData<X>, androidx.arch.core.util.Function<X,androidx.lifecycle.LiveData<Y>> switchMapFunction);
}
}
diff --git a/lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.kt b/lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.kt
index b2c60cc..7c8af62 100644
--- a/lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.kt
+++ b/lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.kt
@@ -19,6 +19,7 @@
import androidx.annotation.CheckResult
import androidx.annotation.MainThread
+import androidx.arch.core.util.Function
/**
* Returns a [LiveData] mapped from `this` LiveData by applying [transform] to each value set on
@@ -52,6 +53,19 @@
return result
}
+@Deprecated(
+ "Use kotlin functions, instead of outdated arch core Functions",
+ level = DeprecationLevel.HIDDEN
+)
+@JvmName("map")
+@MainThread
+@CheckResult
+fun <X, Y> LiveData<X>.map(mapFunction: Function<X, Y>): LiveData<Y> {
+ val result = MediatorLiveData<Y>()
+ result.addSource(this) { x -> result.value = mapFunction.apply(x) }
+ return result
+}
+
/**
* Returns a [LiveData] mapped from the input `this` `LiveData` by applying
* [transform] to each value set on `this`.
@@ -123,6 +137,35 @@
return result
}
+@Deprecated(
+ "Use kotlin functions, instead of outdated arch core Functions",
+ level = DeprecationLevel.HIDDEN
+)
+@JvmName("switchMap")
+@MainThread
+@CheckResult
+fun <X, Y> LiveData<X>.switchMap(switchMapFunction: Function<X, LiveData<Y>>): LiveData<Y> {
+ val result = MediatorLiveData<Y>()
+ result.addSource(this, object : Observer<X> {
+ var liveData: LiveData<Y>? = null
+
+ override fun onChanged(x: X) {
+ val newLiveData = switchMapFunction.apply(x)
+ if (liveData === newLiveData) {
+ return
+ }
+ if (liveData != null) {
+ result.removeSource(liveData!!)
+ }
+ liveData = newLiveData
+ if (liveData != null) {
+ result.addSource(liveData!!) { y -> result.setValue(y) }
+ }
+ }
+ })
+ return result
+}
+
/**
* Creates a new [LiveData] object does not emit a value until the source `this` LiveData value
* has been changed. The value is considered changed if `equals()` yields `false`.