From 7d6eebdae3e2199c4ec9cd67b65912055b1308d9 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Tue, 29 Oct 2019 16:50:01 +0100 Subject: [PATCH] Fixed unreasonable change resulting in major breakage all around the app --- .../magisk/base/viewmodel/BaseViewModel.kt | 8 +--- .../magisk/utils/KObservableField.kt | 37 ++++++++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/base/viewmodel/BaseViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/base/viewmodel/BaseViewModel.kt index 987f1bf6d..13697e12b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/base/viewmodel/BaseViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/base/viewmodel/BaseViewModel.kt @@ -5,7 +5,7 @@ import com.topjohnwu.magisk.extensions.doOnSubscribeUi import com.topjohnwu.magisk.model.events.BackPressEvent import com.topjohnwu.magisk.model.events.PermissionEvent import com.topjohnwu.magisk.model.events.ViewActionEvent -import com.topjohnwu.magisk.utils.KObservableField +import com.topjohnwu.magisk.model.observer.Observer import io.reactivex.Observable import io.reactivex.subjects.PublishSubject import com.topjohnwu.magisk.Info.isConnected as gIsConnected @@ -15,11 +15,7 @@ abstract class BaseViewModel( initialState: State = State.LOADING ) : LoadingViewModel(initialState) { - val isConnected = object : KObservableField(gIsConnected.value, gIsConnected) { - override fun get(): Boolean { - return gIsConnected.value - } - } + val isConnected = Observer(gIsConnected) { gIsConnected.value } fun withView(action: Activity.() -> Unit) { ViewActionEvent(action).publish() diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/KObservableField.kt b/app/src/main/java/com/topjohnwu/magisk/utils/KObservableField.kt index cda0e5eb0..b9ae8f251 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/KObservableField.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/KObservableField.kt @@ -2,18 +2,30 @@ package com.topjohnwu.magisk.utils import androidx.databinding.Observable import androidx.databinding.ObservableField +import com.topjohnwu.magisk.model.observer.Observer import java.io.Serializable /** * Kotlin version of [ObservableField]. * You can define if wrapped type is Nullable or not. * You can use kotlin get/set syntax for value + * + * ## Notes + * This stays final for fuck's sake. Too many things depend on it, so you just cannot go around and + * change it randomly. Even though you think you're improving the design, you might be fucking this + * up in unimaginable ways. So DON'T TOUCH THIS. + * + * In order to have value-less observer you need - you guessed it - **a fucking [Observer]**! */ -open class KObservableField : ObservableField, Serializable { +class KObservableField : ObservableField, Serializable { var value: T - get() = get() - set(value) { set(value) } + set(value) { + if (field != value) { + field = value + notifyChange() + } + } constructor(init: T) { value = init @@ -23,8 +35,23 @@ open class KObservableField : ObservableField, Serializable { value = init } - @Suppress("UNCHECKED_CAST") + @Deprecated( + message = "Needed for data binding, use KObservableField.value syntax from code", + replaceWith = ReplaceWith("value") + ) override fun get(): T { - return super.get() as T + return value + } + + @Deprecated( + message = "Needed for data binding, use KObservableField.value = ... syntax from code", + replaceWith = ReplaceWith("value = newValue") + ) + override fun set(newValue: T) { + value = newValue + } + + override fun toString(): String { + return "KObservableField(value=$value)" } }