More efficient databinding

This commit is contained in:
topjohnwu
2020-07-12 03:17:50 -07:00
parent b41b2283f4
commit 2c12fe6eb2
26 changed files with 386 additions and 276 deletions
@@ -4,7 +4,6 @@ import android.content.Intent
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.FragmentInstallMd2Binding
import com.topjohnwu.magisk.ktx.value
import com.topjohnwu.magisk.model.events.RequestFileEvent
import com.topjohnwu.magisk.ui.base.BaseUIFragment
import org.koin.androidx.viewmodel.ext.android.viewModel
@@ -16,7 +15,7 @@ class InstallFragment : BaseUIFragment<InstallViewModel, FragmentInstallMd2Bindi
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
viewModel.data.value = RequestFileEvent.resolve(requestCode, resultCode, data)
viewModel.data = RequestFileEvent.resolve(requestCode, resultCode, data)
}
override fun onStart() {
@@ -2,21 +2,21 @@ package com.topjohnwu.magisk.ui.install
import android.net.Uri
import android.widget.Toast
import androidx.databinding.ObservableField
import androidx.databinding.Bindable
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.core.utils.Utils
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.ktx.addOnPropertyChangedCallback
import com.topjohnwu.magisk.ktx.value
import com.topjohnwu.magisk.model.entity.internal.Configuration
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
import com.topjohnwu.magisk.model.events.RequestFileEvent
import com.topjohnwu.magisk.model.events.dialog.SecondSlotWarningDialog
import com.topjohnwu.magisk.ui.base.BaseViewModel
import com.topjohnwu.magisk.utils.observable
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.launch
import org.koin.core.get
@@ -29,11 +29,26 @@ class InstallViewModel(
val isRooted get() = Shell.rootAccess()
val isAB get() = Info.isAB
val step = ObservableField(0)
val method = ObservableField(-1)
val progress = ObservableField(0)
val data = ObservableField(null as Uri?)
val notes = ObservableField("")
@get:Bindable
var step by observable(0, BR.step)
@get:Bindable
var method by observable(-1, BR.method) {
when (it) {
R.id.method_patch -> {
Utils.toast(R.string.patch_file_msg, Toast.LENGTH_LONG)
RequestFileEvent().publish()
}
R.id.method_inactive_slot -> {
SecondSlotWarningDialog().publish()
}
}
}
@get:Bindable
var progress by observable(0, BR.progress)
@get:Bindable
var data by observable(null as Uri?, BR.data)
@get:Bindable
var notes by observable("", BR.notes)
init {
RemoteFileService.reset()
@@ -42,29 +57,18 @@ class InstallViewModel(
if (subject !is DownloadSubject.Magisk) {
return@observeForever
}
this.progress.value = progress.times(100).roundToInt()
if (this.progress.value >= 100) {
this.progress = progress.times(100).roundToInt()
if (this.progress >= 100) {
state = State.LOADED
}
}
viewModelScope.launch {
notes.value = stringRepo.getString(Info.remote.magisk.note)
}
method.addOnPropertyChangedCallback {
when (it!!) {
R.id.method_patch -> {
Utils.toast(R.string.patch_file_msg, Toast.LENGTH_LONG)
RequestFileEvent().publish()
}
R.id.method_inactive_slot -> {
SecondSlotWarningDialog().publish()
}
}
notes = stringRepo.getString(Info.remote.magisk.note)
}
}
fun step(nextStep: Int) {
step.value = nextStep
step = nextStep
}
fun install() = DownloadService(get()) {
@@ -73,9 +77,9 @@ class InstallViewModel(
// ---
private fun resolveConfiguration() = when (method.value) {
private fun resolveConfiguration() = when (method) {
R.id.method_download -> Configuration.Download
R.id.method_patch -> Configuration.Patch(data.value!!)
R.id.method_patch -> Configuration.Patch(data!!)
R.id.method_direct -> Configuration.Flash.Primary
R.id.method_inactive_slot -> Configuration.Flash.Secondary
else -> throw IllegalArgumentException("Unknown value")