From 98a2d645f7a370369d186875fd2789657d9bf8c6 Mon Sep 17 00:00:00 2001 From: FunkyMuse Date: Wed, 22 May 2024 11:11:13 +0200 Subject: [PATCH] fix: issues with live data remove live data transformations --- .../dev/funkymuse/kahelpers/ProjectExtensions.kt | 2 ++ gradle/libs.versions.toml | 1 + .../main/java/dev/funkymuse/livedata/EventObserver.kt | 4 ++-- .../java/dev/funkymuse/livedata/LiveDataExtensions.kt | 11 +---------- .../java/dev/funkymuse/livedata/SingleLiveEvent.kt | 4 ++-- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/build-logic/convention/src/main/kotlin/dev/funkymuse/kahelpers/ProjectExtensions.kt b/build-logic/convention/src/main/kotlin/dev/funkymuse/kahelpers/ProjectExtensions.kt index ffe3a211b..da9faa849 100644 --- a/build-logic/convention/src/main/kotlin/dev/funkymuse/kahelpers/ProjectExtensions.kt +++ b/build-logic/convention/src/main/kotlin/dev/funkymuse/kahelpers/ProjectExtensions.kt @@ -9,6 +9,7 @@ import org.gradle.api.JavaVersion import org.gradle.api.Project import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.withType +import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.tasks.KotlinCompile fun CommonExtension<*, *, *, *, *, *>.configureBuildFeatures() { @@ -36,6 +37,7 @@ fun Project.configureKotlinOptions() { tasks .withType().configureEach { compilerOptions { + jvmTarget.set(JvmTarget.valueOf(versionCatalog.getVersion("app-build-jVMTarget"))) freeCompilerArgs.addAll( listOf( "-opt-in=kotlin.RequiresOptIn", diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3d675ba9e..3def6d47d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -59,6 +59,7 @@ app-build-targetSDK = "34" app-build-minimumSDK = "21" app-build-testRunner = "androidx.test.runner.AndroidJUnitRunner" app-build-kotlinJVMTarget = "17" +app-build-jVMTarget = "JVM_17" app-build-javaVersion = "VERSION_17" #versioning app-version-appId = "dev.funkymuse.setofusefulkotlinextensions" diff --git a/livedata/src/main/java/dev/funkymuse/livedata/EventObserver.kt b/livedata/src/main/java/dev/funkymuse/livedata/EventObserver.kt index bfe5449b4..4c059564d 100644 --- a/livedata/src/main/java/dev/funkymuse/livedata/EventObserver.kt +++ b/livedata/src/main/java/dev/funkymuse/livedata/EventObserver.kt @@ -10,8 +10,8 @@ import androidx.lifecycle.Observer * [onEventUnhandledContent] is *only* called if the [SingleEvent]'s contents has not been handled. */ class EventObserver(private val onEventUnhandledContent: (T) -> Unit) : Observer> { - override fun onChanged(singleEvent: SingleEvent?) { - singleEvent?.getContentIfNotHandled()?.let { value -> + override fun onChanged(singleEvent: SingleEvent) { + singleEvent.getContentIfNotHandled()?.let { value -> onEventUnhandledContent(value) } } diff --git a/livedata/src/main/java/dev/funkymuse/livedata/LiveDataExtensions.kt b/livedata/src/main/java/dev/funkymuse/livedata/LiveDataExtensions.kt index 9f9249a61..aafa47486 100644 --- a/livedata/src/main/java/dev/funkymuse/livedata/LiveDataExtensions.kt +++ b/livedata/src/main/java/dev/funkymuse/livedata/LiveDataExtensions.kt @@ -5,12 +5,11 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer -import androidx.lifecycle.Transformations fun LiveData.observeOnce(observer: Observer) { observeForever(object : Observer { - override fun onChanged(t: T?) { + override fun onChanged(t: T) { observer.onChanged(t) removeObserver(this) } @@ -316,14 +315,6 @@ fun LiveData.buffer(count: Int): MutableLiveData> { return mutableLiveData } - -fun LiveData.map(mapFunction: (value: X?) -> Y?) = - Transformations.map(this, mapFunction) - - -fun LiveData.switchMap(mapFunction: (value: X?) -> LiveData): LiveData = - Transformations.switchMap(this, mapFunction) - inline fun LiveData>.observeEvent(owner: LifecycleOwner, crossinline onEventUnhandledContent: (T) -> Unit) { observe(owner, Observer { it?.getContentIfNotHandled()?.let(onEventUnhandledContent) }) } diff --git a/livedata/src/main/java/dev/funkymuse/livedata/SingleLiveEvent.kt b/livedata/src/main/java/dev/funkymuse/livedata/SingleLiveEvent.kt index b538b3a95..fd7f427b2 100644 --- a/livedata/src/main/java/dev/funkymuse/livedata/SingleLiveEvent.kt +++ b/livedata/src/main/java/dev/funkymuse/livedata/SingleLiveEvent.kt @@ -32,11 +32,11 @@ class SingleLiveEvent : MutableLiveData() { } // Observe the internal MutableLiveData - super.observe(owner, { t -> + super.observe(owner) { t -> if (mPending.compareAndSet(true, false)) { observer.onChanged(t) } - }) + } } @MainThread