Skip to content

Commit

Permalink
fix: issues with live data
Browse files Browse the repository at this point in the history
remove live data transformations
  • Loading branch information
FunkyMuse committed May 22, 2024
1 parent 1211b99 commit 98a2d64
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -36,6 +37,7 @@ fun Project.configureKotlinOptions() {
tasks
.withType<org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.valueOf(versionCatalog.getVersion("app-build-jVMTarget")))
freeCompilerArgs.addAll(
listOf(
"-opt-in=kotlin.RequiresOptIn",
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.lifecycle.Observer
* [onEventUnhandledContent] is *only* called if the [SingleEvent]'s contents has not been handled.
*/
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<SingleEvent<T>> {
override fun onChanged(singleEvent: SingleEvent<T>?) {
singleEvent?.getContentIfNotHandled()?.let { value ->
override fun onChanged(singleEvent: SingleEvent<T>) {
singleEvent.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> LiveData<T>.observeOnce(observer: Observer<T>) {
observeForever(object : Observer<T> {
override fun onChanged(t: T?) {
override fun onChanged(t: T) {
observer.onChanged(t)
removeObserver(this)
}
Expand Down Expand Up @@ -316,14 +315,6 @@ fun <T> LiveData<T>.buffer(count: Int): MutableLiveData<List<T?>> {
return mutableLiveData
}


fun <X, Y> LiveData<X>.map(mapFunction: (value: X?) -> Y?) =
Transformations.map(this, mapFunction)


fun <X, Y> LiveData<X>.switchMap(mapFunction: (value: X?) -> LiveData<Y>): LiveData<Y> =
Transformations.switchMap(this, mapFunction)

inline fun <T> LiveData<SingleEvent<T>>.observeEvent(owner: LifecycleOwner, crossinline onEventUnhandledContent: (T) -> Unit) {
observe(owner, Observer { it?.getContentIfNotHandled()?.let(onEventUnhandledContent) })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class SingleLiveEvent<T> : MutableLiveData<T>() {
}

// Observe the internal MutableLiveData
super.observe(owner, { t ->
super.observe(owner) { t ->
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t)
}
})
}
}

@MainThread
Expand Down

0 comments on commit 98a2d64

Please sign in to comment.