Reduce usage of delegation

This commit is contained in:
topjohnwu
2020-07-15 01:21:57 -07:00
parent ba31c6b625
commit 6c6368fd81
15 changed files with 226 additions and 213 deletions
@@ -7,17 +7,21 @@ 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
import com.topjohnwu.magisk.utils.observable
import com.topjohnwu.magisk.utils.set
class LogItem(val item: MagiskLog) : ObservableItem<LogItem>() {
override val layoutRes = R.layout.item_log_access_md2
val date = item.time.toTime(timeDateFormat)
@get:Bindable
var isTop by observable(false, BR.top)
var isTop = false
set(value) = set(value, field, { field = it }, BR.top)
@get:Bindable
var isBottom by observable(false, BR.bottom)
var isBottom = false
set(value) = set(value, field, { field = it }, BR.bottom)
override fun itemSameAs(other: LogItem) = item.appName == other.item.appName
@@ -11,7 +11,7 @@ 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
import com.topjohnwu.magisk.utils.observable
import com.topjohnwu.magisk.utils.set
object InstallModule : ComparableRvItem<InstallModule>() {
override val layoutRes = R.layout.item_module_download
@@ -34,11 +34,16 @@ class SectionTitle(
override val layoutRes = R.layout.item_section_md2
@get:Bindable
var button by observable(_button, BR.button)
var button = _button
set(value) = set(value, field, { field = it }, BR.button)
@get:Bindable
var icon by observable(_icon, BR.icon)
var icon = _icon
set(value) = set(value, field, { field = it }, BR.icon)
@get:Bindable
var hasButton by observable(_button != 0 && _icon != 0, BR.hasButton)
var hasButton = _button != 0 && _icon != 0
set(value) = set(value, field, { field = it }, BR.hasButton)
override fun onBindingBound(binding: ViewDataBinding) {
super.onBindingBound(binding)
@@ -54,9 +59,13 @@ sealed class RepoItem(val item: Repo) : ObservableItem<RepoItem>() {
override val layoutRes: Int = R.layout.item_repo_md2
@get:Bindable
var progress by observable(0, BR.progress)
var progress = 0
set(value) = set(value, field, { field = it }, BR.progress)
@get:Bindable
var isUpdate by observable(false, BR.update)
var isUpdate = false
set(value) = set(value, field, { field = it }, BR.update)
override fun contentSameAs(other: RepoItem): Boolean = item == other.item
override fun itemSameAs(other: RepoItem): Boolean = item.id == other.item.id
@@ -75,7 +84,8 @@ class ModuleItem(val item: Module) : ObservableItem<ModuleItem>(), Observable {
override val layoutRes = R.layout.item_module_md2
@get:Bindable
var repo: Repo? by observable(null, BR.repo)
var repo: Repo? = null
set(value) = set(value, field, { field = it }, BR.repo)
@get:Bindable
var isEnabled
@@ -13,7 +13,7 @@ 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.utils.observable
import com.topjohnwu.magisk.utils.set
import com.topjohnwu.magisk.view.MagiskDialog
import org.koin.core.KoinComponent
import org.koin.core.get
@@ -27,7 +27,8 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
open val description: TransitiveText get() = TransitiveText.EMPTY
@get:Bindable
var isEnabled by observable(true, BR.enabled)
var isEnabled = true
set(value) = set(value, field, { field = it }, BR.enabled)
protected open val isFullSpan get() = false
@@ -63,12 +64,15 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
@get:Bindable
abstract var value: T
protected inline fun <reified T> value(
initialValue: T,
vararg fieldIds: Int,
crossinline setter: (T) -> Unit = {}
) = observable(initialValue, BR.value, *fieldIds, afterChanged = setter)
protected inline fun <reified T> setV(
new: T, old: T, setter: (T) -> Unit, vararg fieldIds: Int) {
set(new, old, setter, BR.value, *fieldIds)
}
protected inline fun <reified T> setV(
new: T, old: T, setter: (T) -> Unit, afterChanged: (T) -> Unit = {}) {
set(new, old, setter, BR.value, afterChanged = afterChanged)
}
}
abstract class Toggle : Value<Boolean>() {
@@ -142,10 +146,10 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
val selectedEntry
get() = entries.getOrNull(value)
/* override */ protected inline fun value(
initialValue: Int,
crossinline setter: (Int) -> Unit
) = observable(initialValue, BR.value, BR.selectedEntry, BR.description, afterChanged = setter)
protected inline fun <reified T> setS(
new: T, old: T, setter: (T) -> Unit, afterChanged: (T) -> Unit = {}) {
set(new, old, setter, BR.value, BR.selectedEntry, BR.description, afterChanged = afterChanged)
}
private fun Resources.getArrayOrEmpty(id: Int): Array<String> =
runCatching { getStringArray(id) }.getOrDefault(emptyArray())