diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt index 85054c749..57ec12564 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt @@ -11,6 +11,6 @@ import org.koin.dsl.module val viewModelModules = module { viewModel { MainViewModel() } viewModel { HomeViewModel(get(), get()) } - viewModel { SuperuserViewModel(get(), get(), get()) } + viewModel { SuperuserViewModel(get(), get(), get(), get()) } viewModel { HideViewModel(get(), get()) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt index e7093af63..c38c8881c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt @@ -2,9 +2,14 @@ package com.topjohnwu.magisk.model.entity.recycler import android.graphics.drawable.Drawable import com.skoumal.teanity.databinding.ComparableRvItem +import com.skoumal.teanity.extensions.addOnPropertyChangedCallback +import com.skoumal.teanity.rxbus.RxBus import com.skoumal.teanity.util.KObservableField import com.topjohnwu.magisk.R import com.topjohnwu.magisk.model.entity.Policy +import com.topjohnwu.magisk.model.events.PolicyEnableEvent +import com.topjohnwu.magisk.model.events.PolicyUpdateEvent +import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.toggle class PolicyRvItem(val item: Policy, val icon: Drawable) : ComparableRvItem() { @@ -18,6 +23,25 @@ class PolicyRvItem(val item: Policy, val icon: Drawable) : ComparableRvItem() + .subscribeK { togglePolicy(it.item, it.enable) } + .add() + rxBus.register() + .subscribeK { updatePolicy(it) } + .add() + updatePolicies() } fun updatePolicies() { Single.fromCallable { database.policyList } .flattenAsFlowable { it } - .map { PolicyRvItem(it, it.info.loadIcon(packageManager)).setListeners() } + .map { PolicyRvItem(it, it.info.loadIcon(packageManager)) } .toList() .applySchedulers() .applyViewModel(this) @@ -70,64 +80,48 @@ class SuperuserViewModel( } } - private fun PolicyRvItem.setListeners() = apply { - isEnabled.addOnPropertyChangedCallback { - it ?: return@addOnPropertyChangedCallback - - if (ignoreNext == this) { - ignoreNext = null - return@addOnPropertyChangedCallback - } - - fun updateState() { - item.policy = if (it) Policy.ALLOW else Policy.DENY - - updatePolicy(item) - .map { it.policy == Policy.ALLOW } - .subscribeK { - val textId = if (it) R.string.su_snack_grant else R.string.su_snack_deny - val text = resources.getString(textId).format(item.appName) - SnackbarEvent(text).publish() - } - .add() - } - - if (FingerprintHelper.useFingerprint()) { - withView { - FingerprintAuthDialog(this, { updateState() }, { - ignoreNext = this@setListeners - isEnabled.toggle() - }).show() - } - } else { - updateState() - } + private fun updatePolicy(it: PolicyUpdateEvent) = when (it) { + is PolicyUpdateEvent.Notification -> updatePolicy(it.item) { + val textId = if (it.logging) R.string.su_snack_notif_on else R.string.su_snack_notif_off + val text = resources.getString(textId).format(it.appName) + SnackbarEvent(text).publish() } - shouldNotify.addOnPropertyChangedCallback { - it ?: return@addOnPropertyChangedCallback - item.notification = it + is PolicyUpdateEvent.Log -> updatePolicy(it.item) { + val textId = + if (it.notification) R.string.su_snack_log_on else R.string.su_snack_log_off + val text = resources.getString(textId).format(it.appName) + SnackbarEvent(text).publish() + } + } - updatePolicy(item) - .map { it.notification } + private fun updatePolicy(item: PolicyRvItem, onSuccess: (Policy) -> Unit) = + updatePolicy(item.item) + .subscribeK { onSuccess(it) } + .add() + + private fun togglePolicy(item: PolicyRvItem, enable: Boolean) { + fun updateState() { + item.item.policy = if (enable) Policy.ALLOW else Policy.DENY + + updatePolicy(item.item) + .map { it.policy == Policy.ALLOW } .subscribeK { - val textId = if (it) R.string.su_snack_notif_on else R.string.su_snack_notif_off - val text = resources.getString(textId).format(item.appName) + val textId = if (it) R.string.su_snack_grant else R.string.su_snack_deny + val text = resources.getString(textId).format(item.item.appName) SnackbarEvent(text).publish() } .add() } - shouldLog.addOnPropertyChangedCallback { - it ?: return@addOnPropertyChangedCallback - item.logging = it - updatePolicy(item) - .map { it.logging } - .subscribeK { - val textId = if (it) R.string.su_snack_log_on else R.string.su_snack_log_off - val text = resources.getString(textId).format(item.appName) - SnackbarEvent(text).publish() - } - .add() + if (FingerprintHelper.useFingerprint()) { + withView { + FingerprintAuthDialog(this, { updateState() }, { + ignoreNext = item + item.isEnabled.toggle() + }).show() + } + } else { + updateState() } }