Fixed use of RxBus for toggling policies

This commit is contained in:
Viktor De Pasquale 2019-11-01 17:58:42 +01:00
parent 253f3cf1ba
commit fe5c65d798
3 changed files with 62 additions and 22 deletions

View File

@ -3,7 +3,6 @@ package com.topjohnwu.magisk.model.entity.recycler
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.databinding.ComparableRvItem
import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback
@ -12,6 +11,7 @@ import com.topjohnwu.magisk.extensions.toggle
import com.topjohnwu.magisk.model.entity.MagiskPolicy import com.topjohnwu.magisk.model.entity.MagiskPolicy
import com.topjohnwu.magisk.model.events.PolicyEnableEvent import com.topjohnwu.magisk.model.events.PolicyEnableEvent
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent import com.topjohnwu.magisk.model.events.PolicyUpdateEvent
import com.topjohnwu.magisk.redesign.superuser.SuperuserViewModel
import com.topjohnwu.magisk.utils.KObservableField import com.topjohnwu.magisk.utils.KObservableField
import com.topjohnwu.magisk.utils.RxBus import com.topjohnwu.magisk.utils.RxBus
import com.topjohnwu.magisk.utils.rotationTo import com.topjohnwu.magisk.utils.rotationTo
@ -19,10 +19,7 @@ import com.topjohnwu.magisk.utils.setRevealed
class PolicyRvItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<PolicyRvItem>() { class PolicyRvItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<PolicyRvItem>() {
override val layoutRes: Int = when { override val layoutRes = R.layout.item_policy
Config.redesign -> R.layout.item_policy_md2
else -> R.layout.item_policy
}
val isExpanded = KObservableField(false) val isExpanded = KObservableField(false)
val isEnabled = KObservableField(item.policy == MagiskPolicy.ALLOW) val isEnabled = KObservableField(item.policy == MagiskPolicy.ALLOW)
@ -75,3 +72,49 @@ class PolicyRvItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvIte
override fun contentSameAs(other: PolicyRvItem): Boolean = itemSameAs(other) override fun contentSameAs(other: PolicyRvItem): Boolean = itemSameAs(other)
override fun itemSameAs(other: PolicyRvItem): Boolean = item.uid == other.item.uid override fun itemSameAs(other: PolicyRvItem): Boolean = item.uid == other.item.uid
} }
class PolicyItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<PolicyItem>() {
override val layoutRes = R.layout.item_policy_md2
val isExpanded = KObservableField(false)
val isEnabled = KObservableField(item.policy == MagiskPolicy.ALLOW)
val shouldNotify = KObservableField(item.notification)
val shouldLog = KObservableField(item.logging)
private val updatedPolicy
get() = item.copy(
policy = if (isEnabled.value) MagiskPolicy.ALLOW else MagiskPolicy.DENY,
notification = shouldNotify.value,
logging = shouldLog.value
)
fun toggle(viewModel: SuperuserViewModel) {
if (isExpanded.value) {
return
}
isEnabled.toggle()
viewModel.togglePolicy(this, isEnabled.value)
}
fun toggle(view: View) {
isExpanded.toggle()
view.rotationTo(if (isExpanded.value) 225 else 180)
(view.parent as ViewGroup)
.findViewById<View>(R.id.expand_layout)
.setRevealed(isExpanded.value)
}
fun toggleNotify(viewModel: SuperuserViewModel) {
shouldNotify.toggle()
viewModel.updatePolicy(PolicyUpdateEvent.Notification(updatedPolicy))
}
fun toggleLog(viewModel: SuperuserViewModel) {
shouldLog.toggle()
viewModel.updatePolicy(PolicyUpdateEvent.Log(updatedPolicy))
}
override fun contentSameAs(other: PolicyItem) = itemSameAs(other)
override fun itemSameAs(other: PolicyItem) = item.uid == other.item.uid
}

View File

@ -10,8 +10,7 @@ import com.topjohnwu.magisk.extensions.applySchedulers
import com.topjohnwu.magisk.extensions.subscribeK import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.extensions.toggle import com.topjohnwu.magisk.extensions.toggle
import com.topjohnwu.magisk.model.entity.MagiskPolicy import com.topjohnwu.magisk.model.entity.MagiskPolicy
import com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem import com.topjohnwu.magisk.model.entity.recycler.PolicyItem
import com.topjohnwu.magisk.model.events.PolicyEnableEvent
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent 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.FingerprintDialog import com.topjohnwu.magisk.model.events.dialog.FingerprintDialog
@ -22,6 +21,7 @@ import com.topjohnwu.magisk.redesign.home.itemBindingOf
import com.topjohnwu.magisk.utils.DiffObservableList import com.topjohnwu.magisk.utils.DiffObservableList
import com.topjohnwu.magisk.utils.FingerprintHelper import com.topjohnwu.magisk.utils.FingerprintHelper
import com.topjohnwu.magisk.utils.RxBus import com.topjohnwu.magisk.utils.RxBus
import com.topjohnwu.magisk.utils.currentLocale
import io.reactivex.Single import io.reactivex.Single
class SuperuserViewModel( class SuperuserViewModel(
@ -31,15 +31,12 @@ class SuperuserViewModel(
private val resources: Resources private val resources: Resources
) : CompatViewModel() { ) : CompatViewModel() {
val items = diffListOf<PolicyRvItem>() val items = diffListOf<PolicyItem>()
val itemBinding = itemBindingOf<PolicyRvItem> { val itemBinding = itemBindingOf<PolicyItem> {
it.bindExtra(BR.viewModel, this) it.bindExtra(BR.viewModel, this)
} }
init { init {
rxBus.register<PolicyEnableEvent>()
.subscribeK { togglePolicy(it.item, it.enable) }
.add()
rxBus.register<PolicyUpdateEvent>() rxBus.register<PolicyUpdateEvent>()
.subscribeK { updatePolicy(it) } .subscribeK { updatePolicy(it) }
.add() .add()
@ -50,11 +47,11 @@ class SuperuserViewModel(
override fun refresh() = db.fetchAll() override fun refresh() = db.fetchAll()
.flattenAsFlowable { it } .flattenAsFlowable { it }
.parallel() .parallel()
.map { PolicyRvItem(it, it.applicationInfo.loadIcon(packageManager)) } .map { PolicyItem(it, it.applicationInfo.loadIcon(packageManager)) }
.sequential() .sequential()
.sorted { o1, o2 -> .sorted { o1, o2 ->
compareBy<PolicyRvItem>( compareBy<PolicyItem>(
{ it.item.appName.toLowerCase() }, { it.item.appName.toLowerCase(currentLocale) },
{ it.item.packageName } { it.item.packageName }
).compare(o1, o2) ).compare(o1, o2)
} }
@ -69,7 +66,7 @@ class SuperuserViewModel(
fun safetynetPressed() = Navigation.safetynet().publish() fun safetynetPressed() = Navigation.safetynet().publish()
fun hidePressed() = Navigation.hide().publish() fun hidePressed() = Navigation.hide().publish()
fun deletePressed(item: PolicyRvItem) { fun deletePressed(item: PolicyItem) {
fun updateState() = deletePolicy(item.item) fun updateState() = deletePolicy(item.item)
.subscribeK { items.removeAll { it.itemSameAs(item) } } .subscribeK { items.removeAll { it.itemSameAs(item) } }
.add() .add()
@ -88,7 +85,7 @@ class SuperuserViewModel(
//--- //---
private fun updatePolicy(it: PolicyUpdateEvent) = when (it) { fun updatePolicy(it: PolicyUpdateEvent) = when (it) {
is PolicyUpdateEvent.Notification -> updatePolicy(it.item).map { is PolicyUpdateEvent.Notification -> updatePolicy(it.item).map {
when { when {
it.notification -> R.string.su_snack_notif_on it.notification -> R.string.su_snack_notif_on
@ -105,7 +102,7 @@ class SuperuserViewModel(
.subscribeK { SnackbarEvent(it).publish() } .subscribeK { SnackbarEvent(it).publish() }
.add() .add()
private fun togglePolicy(item: PolicyRvItem, enable: Boolean) { fun togglePolicy(item: PolicyItem, enable: Boolean) {
fun updateState() { fun updateState() {
val policy = if (enable) MagiskPolicy.ALLOW else MagiskPolicy.DENY val policy = if (enable) MagiskPolicy.ALLOW else MagiskPolicy.DENY
val app = item.item.copy(policy = policy) val app = item.item.copy(policy = policy)

View File

@ -7,7 +7,7 @@
<variable <variable
name="item" name="item"
type="com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem" /> type="com.topjohnwu.magisk.model.entity.recycler.PolicyItem" />
<variable <variable
name="viewModel" name="viewModel"
@ -21,7 +21,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:alpha="@{item.isEnabled() ? 1f : .5f}" android:alpha="@{item.isEnabled() ? 1f : .5f}"
android:onClick="@{() -> item.toggleEnabled()}" android:onClick="@{() -> item.toggle(viewModel)}"
tools:layout_marginBottom="@dimen/l1" tools:layout_marginBottom="@dimen/l1"
tools:layout_marginEnd="@dimen/l1"> tools:layout_marginEnd="@dimen/l1">
@ -93,7 +93,7 @@
android:id="@+id/bell" android:id="@+id/bell"
style="?styleIconNormal" style="?styleIconNormal"
isSelected="@{item.shouldNotify}" isSelected="@{item.shouldNotify}"
android:onClick="@{() -> item.toggleNotify()}" android:onClick="@{() -> item.toggleNotify(viewModel)}"
app:layout_constraintBottom_toTopOf="@+id/delete" app:layout_constraintBottom_toTopOf="@+id/delete"
app:layout_constraintEnd_toStartOf="@+id/bug" app:layout_constraintEnd_toStartOf="@+id/bug"
app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintHorizontal_chainStyle="packed"
@ -107,7 +107,7 @@
android:id="@+id/bug" android:id="@+id/bug"
style="?styleIconNormal" style="?styleIconNormal"
isSelected="@{item.shouldLog}" isSelected="@{item.shouldLog}"
android:onClick="@{() -> item.toggleLog()}" android:onClick="@{() -> item.toggleLog(viewModel)}"
app:layout_constraintBottom_toBottomOf="@+id/bell" app:layout_constraintBottom_toBottomOf="@+id/bell"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/bell" app:layout_constraintStart_toEndOf="@+id/bell"