From 8453282fa6580021201b6bdbb15ef9f320b7f1e8 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 16 Feb 2020 19:04:26 -0800 Subject: [PATCH] Improve flash console screen --- .../topjohnwu/magisk/di/ViewModelsModule.kt | 5 +-- .../magisk/legacy/flash/FlashActivity.kt | 9 ++--- .../magisk/legacy/flash/FlashViewModel.kt | 27 +++++++------- .../magisk/model/binding/BindingAdapter.kt | 8 ++--- .../{ConsoleRvItem.kt => ConsoleItem.kt} | 13 +++++-- .../topjohnwu/magisk/ui/log/LogViewModel.kt | 7 ++-- .../magisk/utils/DataBindingAdapters.kt | 9 ++--- app/src/main/res/layout/activity_flash.xml | 35 ++++++++++++------- .../main/res/layout/include_log_magisk.xml | 2 +- app/src/main/res/layout/item_console_md2.xml | 5 --- build.gradle | 2 +- 11 files changed, 63 insertions(+), 59 deletions(-) rename app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/{ConsoleRvItem.kt => ConsoleItem.kt} (73%) diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt index d85f332d5..527aceab6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.di -import android.net.Uri import com.topjohnwu.magisk.legacy.flash.FlashViewModel import com.topjohnwu.magisk.legacy.surequest.SuRequestViewModel import com.topjohnwu.magisk.ui.MainViewModel @@ -31,8 +30,6 @@ val viewModelModules = module { viewModel { MainViewModel() } // Legacy - viewModel { (action: String, file: Uri, additional: Uri) -> - FlashViewModel(action, file, additional, get()) - } + viewModel { FlashViewModel(get()) } viewModel { SuRequestViewModel(get(), get(), get(SUTimeout), get()) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashActivity.kt b/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashActivity.kt index 86d146f95..122c0260a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashActivity.kt @@ -18,18 +18,12 @@ import com.topjohnwu.magisk.model.events.ViewEvent import com.topjohnwu.magisk.ui.base.BaseUIActivity import com.topjohnwu.magisk.ui.base.CompatNavigationDelegate import org.koin.androidx.viewmodel.ext.android.viewModel -import org.koin.core.parameter.parametersOf import java.io.File open class FlashActivity : BaseUIActivity() { override val layoutRes: Int = R.layout.activity_flash - override val viewModel: FlashViewModel by viewModel { - val uri = intent.data ?: let { finish(); Uri.EMPTY } - val additionalUri = intent.getParcelableExtra(Const.Key.FLASH_DATA) ?: uri - val action = intent.getStringExtra(Const.Key.FLASH_ACTION) ?: let { finish();"" } - parametersOf(action, uri, additionalUri) - } + override val viewModel: FlashViewModel by viewModel() override val navigation: CompatNavigationDelegate>? = null @@ -40,6 +34,7 @@ open class FlashActivity : BaseUIActivity( val id = intent.getIntExtra(Const.Key.DISMISS_ID, -1) if (id != -1) Notifications.mgr.cancel(id) + viewModel.startFlashing(intent) } override fun onBackPressed() { diff --git a/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashViewModel.kt index eececd0fd..662284916 100644 --- a/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/legacy/flash/FlashViewModel.kt @@ -2,35 +2,32 @@ package com.topjohnwu.magisk.legacy.flash import android.Manifest.permission.READ_EXTERNAL_STORAGE import android.Manifest.permission.WRITE_EXTERNAL_STORAGE +import android.content.Intent import android.content.res.Resources import android.net.Uri import android.os.Handler import android.view.MenuItem import androidx.core.os.postDelayed import androidx.databinding.ObservableArrayList -import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Const -import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.extensions.* +import com.topjohnwu.magisk.model.binding.BindingAdapter import com.topjohnwu.magisk.model.entity.recycler.ConsoleItem import com.topjohnwu.magisk.model.events.SnackbarEvent import com.topjohnwu.magisk.model.flash.FlashResultListener import com.topjohnwu.magisk.model.flash.Flashing import com.topjohnwu.magisk.model.flash.Patching import com.topjohnwu.magisk.ui.base.BaseViewModel -import com.topjohnwu.magisk.utils.DiffObservableList +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 me.tatarka.bindingcollectionadapter2.ItemBinding import java.io.File import java.util.* class FlashViewModel( - action: String, - installer: Uri, - uri: Uri, private val resources: Resources ) : BaseViewModel(), FlashResultListener { @@ -39,11 +36,9 @@ class FlashViewModel( val behaviorText = KObservableField(resources.getString(R.string.flashing)) - val items = DiffObservableList(ComparableRvItem.callback) - val itemBinding = ItemBinding.of> { itemBinding, _, item -> - item.bind(itemBinding) - itemBinding.bindExtra(BR.viewModel, this@FlashViewModel) - } + val adapter = BindingAdapter() + val items = diffListOf() + val itemBinding = itemBindingOf() private val outItems = ObservableArrayList() private val logItems = Collections.synchronizedList(mutableListOf()) @@ -51,8 +46,12 @@ class FlashViewModel( init { outItems.sendUpdatesTo(items) { it.map { ConsoleItem(it) } } outItems.copyNewInputInto(logItems) + } - state = State.LOADING + fun startFlashing(intent: Intent) { + val installer = intent.data ?: return + val uri: Uri? = intent.getParcelableExtra(Const.Key.FLASH_DATA) + val action = intent.getStringExtra(Const.Key.FLASH_ACTION) ?: return when (action) { Const.Value.FLASH_ZIP -> Flashing @@ -68,7 +67,7 @@ class FlashViewModel( .SecondSlot(installer, outItems, logItems, this) .exec() Const.Value.PATCH_FILE -> Patching - .File(installer, uri, outItems, logItems, this) + .File(installer, uri ?: return, outItems, logItems, this) .exec() } } diff --git a/app/src/main/java/com/topjohnwu/magisk/model/binding/BindingAdapter.kt b/app/src/main/java/com/topjohnwu/magisk/model/binding/BindingAdapter.kt index ec4cd43b2..4c4361317 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/binding/BindingAdapter.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/binding/BindingAdapter.kt @@ -6,7 +6,7 @@ import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.model.entity.recycler.LenientRvItem import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter -class BindingAdapter : BindingRecyclerViewAdapter>() { +class BindingAdapter > : BindingRecyclerViewAdapter() { private var recyclerView: RecyclerView? = null @@ -15,12 +15,12 @@ class BindingAdapter : BindingRecyclerViewAdapter>() { variableId: Int, layoutRes: Int, position: Int, - item: ComparableRvItem<*> + item: T ) { super.onBindBinding(binding, variableId, layoutRes, position, item) when (item) { - is LenientRvItem -> { + is LenientRvItem<*> -> { val recycler = recyclerView ?: return item.onBindingBound(binding) item.onBindingBound(binding, recycler) @@ -34,4 +34,4 @@ class BindingAdapter : BindingRecyclerViewAdapter>() { this.recyclerView = recyclerView } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ConsoleRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ConsoleItem.kt similarity index 73% rename from app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ConsoleRvItem.kt rename to app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ConsoleItem.kt index 3b099a787..3430dab43 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ConsoleRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ConsoleItem.kt @@ -1,18 +1,27 @@ package com.topjohnwu.magisk.model.entity.recycler +import android.view.View import android.widget.TextView import androidx.core.view.updateLayoutParams import androidx.databinding.ViewDataBinding import androidx.recyclerview.widget.RecyclerView import com.topjohnwu.magisk.R +import kotlin.math.max class ConsoleItem(val item: String) : LenientRvItem() { override val layoutRes = R.layout.item_console_md2 + private var parentWidth = -1 + override fun onBindingBound(binding: ViewDataBinding, recyclerView: RecyclerView) { + if (parentWidth < 0) + parentWidth = (recyclerView.parent as View).width + val view = binding.root as TextView view.measure(0, 0) - val desiredWidth = view.measuredWidth + + // We want our recyclerView at least as wide as screen + val desiredWidth = max(view.measuredWidth, parentWidth) view.updateLayoutParams { width = desiredWidth } @@ -23,4 +32,4 @@ class ConsoleItem(val item: String) : LenientRvItem() { override fun contentSameAs(other: ConsoleItem) = itemSameAs(other) override fun itemSameAs(other: ConsoleItem) = item == other.item -} \ No newline at end of file +} diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt index 0e39b71c3..752589e28 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt @@ -5,7 +5,6 @@ import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.data.repository.LogRepository -import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.extensions.subscribeK import com.topjohnwu.magisk.model.binding.BindingAdapter import com.topjohnwu.magisk.model.entity.recycler.ConsoleItem @@ -42,9 +41,9 @@ class LogViewModel( // --- console - val consoleAdapter = BindingAdapter() - val itemsConsole = diffListOf>() - val itemConsoleBinding = itemBindingOf> {} + val consoleAdapter = BindingAdapter() + val itemsConsole = diffListOf() + val itemConsoleBinding = itemBindingOf() override fun refresh(): Disposable { val logs = repo.fetchLogs() diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt b/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt index 34ecc9969..83ce0e6bb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt @@ -23,6 +23,7 @@ import com.google.android.material.textfield.TextInputLayout import com.topjohnwu.magisk.R import com.topjohnwu.magisk.extensions.replaceRandomWithSpecial import com.topjohnwu.magisk.extensions.subscribeK +import com.topjohnwu.superuser.internal.UiThreadHandler import io.reactivex.Observable import io.reactivex.disposables.Disposable import java.util.concurrent.TimeUnit @@ -62,12 +63,12 @@ fun setOnTouchListener(view: View, listener: View.OnTouchListener) { @BindingAdapter("scrollToLast") fun setScrollToLast(view: RecyclerView, shouldScrollToLast: Boolean) { - fun scrollToLast() = view.post { + fun scrollToLast() = UiThreadHandler.handler.postDelayed({ view.scrollToPosition(view.adapter?.itemCount?.minus(1) ?: 0) - } + }, 30) fun wait(callback: () -> Unit) { - Observable.timer(1, TimeUnit.SECONDS).subscribeK { callback() } + UiThreadHandler.handler.postDelayed(callback, 1000) } fun RecyclerView.Adapter<*>.setListener() { @@ -241,4 +242,4 @@ fun RecyclerView.setSpanCount(count: Int) { is GridLayoutManager -> lama.spanCount = count is StaggeredGridLayoutManager -> lama.spanCount = count } -} \ No newline at end of file +} diff --git a/app/src/main/res/layout/activity_flash.xml b/app/src/main/res/layout/activity_flash.xml index 75b8c3578..17a46775e 100644 --- a/app/src/main/res/layout/activity_flash.xml +++ b/app/src/main/res/layout/activity_flash.xml @@ -56,21 +56,30 @@ - + android:layout_marginTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size}" + tools:layout_marginTop="@dimen/internal_action_bar_size"> + + + + diff --git a/app/src/main/res/layout/item_console_md2.xml b/app/src/main/res/layout/item_console_md2.xml index d015fb33e..ba9dc0d35 100644 --- a/app/src/main/res/layout/item_console_md2.xml +++ b/app/src/main/res/layout/item_console_md2.xml @@ -8,11 +8,6 @@ name="item" type="com.topjohnwu.magisk.model.entity.recycler.ConsoleItem" /> - - -