Created
July 19, 2019 07:17
-
-
Save arkty/02d9a44df254c27d7aba62cf0b5c6765 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BooleanPreference( | |
private val name: String, | |
private val default: Boolean = false | |
) { | |
operator fun getValue(thisRef: LocalStorage, property: KProperty<*>): Boolean = | |
thisRef.prefs.getBoolean(name, default) | |
operator fun setValue(thisRef: LocalStorage, property: KProperty<*>, value: Boolean) = | |
thisRef.prefs.edit().putBoolean(name, value).apply() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MutableLiveDataProperty<T>( | |
val p: KMutableProperty0<T> | |
) : MutableLiveData<T>() { | |
init { | |
postValue(p.get()) | |
observeForever { | |
p.set(it) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MutablePropertyDelegate<T>( | |
val p: KMutableProperty0<T> | |
) { | |
operator fun getValue(thisRef: Any, property: KProperty<*>): T = p.get() | |
operator fun setValue(thisRef: Any, property: KProperty<*>, value: T) = p.set(value) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample | |
class LocalStorage(ctx: Context) { | |
var showUsername by BooleanPreference("showUsername", true) | |
} | |
class DashboardModel( | |
private val s: LocalStorage | |
) { | |
var showUsername: Boolean by MutablePropertyDelegate(s::showUsername) | |
} | |
class DashboardViewModel( | |
private val m: DashboardModel | |
) { | |
val showUsername = MutableLiveDataProperty(m::showUsername) | |
fun toggleShowUsername() { | |
showUsername.postValue(!showUsername.value!!) | |
} | |
} | |
/* | |
<TextView text='@{vm.showUsername vm.username ? "..."}' onClick="@{() -> vm.toggleShowUsername()}" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment