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

View File

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