diff --git a/app/build.gradle b/app/build.gradle index 378ef3d3b4..58c7184461 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -526,7 +526,7 @@ configurations.configureEach { } else if (details.requested.group == "androidx.lifecycle" && details.requested.name != "lifecycle-extensions") { //print("Pinning " + details.requested.group + ":" + details.requested.name + "\n") - details.useVersion "2.8.1" + details.useVersion "2.7.0" } else if (details.requested.group == "org.apache.poi") { //print("Pinning " + details.requested.group + ":" + details.requested.name + "\n") details.useVersion "3.17" @@ -562,7 +562,7 @@ dependencies { def lbm_version = "1.1.0" def swiperefresh_version = "1.2.0-alpha01" def documentfile_version = "1.1.0-alpha01" - def lifecycle_version = "2.8.1" + def lifecycle_version = "2.7.0" // 2.8.1 def lifecycle_extensions_version = "2.2.0" def room_version = "2.4.3" // 2.5.2/2.6.1/2.7.0-alpha04 def sqlite_version = "2.4.0" // 2.5.0-alpha04 diff --git a/app/src/main/java/androidx/lifecycle/FlowLiveData.kt b/app/src/main/java/androidx/lifecycle/FlowLiveData.kt index 51e541090c..72d3ef74ee 100644 --- a/app/src/main/java/androidx/lifecycle/FlowLiveData.kt +++ b/app/src/main/java/androidx/lifecycle/FlowLiveData.kt @@ -18,19 +18,22 @@ package androidx.lifecycle +import android.annotation.SuppressLint import android.os.Build import androidx.annotation.RequiresApi import androidx.arch.core.executor.ArchTaskExecutor import java.time.Duration import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext +import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.NonCancellable -import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.conflate +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext /** @@ -81,6 +84,7 @@ public fun Flow.asLiveData( }.also { liveData -> val flow = this if (flow is StateFlow) { + @SuppressLint("RestrictedApi") if (ArchTaskExecutor.getInstance().isMainThread) { liveData.value = flow.value } else { @@ -100,6 +104,7 @@ public fun Flow.asLiveData( * BackPressure: the returned flow is conflated. There is no mechanism to suspend an emission by * LiveData due to a slow collector, so collector always gets the most recent value emitted. */ +@OptIn(DelicateCoroutinesApi::class) public fun LiveData.asFlow(): Flow = callbackFlow { val observer = Observer { trySend(it) @@ -108,10 +113,8 @@ public fun LiveData.asFlow(): Flow = callbackFlow { observeForever(observer) } - try { - awaitCancellation() - } finally { - withContext(Dispatchers.Main.immediate + NonCancellable) { + awaitClose { + GlobalScope.launch(Dispatchers.Main.immediate) { removeObserver(observer) } } diff --git a/app/src/main/java/androidx/lifecycle/LiveData.kt b/app/src/main/java/androidx/lifecycle/LiveData.kt deleted file mode 100644 index 951350ba62..0000000000 --- a/app/src/main/java/androidx/lifecycle/LiveData.kt +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package androidx.lifecycle - -import androidx.annotation.MainThread - -/** - * Adds the given [onChanged] lambda as an observer within the lifespan of the given - * [owner] and returns a reference to observer. - * The events are dispatched on the main thread. If LiveData already has data - * set, it will be delivered to the onChanged. - * - * The observer will only receive events if the owner is in [Lifecycle.State.STARTED] - * or [Lifecycle.State.RESUMED] state (active). - * - * If the owner moves to the [Lifecycle.State.DESTROYED] state, the observer will - * automatically be removed. - * - * When data changes while the [owner] is not active, it will not receive any updates. - * If it becomes active again, it will receive the last available data automatically. - * - * LiveData keeps a strong reference to the observer and the owner as long as the - * given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to - * the observer and the owner. - * - * If the given owner is already in [Lifecycle.State.DESTROYED] state, LiveData - * ignores the call. - */ -@Deprecated( - "This extension method is not required when using Kotlin 1.4. " + - "You should remove \"import androidx.lifecycle.observe\"" -) -@MainThread -public inline fun LiveData.observe( - owner: LifecycleOwner, - crossinline onChanged: (T) -> Unit -): Observer { - val wrappedObserver = Observer { t -> onChanged.invoke(t) } - observe(owner, wrappedObserver) - return wrappedObserver -} diff --git a/app/src/main/java/androidx/lifecycle/Transformations.kt b/app/src/main/java/androidx/lifecycle/Transformations.kt index b1e55d1f6f..e7bbf1f23d 100644 --- a/app/src/main/java/androidx/lifecycle/Transformations.kt +++ b/app/src/main/java/androidx/lifecycle/Transformations.kt @@ -49,10 +49,9 @@ import androidx.arch.core.util.Function fun LiveData.map( transform: (@JvmSuppressWildcards X) -> (@JvmSuppressWildcards Y) ): LiveData { - val result = if (isInitialized) { - MediatorLiveData(transform(value as X)) - } else { - MediatorLiveData() + val result = MediatorLiveData() + if (isInitialized) { + result.value = transform(value as X) } result.addSource(this) { x -> result.value = transform(x) } return result @@ -122,16 +121,13 @@ fun LiveData.map(mapFunction: Function): LiveData { fun LiveData.switchMap( transform: (@JvmSuppressWildcards X) -> (@JvmSuppressWildcards LiveData)? ): LiveData { + val result = MediatorLiveData() var liveData: LiveData? = null - val result = if (isInitialized) { + if (isInitialized) { val initialLiveData = transform(value as X) if (initialLiveData != null && initialLiveData.isInitialized) { - MediatorLiveData(initialLiveData.value) - } else { - MediatorLiveData() + result.value = initialLiveData.value } - } else { - MediatorLiveData() } result.addSource(this) { value: X -> val newLiveData = transform(value) @@ -187,12 +183,11 @@ fun LiveData.switchMap(switchMapFunction: Function>): L @MainThread @CheckResult fun LiveData.distinctUntilChanged(): LiveData { + val outputLiveData = MediatorLiveData() var firstTime = true - val outputLiveData = if (isInitialized) { + if (isInitialized) { + outputLiveData.value = value firstTime = false - MediatorLiveData(value) - } else { - MediatorLiveData() } outputLiveData.addSource(this) { value -> val previousValue = outputLiveData.value