From aabc36f86b3a294d8e37ff5b51f7b1634b372b26 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 10 Jun 2022 00:33:13 -0700 Subject: [PATCH] Maintain separate flash screen state --- .../magisk/ui/flash/FlashFragment.kt | 20 +++++++++------- .../magisk/ui/flash/FlashViewModel.kt | 23 ++++++++----------- .../main/res/layout/fragment_flash_md2.xml | 6 ++--- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt index 78f91054b..c9c3873cd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt @@ -35,13 +35,16 @@ class FlashFragment : BaseFragment() { setHasOptionsMenu(true) activity?.setTitle(R.string.flash_screen_title) - viewModel.subtitle.observe(this) { - activity?.supportActionBar?.setSubtitle(it) - } - - viewModel.flashResult.observe(this) { success -> - binding.restartBtn.apply { - if (success && viewModel.showReboot) { + viewModel.flashState.observe(this) { + activity?.supportActionBar?.setSubtitle( + when (it) { + FlashViewModel.State.FLASHING -> R.string.flashing + FlashViewModel.State.SUCCESS -> R.string.done + FlashViewModel.State.FAILED -> R.string.failure + } + ) + if (it == FlashViewModel.State.SUCCESS && viewModel.showReboot) { + binding.restartBtn.apply { if (!this.isVisible) this.show() if (!this.isFocused) this.requestFocus() } @@ -84,7 +87,8 @@ class FlashFragment : BaseFragment() { } override fun onBackPressed(): Boolean { - if (viewModel.loading) return true + if (viewModel.flashing.value == true) + return true return super.onBackPressed() } 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 30aad2ea1..7eeeb5fbb 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 @@ -4,6 +4,7 @@ import android.view.MenuItem import androidx.databinding.Bindable import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Transformations import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R @@ -27,17 +28,18 @@ import kotlinx.coroutines.launch class FlashViewModel : BaseViewModel() { + enum class State { + FLASHING, SUCCESS, FAILED + } + + private val _flashState = MutableLiveData(State.FLASHING) + val flashState: LiveData get() = _flashState + val flashing = Transformations.map(flashState) { it == State.FLASHING } + @get:Bindable var showReboot = Info.isRooted set(value) = set(value, field, { field = it }, BR.showReboot) - private val _subtitle = MutableLiveData(R.string.flashing) - val subtitle get() = _subtitle as LiveData - - private val _flashResult = MutableLiveData() - val flashResult: LiveData - get() = _flashResult - val items = diffListOf() lateinit var args: FlashFragmentArgs @@ -87,12 +89,7 @@ class FlashViewModel : BaseViewModel() { } private fun onResult(success: Boolean) { - state = if (success) State.LOADED else State.LOADING_FAILED - when { - success -> _subtitle.postValue(R.string.done) - else -> _subtitle.postValue(R.string.failure) - } - _flashResult.value = success + _flashState.value = if (success) State.SUCCESS else State.FAILED } fun onMenuItemClicked(item: MenuItem): Boolean { diff --git a/app/src/main/res/layout/fragment_flash_md2.xml b/app/src/main/res/layout/fragment_flash_md2.xml index c4578b02d..09cb44cee 100644 --- a/app/src/main/res/layout/fragment_flash_md2.xml +++ b/app/src/main/res/layout/fragment_flash_md2.xml @@ -39,14 +39,14 @@