Removed unnecessary overriding of observable list and replaced it copy function within observable changed callback

This commit is contained in:
Viktor De Pasquale 2019-05-05 12:46:28 +02:00 committed by topjohnwu
parent 79d704008b
commit 0167bd76f1
2 changed files with 49 additions and 39 deletions

View File

@ -24,8 +24,7 @@ import com.topjohnwu.magisk.utils.*
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import me.tatarka.bindingcollectionadapter2.ItemBinding import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.File import java.io.File
import java.util.Collections import java.util.*
import kotlin.collections.ArrayList
class FlashViewModel( class FlashViewModel(
action: String, action: String,
@ -44,17 +43,12 @@ class FlashViewModel(
itemBinding.bindExtra(BR.viewModel, this@FlashViewModel) itemBinding.bindExtra(BR.viewModel, this@FlashViewModel)
} }
private val outItems = object : ObservableArrayList<String>() { private val outItems = ObservableArrayList<String>()
override fun add(element: String?): Boolean { private val logItems = Collections.synchronizedList(mutableListOf<String>())
if (element != null)
logItems.add(element)
return super.add(element)
}
}
private val logItems = Collections.synchronizedList(ArrayList<String>())
init { init {
outItems.sendUpdatesTo(items) { it.map { ConsoleRvItem(it) } } outItems.sendUpdatesTo(items) { it.map { ConsoleRvItem(it) } }
outItems.copyNewInputInto(logItems)
state = State.LOADING state = State.LOADING

View File

@ -10,7 +10,7 @@ fun <T> MutableList<T>.update(newList: List<T>) {
addAll(newList) addAll(newList)
} }
fun List<String>.toShellCmd() : String { fun List<String>.toShellCmd(): String {
val sb = StringBuilder() val sb = StringBuilder()
for (s in this) { for (s in this) {
if (s.contains(" ")) { if (s.contains(" ")) {
@ -27,37 +27,53 @@ fun List<String>.toShellCmd() : String {
fun <T1, T2> ObservableList<T1>.sendUpdatesTo( fun <T1, T2> ObservableList<T1>.sendUpdatesTo(
target: DiffObservableList<T2>, target: DiffObservableList<T2>,
mapper: (List<T1>) -> List<T2> mapper: (List<T1>) -> List<T2>
) { ) = addOnListChangedCallback(object :
addOnListChangedCallback(object : ObservableList.OnListChangedCallback<ObservableList<T1>>() {
ObservableList.OnListChangedCallback<ObservableList<T1>>() { override fun onChanged(sender: ObservableList<T1>?) {
override fun onChanged(sender: ObservableList<T1>?) { updateAsync(sender ?: return)
updateAsync(sender ?: return) }
}
override fun onItemRangeRemoved(sender: ObservableList<T1>?, p0: Int, p1: Int) { override fun onItemRangeRemoved(sender: ObservableList<T1>?, p0: Int, p1: Int) {
updateAsync(sender ?: return) updateAsync(sender ?: return)
} }
override fun onItemRangeMoved(sender: ObservableList<T1>?, p0: Int, p1: Int, p2: Int) { override fun onItemRangeMoved(sender: ObservableList<T1>?, p0: Int, p1: Int, p2: Int) {
updateAsync(sender ?: return) updateAsync(sender ?: return)
} }
override fun onItemRangeInserted(sender: ObservableList<T1>?, p0: Int, p1: Int) { override fun onItemRangeInserted(sender: ObservableList<T1>?, p0: Int, p1: Int) {
updateAsync(sender ?: return) updateAsync(sender ?: return)
} }
override fun onItemRangeChanged(sender: ObservableList<T1>?, p0: Int, p1: Int) { override fun onItemRangeChanged(sender: ObservableList<T1>?, p0: Int, p1: Int) {
updateAsync(sender ?: return) updateAsync(sender ?: return)
} }
private var updater: Disposable? = null private var updater: Disposable? = null
private fun updateAsync(sender: List<T1>) { private fun updateAsync(sender: List<T1>) {
updater?.dispose() updater?.dispose()
updater = sender.toSingle() updater = sender.toSingle()
.map { mapper(it) } .map { mapper(it) }
.map { it to target.calculateDiff(it) } .map { it to target.calculateDiff(it) }
.subscribeK { target.update(it.first, it.second) } .subscribeK { target.update(it.first, it.second) }
} }
}) })
}
fun <T1> ObservableList<T1>.copyNewInputInto(
target: MutableList<T1>
) = addOnListChangedCallback(object : ObservableList.OnListChangedCallback<ObservableList<T1>>() {
override fun onChanged(p0: ObservableList<T1>?) = Unit
override fun onItemRangeRemoved(p0: ObservableList<T1>?, p1: Int, p2: Int) = Unit
override fun onItemRangeMoved(p0: ObservableList<T1>?, p1: Int, p2: Int, p3: Int) = Unit
override fun onItemRangeChanged(p0: ObservableList<T1>?, p1: Int, p2: Int) = Unit
override fun onItemRangeInserted(
sender: ObservableList<T1>?,
positionStart: Int,
itemCount: Int
) {
val positionEnd = positionStart + itemCount
val addedValues = sender?.slice(positionStart until positionEnd).orEmpty()
target.addAll(addedValues)
}
})