Remove RxBus

This commit is contained in:
topjohnwu 2020-07-09 05:13:24 -07:00
parent 8647ba4729
commit a97d278bcd
8 changed files with 31 additions and 97 deletions

View File

@ -4,7 +4,6 @@ import android.content.Context
import android.os.Build import android.os.Build
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.topjohnwu.magisk.core.ResMgr import com.topjohnwu.magisk.core.ResMgr
import com.topjohnwu.magisk.utils.RxBus
import org.koin.core.qualifier.named import org.koin.core.qualifier.named
import org.koin.dsl.module import org.koin.dsl.module
@ -12,7 +11,6 @@ val SUTimeout = named("su_timeout")
val Protected = named("protected") val Protected = named("protected")
val applicationModule = module { val applicationModule = module {
single { RxBus() }
factory { ResMgr.resource } factory { ResMgr.resource }
factory { get<Context>().packageManager } factory { get<Context>().packageManager }
factory(Protected) { createDEContext(get()) } factory(Protected) { createDEContext(get()) }

View File

@ -21,7 +21,7 @@ val viewModelModules = module {
viewModel { HomeViewModel(get()) } viewModel { HomeViewModel(get()) }
viewModel { LogViewModel(get()) } viewModel { LogViewModel(get()) }
viewModel { ModuleViewModel(get(), get(), get()) } viewModel { ModuleViewModel(get(), get(), get()) }
viewModel { SafetynetViewModel(get()) } viewModel { SafetynetViewModel() }
viewModel { SettingsViewModel(get()) } viewModel { SettingsViewModel(get()) }
viewModel { SuperuserViewModel(get(), get(), get()) } viewModel { SuperuserViewModel(get(), get(), get()) }
viewModel { ThemeViewModel() } viewModel { ThemeViewModel() }

View File

@ -9,7 +9,6 @@ import com.topjohnwu.magisk.core.model.MagiskPolicy
import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.databinding.ComparableRvItem
import com.topjohnwu.magisk.extensions.toggle import com.topjohnwu.magisk.extensions.toggle
import com.topjohnwu.magisk.extensions.value import com.topjohnwu.magisk.extensions.value
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent
import com.topjohnwu.magisk.ui.superuser.SuperuserViewModel import com.topjohnwu.magisk.ui.superuser.SuperuserViewModel
class PolicyItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<PolicyItem>() { class PolicyItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<PolicyItem>() {
@ -42,12 +41,12 @@ class PolicyItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<
fun toggleNotify(viewModel: SuperuserViewModel) { fun toggleNotify(viewModel: SuperuserViewModel) {
shouldNotify.toggle() shouldNotify.toggle()
viewModel.updatePolicy(PolicyUpdateEvent.Notification(updatedPolicy)) viewModel.updatePolicy(updatedPolicy, isLogging = false)
} }
fun toggleLog(viewModel: SuperuserViewModel) { fun toggleLog(viewModel: SuperuserViewModel) {
shouldLog.toggle() shouldLog.toggle()
viewModel.updatePolicy(PolicyUpdateEvent.Log(updatedPolicy)) viewModel.updatePolicy(updatedPolicy, isLogging = true)
} }
override fun onBindingBound(binding: ViewDataBinding) { override fun onBindingBound(binding: ViewDataBinding) {

View File

@ -1,15 +0,0 @@
package com.topjohnwu.magisk.model.events
import com.topjohnwu.magisk.core.model.MagiskPolicy
import com.topjohnwu.magisk.utils.RxBus
import org.json.JSONObject
sealed class PolicyUpdateEvent(val item: MagiskPolicy) : RxBus.Event {
class Notification(item: MagiskPolicy) : PolicyUpdateEvent(item)
class Log(item: MagiskPolicy) : PolicyUpdateEvent(item)
}
data class SafetyNetResult(
val response: JSONObject? = null,
val dismiss: Boolean = false
) : RxBus.Event

View File

@ -13,7 +13,7 @@ import com.topjohnwu.magisk.extensions.DynamicClassLoader
import com.topjohnwu.magisk.extensions.OnErrorListener import com.topjohnwu.magisk.extensions.OnErrorListener
import com.topjohnwu.magisk.extensions.subscribeK import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.extensions.writeTo import com.topjohnwu.magisk.extensions.writeTo
import com.topjohnwu.magisk.utils.RxBus import com.topjohnwu.magisk.ui.safetynet.SafetyNetResult
import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.MagiskDialog
import com.topjohnwu.magisk.view.MarkDownWindow import com.topjohnwu.magisk.view.MarkDownWindow
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
@ -38,10 +38,11 @@ abstract class ViewEvent {
var handled = false var handled = false
} }
class UpdateSafetyNetEvent : ViewEvent(), ContextExecutor, KoinComponent, SafetyNetHelper.Callback { class CheckSafetyNetEvent(
private val callback: (SafetyNetResult) -> Unit = {}
) : ViewEvent(), ContextExecutor, KoinComponent, SafetyNetHelper.Callback {
private val magiskRepo by inject<MagiskRepository>() private val magiskRepo by inject<MagiskRepository>()
private val rxBus by inject<RxBus>()
private lateinit var apk: File private lateinit var apk: File
private lateinit var dex: File private lateinit var dex: File
@ -93,7 +94,7 @@ class UpdateSafetyNetEvent : ViewEvent(), ContextExecutor, KoinComponent, Safety
.map { it.byteStream().writeTo(apk) } .map { it.byteStream().writeTo(apk) }
.subscribeK { attest(context) { .subscribeK { attest(context) {
Timber.e(it) Timber.e(it)
rxBus.post(SafetyNetResult()) callback(SafetyNetResult())
} } } }
if (!askUser) { if (!askUser) {
@ -111,13 +112,13 @@ class UpdateSafetyNetEvent : ViewEvent(), ContextExecutor, KoinComponent, Safety
} }
.applyButton(MagiskDialog.ButtonType.NEGATIVE) { .applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel titleRes = android.R.string.cancel
onClick { rxBus.post(SafetyNetResult(dismiss = true)) } onClick { callback(SafetyNetResult(dismiss = true)) }
} }
.reveal() .reveal()
} }
override fun onResponse(response: JSONObject?) { override fun onResponse(response: JSONObject?) {
rxBus.post(SafetyNetResult(response)) callback(SafetyNetResult(response))
} }
} }

View File

@ -4,22 +4,22 @@ import androidx.databinding.Bindable
import androidx.databinding.ObservableField import androidx.databinding.ObservableField
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.extensions.value import com.topjohnwu.magisk.extensions.value
import com.topjohnwu.magisk.model.events.SafetyNetResult import com.topjohnwu.magisk.model.events.CheckSafetyNetEvent
import com.topjohnwu.magisk.model.events.UpdateSafetyNetEvent
import com.topjohnwu.magisk.ui.base.BaseViewModel import com.topjohnwu.magisk.ui.base.BaseViewModel
import com.topjohnwu.magisk.ui.safetynet.SafetyNetState.* import com.topjohnwu.magisk.ui.safetynet.SafetyNetState.*
import com.topjohnwu.magisk.utils.RxBus
import org.json.JSONObject import org.json.JSONObject
enum class SafetyNetState { enum class SafetyNetState {
LOADING, PASS, FAILED, IDLE LOADING, PASS, FAILED, IDLE
} }
class SafetynetViewModel( data class SafetyNetResult(
rxBus: RxBus val response: JSONObject? = null,
) : BaseViewModel() { val dismiss: Boolean = false
)
class SafetynetViewModel : BaseViewModel() {
private var currentState = IDLE private var currentState = IDLE
set(value) { set(value) {
@ -36,10 +36,6 @@ class SafetynetViewModel(
val isSuccess @Bindable get() = currentState == PASS val isSuccess @Bindable get() = currentState == PASS
init { init {
rxBus.register<SafetyNetResult>()
.subscribeK { resolveResponse(it) }
.add()
cachedResult?.also { cachedResult?.also {
resolveResponse(SafetyNetResult(it)) resolveResponse(SafetyNetResult(it))
} ?: attest() } ?: attest()
@ -54,7 +50,9 @@ class SafetynetViewModel(
private fun attest() { private fun attest() {
currentState = LOADING currentState = LOADING
UpdateSafetyNetEvent().publish() CheckSafetyNetEvent {
resolveResponse(it)
}.publish()
} }
fun reset() = attest() fun reset() = attest()

View File

@ -15,7 +15,6 @@ import com.topjohnwu.magisk.extensions.toggle
import com.topjohnwu.magisk.model.entity.recycler.PolicyItem import com.topjohnwu.magisk.model.entity.recycler.PolicyItem
import com.topjohnwu.magisk.model.entity.recycler.TappableHeadlineItem import com.topjohnwu.magisk.model.entity.recycler.TappableHeadlineItem
import com.topjohnwu.magisk.model.entity.recycler.TextItem import com.topjohnwu.magisk.model.entity.recycler.TextItem
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent
import com.topjohnwu.magisk.model.events.SnackbarEvent import com.topjohnwu.magisk.model.events.SnackbarEvent
import com.topjohnwu.magisk.model.events.dialog.BiometricDialog import com.topjohnwu.magisk.model.events.dialog.BiometricDialog
import com.topjohnwu.magisk.model.events.dialog.SuperuserRevokeDialog import com.topjohnwu.magisk.model.events.dialog.SuperuserRevokeDialog
@ -110,24 +109,19 @@ class SuperuserViewModel(
//--- //---
fun updatePolicy(it: PolicyUpdateEvent) = viewModelScope.launch { fun updatePolicy(policy: MagiskPolicy, isLogging: Boolean) = viewModelScope.launch {
val snackStr = when (it) { db.update(policy)
is PolicyUpdateEvent.Notification -> { val str = when {
updatePolicy(it.item) isLogging -> when {
when { policy.logging -> R.string.su_snack_log_on
it.item.notification -> R.string.su_snack_notif_on else -> R.string.su_snack_log_off
else -> R.string.su_snack_notif_off
}
} }
is PolicyUpdateEvent.Log -> { else -> when {
updatePolicy(it.item) policy.notification -> R.string.su_snack_notif_on
when { else -> R.string.su_snack_notif_off
it.item.logging -> R.string.su_snack_log_on
else -> R.string.su_snack_log_off
}
} }
} }
SnackbarEvent(resources.getString(snackStr, it.item.appName)).publish() SnackbarEvent(resources.getString(str, policy.appName)).publish()
} }
fun togglePolicy(item: PolicyItem, enable: Boolean) { fun togglePolicy(item: PolicyItem, enable: Boolean) {
@ -136,7 +130,7 @@ class SuperuserViewModel(
val app = item.item.copy(policy = policy) val app = item.item.copy(policy = policy)
viewModelScope.launch { viewModelScope.launch {
updatePolicy(app) db.update(app)
val res = if (app.policy == MagiskPolicy.ALLOW) R.string.su_snack_grant val res = if (app.policy == MagiskPolicy.ALLOW) R.string.su_snack_grant
else R.string.su_snack_deny else R.string.su_snack_deny
SnackbarEvent(resources.getString(res).format(item.item.appName)) SnackbarEvent(resources.getString(res).format(item.item.appName))
@ -152,9 +146,4 @@ class SuperuserViewModel(
updateState() updateState()
} }
} }
//---
private suspend fun updatePolicy(policy: MagiskPolicy) = db.update(policy)
} }

View File

@ -1,36 +0,0 @@
package com.topjohnwu.magisk.utils
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
class RxBus {
private val _bus = PublishSubject.create<Event>()
val bus: Observable<Event> get() = _bus
fun post(event: Event) {
_bus.onNext(event)
}
fun post(event: Int) {
_bus.onNext(SimpleEvent(event))
}
inline fun <reified T : Event> register(noinline predicate: (T) -> Boolean = { true }): Observable<T> {
return bus
.ofType(T::class.java)
.filter(predicate)
}
fun register(eventId: Int): Observable<Int> {
return bus
.ofType(SimpleEvent::class.java)
.map { it.eventId }
.filter { it == eventId }
}
interface Event
private class SimpleEvent(val eventId: Int) : Event
}