Simplify UI code for Magisk logs

We have all texts, no need to go through recyclerview
This commit is contained in:
topjohnwu 2020-06-29 05:22:16 -07:00
parent ddc2f317ab
commit 89e9e7c176
3 changed files with 41 additions and 32 deletions

View File

@ -15,8 +15,22 @@ class LogRepository(
fun fetchLogs() = logDao.fetchAll()
fun fetchMagiskLogs() = Single.fromCallable {
Shell.su("tail -n 5000 ${Const.MAGISK_LOG}").exec().out
}.flattenAsFlowable { it }.filter { it.isNotEmpty() }
val list = object : AbstractMutableList<String>() {
val buf = StringBuilder()
override val size get() = 0
override fun get(index: Int): String = ""
override fun removeAt(index: Int): String = ""
override fun set(index: Int, element: String): String = ""
override fun add(index: Int, element: String) {
if (element.isNotEmpty()) {
buf.append(element)
buf.append('\n')
}
}
}
Shell.su("cat ${Const.MAGISK_LOG}").to(list).exec()
list.buf.toString()
}
fun clearLogs() = logDao.deleteAll()

View File

@ -6,14 +6,13 @@ import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.data.repository.LogRepository
import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.model.binding.BindingAdapter
import com.topjohnwu.magisk.model.entity.recycler.ConsoleItem
import com.topjohnwu.magisk.model.entity.recycler.LogItem
import com.topjohnwu.magisk.model.entity.recycler.TextItem
import com.topjohnwu.magisk.model.events.SnackbarEvent
import com.topjohnwu.magisk.ui.base.BaseViewModel
import com.topjohnwu.magisk.ui.base.diffListOf
import com.topjohnwu.magisk.ui.base.itemBindingOf
import com.topjohnwu.magisk.utils.KObservableField
import com.topjohnwu.superuser.Shell
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
@ -32,18 +31,16 @@ class LogViewModel(
val itemEmpty = TextItem(R.string.log_data_none)
val itemMagiskEmpty = TextItem(R.string.log_data_magisk_none)
// --- main view
// --- su log
val items = diffListOf<LogItem>()
val itemBinding = itemBindingOf<LogItem> {
it.bindExtra(BR.viewModel, this)
}
// --- console
// --- magisk log
val consoleAdapter = BindingAdapter<ConsoleItem>()
val itemsConsole = diffListOf<ConsoleItem>()
val itemConsoleBinding = itemBindingOf<ConsoleItem>()
val consoleText = KObservableField(" ")
override fun refresh(): Disposable {
val logs = repo.fetchLogs()
@ -63,12 +60,7 @@ class LogViewModel(
.ignoreElement()
val console = repo.fetchMagiskLogs()
.map { ConsoleItem(it) }
.toList()
.observeOn(Schedulers.computation())
.map { it to itemsConsole.calculateDiff(it) }
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess { itemsConsole.update(it.first, it.second) }
.doOnSuccess { consoleText.value = it }
.ignoreElement()
return Completable.merge(listOf(logs, console)).subscribeK()

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
@ -15,28 +14,32 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
<ScrollView
gone="@{viewModel.consoleText.empty}"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
adapter="@{viewModel.consoleAdapter}"
itemBinding="@{viewModel.itemConsoleBinding}"
items="@{viewModel.itemsConsole}"
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="@{viewModel.consoleText}"
android:textAppearance="@style/AppearanceFoundation.Caption"
android:textSize="10sp"
android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size}"
android:paddingBottom="@{viewModel.insets.bottom}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_console_md2"
tools:paddingTop="24dp" />
tools:text="@tools:sample/lorem/random" />
</HorizontalScrollView>
</ScrollView>
<FrameLayout
gone="@{!viewModel.itemsConsole.empty}"
gone="@{!viewModel.consoleText.empty}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">