mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-06 07:20:47 -05:00
Remove more code using RxJava
This commit is contained in:
@@ -10,6 +10,7 @@ import androidx.databinding.PropertyChangeRegistry
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.navigation.NavDirections
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.core.Info
|
||||
@@ -96,6 +97,11 @@ abstract class BaseViewModel(
|
||||
_viewEvents.postValue(this)
|
||||
}
|
||||
|
||||
fun <Event : ViewEventsWithScope> Event.publish() {
|
||||
scope = viewModelScope
|
||||
_viewEvents.postValue(this)
|
||||
}
|
||||
|
||||
fun Int.publish() {
|
||||
_viewEvents.postValue(SimpleViewEvent(this))
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class FlashViewModel(
|
||||
private val logItems = Collections.synchronizedList(mutableListOf<String>())
|
||||
|
||||
init {
|
||||
outItems.sendUpdatesTo(items) { it.map { ConsoleItem(it) } }
|
||||
outItems.sendUpdatesTo(items, viewModelScope) { it.map { ConsoleItem(it) } }
|
||||
outItems.copyNewInputInto(logItems)
|
||||
|
||||
args.dismissId.takeIf { it != -1 }?.also {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.topjohnwu.magisk.ui.install
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.databinding.FragmentInstallMd2Binding
|
||||
import com.topjohnwu.magisk.extensions.value
|
||||
@@ -21,6 +22,9 @@ class InstallFragment : BaseUIFragment<InstallViewModel, FragmentInstallMd2Bindi
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
requireActivity().setTitle(R.string.install)
|
||||
|
||||
// Allow markwon to run in viewmodel scope
|
||||
binding.releaseNotes.tag = viewModel.viewModelScope
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.topjohnwu.magisk.ui.install
|
||||
|
||||
import android.net.Uri
|
||||
import android.text.SpannableString
|
||||
import android.text.Spanned
|
||||
import android.widget.Toast
|
||||
import androidx.databinding.ObservableField
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.core.Info
|
||||
import com.topjohnwu.magisk.core.download.DownloadService
|
||||
@@ -12,7 +11,6 @@ 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.extensions.addOnPropertyChangedCallback
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.extensions.value
|
||||
import com.topjohnwu.magisk.model.entity.internal.Configuration
|
||||
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
|
||||
@@ -20,13 +18,12 @@ 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.superuser.Shell
|
||||
import io.noties.markwon.Markwon
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.core.get
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class InstallViewModel(
|
||||
stringRepo: StringRepository,
|
||||
markwon: Markwon
|
||||
stringRepo: StringRepository
|
||||
) : BaseViewModel(State.LOADED) {
|
||||
|
||||
val isRooted get() = Shell.rootAccess()
|
||||
@@ -35,8 +32,8 @@ class InstallViewModel(
|
||||
val step = ObservableField(0)
|
||||
val method = ObservableField(-1)
|
||||
val progress = ObservableField(0)
|
||||
val data = ObservableField<Uri?>(null)
|
||||
val notes = ObservableField<Spanned>(SpannableString(""))
|
||||
val data = ObservableField(null as Uri?)
|
||||
val notes = ObservableField("")
|
||||
|
||||
init {
|
||||
RemoteFileService.reset()
|
||||
@@ -50,10 +47,8 @@ class InstallViewModel(
|
||||
state = State.LOADED
|
||||
}
|
||||
}
|
||||
stringRepo.getString(Info.remote.magisk.note).map {
|
||||
markwon.toMarkdown(it)
|
||||
}.subscribeK {
|
||||
notes.value = it
|
||||
viewModelScope.launch {
|
||||
notes.value = stringRepo.getString(Info.remote.magisk.note)
|
||||
}
|
||||
method.addOnPropertyChangedCallback {
|
||||
when (it!!) {
|
||||
|
||||
@@ -50,7 +50,7 @@ class SafetynetViewModel : BaseViewModel() {
|
||||
|
||||
private fun attest() {
|
||||
currentState = LOADING
|
||||
CheckSafetyNetEvent {
|
||||
CheckSafetyNetEvent() {
|
||||
resolveResponse(it)
|
||||
}.publish()
|
||||
}
|
||||
|
||||
@@ -16,10 +16,11 @@ import com.topjohnwu.magisk.databinding.DialogSettingsAppNameBinding
|
||||
import com.topjohnwu.magisk.databinding.DialogSettingsDownloadPathBinding
|
||||
import com.topjohnwu.magisk.databinding.DialogSettingsUpdateChannelBinding
|
||||
import com.topjohnwu.magisk.extensions.get
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.model.entity.recycler.SettingsItem
|
||||
import com.topjohnwu.magisk.utils.asTransitive
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
|
||||
// --- Customization
|
||||
@@ -38,13 +39,15 @@ object Language : SettingsItem.Selector() {
|
||||
override var entries = emptyArray<String>()
|
||||
override var entryValues = emptyArray<String>()
|
||||
|
||||
init {
|
||||
availableLocales.subscribeK { (names, values) ->
|
||||
entries = names
|
||||
entryValues = values
|
||||
val selectedLocale = currentLocale.getDisplayName(currentLocale)
|
||||
value = names.indexOfFirst { it == selectedLocale }.let { if (it == -1) 0 else it }
|
||||
notifyChange(BR.selectedEntry)
|
||||
suspend fun loadLanguages(scope: CoroutineScope) {
|
||||
scope.launch {
|
||||
availableLocales().let { (names, values) ->
|
||||
entries = names
|
||||
entryValues = values
|
||||
val selectedLocale = currentLocale.getDisplayName(currentLocale)
|
||||
value = names.indexOfFirst { it == selectedLocale }.let { if (it == -1) 0 else it }
|
||||
notifyChange(BR.selectedEntry)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.topjohnwu.magisk.ui.settings
|
||||
|
||||
import android.Manifest
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.core.Const
|
||||
@@ -12,21 +12,18 @@ import com.topjohnwu.magisk.core.download.DownloadService
|
||||
import com.topjohnwu.magisk.core.utils.PatchAPK
|
||||
import com.topjohnwu.magisk.core.utils.Utils
|
||||
import com.topjohnwu.magisk.data.database.RepoDao
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.extensions.value
|
||||
import com.topjohnwu.magisk.model.entity.internal.Configuration
|
||||
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
|
||||
import com.topjohnwu.magisk.model.entity.recycler.SettingsItem
|
||||
import com.topjohnwu.magisk.model.events.RecreateEvent
|
||||
import com.topjohnwu.magisk.model.events.RxPermissionEvent
|
||||
import com.topjohnwu.magisk.model.events.dialog.BiometricDialog
|
||||
import com.topjohnwu.magisk.ui.base.BaseViewModel
|
||||
import com.topjohnwu.magisk.ui.base.adapterOf
|
||||
import com.topjohnwu.magisk.ui.base.diffListOf
|
||||
import com.topjohnwu.magisk.ui.base.itemBindingOf
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.core.get
|
||||
|
||||
class SettingsViewModel(
|
||||
@@ -48,6 +45,9 @@ class SettingsViewModel(
|
||||
// making theming a pain in the ass. Just forget about it
|
||||
list.remove(Theme)
|
||||
}
|
||||
viewModelScope.launch {
|
||||
Language.loadLanguages(this)
|
||||
}
|
||||
|
||||
// Manager
|
||||
list.addAll(listOf(
|
||||
@@ -125,8 +125,10 @@ class SettingsViewModel(
|
||||
}
|
||||
|
||||
private fun clearRepoCache() {
|
||||
Completable.fromAction { repositoryDao.clear() }
|
||||
.subscribeK { Utils.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT) }
|
||||
viewModelScope.launch {
|
||||
repositoryDao.clear()
|
||||
Utils.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createHosts() {
|
||||
@@ -136,14 +138,7 @@ class SettingsViewModel(
|
||||
}
|
||||
|
||||
private fun requireRWPermission() {
|
||||
val callback = PublishSubject.create<Boolean>()
|
||||
callback.subscribeK { if (!it) requireRWPermission() }
|
||||
RxPermissionEvent(
|
||||
listOf(
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||
), callback
|
||||
).publish()
|
||||
withExternalRW { if (!it) requireRWPermission() }
|
||||
}
|
||||
|
||||
private fun updateManager(hide: Boolean) {
|
||||
|
||||
Reference in New Issue
Block a user