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 53a61e0e4..1906573a3 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 @@ -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() { - 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) @@ -74,4 +71,50 @@ 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() { + 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(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 + } \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/superuser/SuperuserViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/superuser/SuperuserViewModel.kt index 82ac3c0de..5308ed35d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/superuser/SuperuserViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/superuser/SuperuserViewModel.kt @@ -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() - val itemBinding = itemBindingOf { + val items = diffListOf() + val itemBinding = itemBindingOf { it.bindExtra(BR.viewModel, this) } init { - rxBus.register() - .subscribeK { togglePolicy(it.item, it.enable) } - .add() rxBus.register() .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( - { it.item.appName.toLowerCase() }, + compareBy( + { 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) diff --git a/app/src/main/res/layout/item_policy_md2.xml b/app/src/main/res/layout/item_policy_md2.xml index 2d69bec7e..215a2bac6 100644 --- a/app/src/main/res/layout/item_policy_md2.xml +++ b/app/src/main/res/layout/item_policy_md2.xml @@ -7,7 +7,7 @@ + type="com.topjohnwu.magisk.model.entity.recycler.PolicyItem" /> @@ -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"