Fixed use of RxBus for toggling policies
This commit is contained in:
parent
253f3cf1ba
commit
fe5c65d798
@ -3,7 +3,6 @@ package com.topjohnwu.magisk.model.entity.recycler
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.topjohnwu.magisk.Config
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.databinding.ComparableRvItem
|
||||
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.events.PolicyEnableEvent
|
||||
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.RxBus
|
||||
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>() {
|
||||
|
||||
override val layoutRes: Int = when {
|
||||
Config.redesign -> R.layout.item_policy_md2
|
||||
else -> R.layout.item_policy
|
||||
}
|
||||
override val layoutRes = R.layout.item_policy
|
||||
|
||||
val isExpanded = KObservableField(false)
|
||||
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 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
|
||||
|
||||
}
|
@ -10,8 +10,7 @@ import com.topjohnwu.magisk.extensions.applySchedulers
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.extensions.toggle
|
||||
import com.topjohnwu.magisk.model.entity.MagiskPolicy
|
||||
import com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem
|
||||
import com.topjohnwu.magisk.model.events.PolicyEnableEvent
|
||||
import com.topjohnwu.magisk.model.entity.recycler.PolicyItem
|
||||
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent
|
||||
import com.topjohnwu.magisk.model.events.SnackbarEvent
|
||||
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.FingerprintHelper
|
||||
import com.topjohnwu.magisk.utils.RxBus
|
||||
import com.topjohnwu.magisk.utils.currentLocale
|
||||
import io.reactivex.Single
|
||||
|
||||
class SuperuserViewModel(
|
||||
@ -31,15 +31,12 @@ class SuperuserViewModel(
|
||||
private val resources: Resources
|
||||
) : CompatViewModel() {
|
||||
|
||||
val items = diffListOf<PolicyRvItem>()
|
||||
val itemBinding = itemBindingOf<PolicyRvItem> {
|
||||
val items = diffListOf<PolicyItem>()
|
||||
val itemBinding = itemBindingOf<PolicyItem> {
|
||||
it.bindExtra(BR.viewModel, this)
|
||||
}
|
||||
|
||||
init {
|
||||
rxBus.register<PolicyEnableEvent>()
|
||||
.subscribeK { togglePolicy(it.item, it.enable) }
|
||||
.add()
|
||||
rxBus.register<PolicyUpdateEvent>()
|
||||
.subscribeK { updatePolicy(it) }
|
||||
.add()
|
||||
@ -50,11 +47,11 @@ class SuperuserViewModel(
|
||||
override fun refresh() = db.fetchAll()
|
||||
.flattenAsFlowable { it }
|
||||
.parallel()
|
||||
.map { PolicyRvItem(it, it.applicationInfo.loadIcon(packageManager)) }
|
||||
.map { PolicyItem(it, it.applicationInfo.loadIcon(packageManager)) }
|
||||
.sequential()
|
||||
.sorted { o1, o2 ->
|
||||
compareBy<PolicyRvItem>(
|
||||
{ it.item.appName.toLowerCase() },
|
||||
compareBy<PolicyItem>(
|
||||
{ it.item.appName.toLowerCase(currentLocale) },
|
||||
{ it.item.packageName }
|
||||
).compare(o1, o2)
|
||||
}
|
||||
@ -69,7 +66,7 @@ class SuperuserViewModel(
|
||||
fun safetynetPressed() = Navigation.safetynet().publish()
|
||||
fun hidePressed() = Navigation.hide().publish()
|
||||
|
||||
fun deletePressed(item: PolicyRvItem) {
|
||||
fun deletePressed(item: PolicyItem) {
|
||||
fun updateState() = deletePolicy(item.item)
|
||||
.subscribeK { items.removeAll { it.itemSameAs(item) } }
|
||||
.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 {
|
||||
when {
|
||||
it.notification -> R.string.su_snack_notif_on
|
||||
@ -105,7 +102,7 @@ class SuperuserViewModel(
|
||||
.subscribeK { SnackbarEvent(it).publish() }
|
||||
.add()
|
||||
|
||||
private fun togglePolicy(item: PolicyRvItem, enable: Boolean) {
|
||||
fun togglePolicy(item: PolicyItem, enable: Boolean) {
|
||||
fun updateState() {
|
||||
val policy = if (enable) MagiskPolicy.ALLOW else MagiskPolicy.DENY
|
||||
val app = item.item.copy(policy = policy)
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
<variable
|
||||
name="item"
|
||||
type="com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem" />
|
||||
type="com.topjohnwu.magisk.model.entity.recycler.PolicyItem" />
|
||||
|
||||
<variable
|
||||
name="viewModel"
|
||||
@ -21,7 +21,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:alpha="@{item.isEnabled() ? 1f : .5f}"
|
||||
android:onClick="@{() -> item.toggleEnabled()}"
|
||||
android:onClick="@{() -> item.toggle(viewModel)}"
|
||||
tools:layout_marginBottom="@dimen/l1"
|
||||
tools:layout_marginEnd="@dimen/l1">
|
||||
|
||||
@ -93,7 +93,7 @@
|
||||
android:id="@+id/bell"
|
||||
style="?styleIconNormal"
|
||||
isSelected="@{item.shouldNotify}"
|
||||
android:onClick="@{() -> item.toggleNotify()}"
|
||||
android:onClick="@{() -> item.toggleNotify(viewModel)}"
|
||||
app:layout_constraintBottom_toTopOf="@+id/delete"
|
||||
app:layout_constraintEnd_toStartOf="@+id/bug"
|
||||
app:layout_constraintHorizontal_chainStyle="packed"
|
||||
@ -107,7 +107,7 @@
|
||||
android:id="@+id/bug"
|
||||
style="?styleIconNormal"
|
||||
isSelected="@{item.shouldLog}"
|
||||
android:onClick="@{() -> item.toggleLog()}"
|
||||
android:onClick="@{() -> item.toggleLog(viewModel)}"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/bell"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/bell"
|
||||
|
Loading…
Reference in New Issue
Block a user