Commit 5d632d0d authored by Viktor De Pasquale's avatar Viktor De Pasquale

Removed unnecessary overriding of observable list and replaced it copy...

Removed unnecessary overriding of observable list and replaced it copy function within observable changed callback
parent 4eecaea6
......@@ -24,8 +24,7 @@ import com.topjohnwu.magisk.utils.*
import com.topjohnwu.superuser.Shell
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.File
import java.util.Collections
import kotlin.collections.ArrayList
import java.util.*
class FlashViewModel(
action: String,
......@@ -44,17 +43,12 @@ class FlashViewModel(
itemBinding.bindExtra(BR.viewModel, this@FlashViewModel)
}
private val outItems = object : ObservableArrayList<String>() {
override fun add(element: String?): Boolean {
if (element != null)
logItems.add(element)
return super.add(element)
}
}
private val logItems = Collections.synchronizedList(ArrayList<String>())
private val outItems = ObservableArrayList<String>()
private val logItems = Collections.synchronizedList(mutableListOf<String>())
init {
outItems.sendUpdatesTo(items) { it.map { ConsoleRvItem(it) } }
outItems.copyNewInputInto(logItems)
state = State.LOADING
......
......@@ -10,7 +10,7 @@ fun <T> MutableList<T>.update(newList: List<T>) {
addAll(newList)
}
fun List<String>.toShellCmd() : String {
fun List<String>.toShellCmd(): String {
val sb = StringBuilder()
for (s in this) {
if (s.contains(" ")) {
......@@ -27,8 +27,7 @@ fun List<String>.toShellCmd() : String {
fun <T1, T2> ObservableList<T1>.sendUpdatesTo(
target: DiffObservableList<T2>,
mapper: (List<T1>) -> List<T2>
) {
addOnListChangedCallback(object :
) = addOnListChangedCallback(object :
ObservableList.OnListChangedCallback<ObservableList<T1>>() {
override fun onChanged(sender: ObservableList<T1>?) {
updateAsync(sender ?: return)
......@@ -59,5 +58,22 @@ fun <T1, T2> ObservableList<T1>.sendUpdatesTo(
.map { it to target.calculateDiff(it) }
.subscribeK { target.update(it.first, it.second) }
}
})
}
\ No newline at end of file
})
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)
}
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment