Maintain separate flash screen state

This commit is contained in:
topjohnwu 2022-06-10 00:33:13 -07:00 committed by John Wu
parent e0d5d90267
commit aabc36f86b
3 changed files with 25 additions and 24 deletions

View File

@ -35,13 +35,16 @@ class FlashFragment : BaseFragment<FragmentFlashMd2Binding>() {
setHasOptionsMenu(true) setHasOptionsMenu(true)
activity?.setTitle(R.string.flash_screen_title) activity?.setTitle(R.string.flash_screen_title)
viewModel.subtitle.observe(this) { viewModel.flashState.observe(this) {
activity?.supportActionBar?.setSubtitle(it) activity?.supportActionBar?.setSubtitle(
} when (it) {
FlashViewModel.State.FLASHING -> R.string.flashing
viewModel.flashResult.observe(this) { success -> FlashViewModel.State.SUCCESS -> R.string.done
binding.restartBtn.apply { FlashViewModel.State.FAILED -> R.string.failure
if (success && viewModel.showReboot) { }
)
if (it == FlashViewModel.State.SUCCESS && viewModel.showReboot) {
binding.restartBtn.apply {
if (!this.isVisible) this.show() if (!this.isVisible) this.show()
if (!this.isFocused) this.requestFocus() if (!this.isFocused) this.requestFocus()
} }
@ -84,7 +87,8 @@ class FlashFragment : BaseFragment<FragmentFlashMd2Binding>() {
} }
override fun onBackPressed(): Boolean { override fun onBackPressed(): Boolean {
if (viewModel.loading) return true if (viewModel.flashing.value == true)
return true
return super.onBackPressed() return super.onBackPressed()
} }

View File

@ -4,6 +4,7 @@ import android.view.MenuItem
import androidx.databinding.Bindable import androidx.databinding.Bindable
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
@ -27,17 +28,18 @@ import kotlinx.coroutines.launch
class FlashViewModel : BaseViewModel() { class FlashViewModel : BaseViewModel() {
enum class State {
FLASHING, SUCCESS, FAILED
}
private val _flashState = MutableLiveData(State.FLASHING)
val flashState: LiveData<State> get() = _flashState
val flashing = Transformations.map(flashState) { it == State.FLASHING }
@get:Bindable @get:Bindable
var showReboot = Info.isRooted var showReboot = Info.isRooted
set(value) = set(value, field, { field = it }, BR.showReboot) set(value) = set(value, field, { field = it }, BR.showReboot)
private val _subtitle = MutableLiveData(R.string.flashing)
val subtitle get() = _subtitle as LiveData<Int>
private val _flashResult = MutableLiveData<Boolean>()
val flashResult: LiveData<Boolean>
get() = _flashResult
val items = diffListOf<ConsoleItem>() val items = diffListOf<ConsoleItem>()
lateinit var args: FlashFragmentArgs lateinit var args: FlashFragmentArgs
@ -87,12 +89,7 @@ class FlashViewModel : BaseViewModel() {
} }
private fun onResult(success: Boolean) { private fun onResult(success: Boolean) {
state = if (success) State.LOADED else State.LOADING_FAILED _flashState.value = if (success) State.SUCCESS else State.FAILED
when {
success -> _subtitle.postValue(R.string.done)
else -> _subtitle.postValue(R.string.failure)
}
_flashResult.value = success
} }
fun onMenuItemClicked(item: MenuItem): Boolean { fun onMenuItemClicked(item: MenuItem): Boolean {

View File

@ -39,14 +39,14 @@
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/restart_btn" android:id="@+id/restart_btn"
gone="@{!viewModel.loaded || !viewModel.showReboot}" gone="@{viewModel.flashing || !viewModel.showReboot}"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:layout_margin="@dimen/l1" android:layout_margin="@dimen/l1"
android:layout_marginBottom="@dimen/l1" android:layout_marginBottom="@dimen/l1"
android:clickable="@{!viewModel.loaded}" android:clickable="@{!viewModel.flashing}"
android:enabled="@{viewModel.loaded}" android:enabled="@{!viewModel.flashing}"
android:focusable="true" android:focusable="true"
android:onClick="@{() -> viewModel.restartPressed()}" android:onClick="@{() -> viewModel.restartPressed()}"
android:text="@string/reboot" android:text="@string/reboot"