diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index 968faeab4..99d72aa13 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -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() { - override fun add(element: String?): Boolean { - if (element != null) - logItems.add(element) - return super.add(element) - } - } - private val logItems = Collections.synchronizedList(ArrayList()) + private val outItems = ObservableArrayList() + private val logItems = Collections.synchronizedList(mutableListOf()) init { outItems.sendUpdatesTo(items) { it.map { ConsoleRvItem(it) } } + outItems.copyNewInputInto(logItems) state = State.LOADING diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/XList.kt b/app/src/main/java/com/topjohnwu/magisk/utils/XList.kt index 26b5009fa..fd4ddc2e5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/XList.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/XList.kt @@ -10,7 +10,7 @@ fun MutableList.update(newList: List) { addAll(newList) } -fun List.toShellCmd() : String { +fun List.toShellCmd(): String { val sb = StringBuilder() for (s in this) { if (s.contains(" ")) { @@ -27,37 +27,53 @@ fun List.toShellCmd() : String { fun ObservableList.sendUpdatesTo( target: DiffObservableList, mapper: (List) -> List -) { - addOnListChangedCallback(object : - ObservableList.OnListChangedCallback>() { - override fun onChanged(sender: ObservableList?) { - updateAsync(sender ?: return) - } +) = addOnListChangedCallback(object : + ObservableList.OnListChangedCallback>() { + override fun onChanged(sender: ObservableList?) { + updateAsync(sender ?: return) + } - override fun onItemRangeRemoved(sender: ObservableList?, p0: Int, p1: Int) { - updateAsync(sender ?: return) - } + override fun onItemRangeRemoved(sender: ObservableList?, p0: Int, p1: Int) { + updateAsync(sender ?: return) + } - override fun onItemRangeMoved(sender: ObservableList?, p0: Int, p1: Int, p2: Int) { - updateAsync(sender ?: return) - } + override fun onItemRangeMoved(sender: ObservableList?, p0: Int, p1: Int, p2: Int) { + updateAsync(sender ?: return) + } - override fun onItemRangeInserted(sender: ObservableList?, p0: Int, p1: Int) { - updateAsync(sender ?: return) - } + override fun onItemRangeInserted(sender: ObservableList?, p0: Int, p1: Int) { + updateAsync(sender ?: return) + } - override fun onItemRangeChanged(sender: ObservableList?, p0: Int, p1: Int) { - updateAsync(sender ?: return) - } + override fun onItemRangeChanged(sender: ObservableList?, p0: Int, p1: Int) { + updateAsync(sender ?: return) + } - private var updater: Disposable? = null + private var updater: Disposable? = null - private fun updateAsync(sender: List) { - updater?.dispose() - updater = sender.toSingle() - .map { mapper(it) } - .map { it to target.calculateDiff(it) } - .subscribeK { target.update(it.first, it.second) } - } - }) -} \ No newline at end of file + private fun updateAsync(sender: List) { + updater?.dispose() + updater = sender.toSingle() + .map { mapper(it) } + .map { it to target.calculateDiff(it) } + .subscribeK { target.update(it.first, it.second) } + } +}) + +fun ObservableList.copyNewInputInto( + target: MutableList +) = addOnListChangedCallback(object : ObservableList.OnListChangedCallback>() { + override fun onChanged(p0: ObservableList?) = Unit + override fun onItemRangeRemoved(p0: ObservableList?, p1: Int, p2: Int) = Unit + override fun onItemRangeMoved(p0: ObservableList?, p1: Int, p2: Int, p3: Int) = Unit + override fun onItemRangeChanged(p0: ObservableList?, p1: Int, p2: Int) = Unit + override fun onItemRangeInserted( + sender: ObservableList?, + 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