More efficient databinding

This commit is contained in:
topjohnwu
2020-07-12 03:17:50 -07:00
parent b41b2283f4
commit 2c12fe6eb2
26 changed files with 386 additions and 276 deletions
@@ -3,6 +3,7 @@ package com.topjohnwu.magisk.model.entity.recycler
import androidx.databinding.Bindable
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.ObservableItem
import com.topjohnwu.magisk.ktx.timeDateFormat
import com.topjohnwu.magisk.ktx.toTime
import com.topjohnwu.magisk.model.entity.MagiskLog
@@ -16,13 +17,13 @@ class LogItem(val item: MagiskLog) : ObservableItem<LogItem>() {
@Bindable get
set(value) {
field = value
notifyChange(BR.top)
notifyPropertyChanged(BR.top)
}
var isBottom = false
@Bindable get
set(value) {
field = value
notifyChange(BR.bottom)
notifyPropertyChanged(BR.bottom)
}
override fun itemSameAs(other: LogItem) = item.appName == other.item.appName
@@ -1,12 +1,16 @@
package com.topjohnwu.magisk.model.entity.recycler
import androidx.databinding.*
import androidx.databinding.Bindable
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.model.module.Module
import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.databinding.ComparableRvItem
import com.topjohnwu.magisk.databinding.ObservableItem
import com.topjohnwu.magisk.ui.module.ModuleViewModel
object InstallModule : ComparableRvItem<InstallModule>() {
@@ -33,19 +37,19 @@ class SectionTitle(
@Bindable get
set(value) {
field = value
notifyChange(BR.button)
notifyPropertyChanged(BR.button)
}
var icon = _icon
@Bindable get
set(value) {
field = value
notifyChange(BR.icon)
notifyPropertyChanged(BR.icon)
}
var hasButton = button != 0 || icon != 0
@Bindable get
set(value) {
field = value
notifyChange(BR.hasButton)
notifyPropertyChanged(BR.hasButton)
}
override fun onBindingBound(binding: ViewDataBinding) {
@@ -66,7 +70,7 @@ sealed class RepoItem(val item: Repo) : ObservableItem<RepoItem>() {
@Bindable get
protected set(value) {
field = value
notifyChange(BR.update)
notifyPropertyChanged(BR.update)
}
override fun contentSameAs(other: RepoItem): Boolean = item == other.item
@@ -89,7 +93,7 @@ class ModuleItem(val item: Module) : ObservableItem<ModuleItem>(), Observable {
var repo: Repo? = null
set(value) {
field = value
notifyChange(BR.repo)
notifyPropertyChanged(BR.repo)
}
@get:Bindable
@@ -97,7 +101,7 @@ class ModuleItem(val item: Module) : ObservableItem<ModuleItem>(), Observable {
get() = item.enable
set(value) {
item.enable = value
notifyChange(BR.enabled)
notifyPropertyChanged(BR.enabled)
}
@get:Bindable
@@ -105,7 +109,7 @@ class ModuleItem(val item: Module) : ObservableItem<ModuleItem>(), Observable {
get() = item.remove
set(value) {
item.remove = value
notifyChange(BR.removed)
notifyPropertyChanged(BR.removed)
}
val isUpdated get() = item.updated
@@ -126,21 +130,5 @@ class ModuleItem(val item: Module) : ObservableItem<ModuleItem>(), Observable {
&& item.name == other.item.name
override fun itemSameAs(other: ModuleItem): Boolean = item.id == other.item.id
}
abstract class ObservableItem<T> : ComparableRvItem<T>(), Observable {
private val list = PropertyChangeRegistry()
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
list.remove(callback ?: return)
}
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
list.add(callback ?: return)
}
fun notifyChange(id: Int) = list.notifyChange(this, id)
}
@@ -11,6 +11,7 @@ import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.ObservableItem
import com.topjohnwu.magisk.utils.TransitiveText
import com.topjohnwu.magisk.view.MagiskDialog
import org.koin.core.KoinComponent
@@ -37,7 +38,7 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
// notify only after the callback invocation; callback can invalidate the backing data,
// which wouldn't be recognized with reverse approach
notifyChange(BR.description)
notifyPropertyChanged(BR.description)
}
open fun refresh() {}
@@ -60,7 +61,7 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
) = object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) {
setter(newValue)
notifyChange(fieldId)
notifyPropertyChanged(fieldId)
}
}
@@ -169,7 +170,7 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
}
.applyAdapter(entries) {
value = it
notifyChange(BR.selectedEntry)
notifyPropertyChanged(BR.selectedEntry)
super.onPressed(view, callback)
}
.reveal()