Do not hold resources in SuperuserViewModel

This commit is contained in:
topjohnwu 2021-04-09 01:00:26 -07:00
parent 7cb2806878
commit 0f95a7babe
4 changed files with 15 additions and 19 deletions

View File

@ -23,12 +23,10 @@ val viewModelModules = module {
viewModel { ModuleViewModel(get(), get()) } viewModel { ModuleViewModel(get(), get()) }
viewModel { SafetynetViewModel() } viewModel { SafetynetViewModel() }
viewModel { SettingsViewModel(get()) } viewModel { SettingsViewModel(get()) }
viewModel { SuperuserViewModel(get(), get()) } viewModel { SuperuserViewModel(get()) }
viewModel { ThemeViewModel() } viewModel { ThemeViewModel() }
viewModel { InstallViewModel(get()) } viewModel { InstallViewModel(get()) }
viewModel { MainViewModel() } viewModel { MainViewModel() }
// Legacy
viewModel { (args: FlashFragmentArgs) -> FlashViewModel(args) } viewModel { (args: FlashFragmentArgs) -> FlashViewModel(args) }
viewModel { SuRequestViewModel(get(), get(), get(SUTimeout), get()) } viewModel { SuRequestViewModel(get(), get(), get(SUTimeout), get()) }
} }

View File

@ -7,31 +7,32 @@ import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.BaseUIActivity import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.utils.TransitiveText import com.topjohnwu.magisk.utils.TransitiveText
import com.topjohnwu.magisk.utils.asTransitive
class SnackbarEvent private constructor( class SnackbarEvent constructor(
private val msg: TransitiveText, private val msg: TransitiveText,
private val length: Int, private val length: Int = Snackbar.LENGTH_SHORT,
private val builder: Snackbar.() -> Unit private val builder: Snackbar.() -> Unit = {}
) : ViewEvent(), ActivityExecutor { ) : ViewEvent(), ActivityExecutor {
constructor( constructor(
@StringRes res: Int, @StringRes res: Int,
length: Int = Snackbar.LENGTH_SHORT, length: Int = Snackbar.LENGTH_SHORT,
builder: Snackbar.() -> Unit = {} builder: Snackbar.() -> Unit = {}
) : this(TransitiveText.Res(res), length, builder) ) : this(res.asTransitive(), length, builder)
constructor( constructor(
message: String, msg: String,
length: Int = Snackbar.LENGTH_SHORT, length: Int = Snackbar.LENGTH_SHORT,
builder: Snackbar.() -> Unit = {} builder: Snackbar.() -> Unit = {}
) : this(TransitiveText.String(message), length, builder) ) : this(msg.asTransitive(), length, builder)
private fun snackbar( private fun snackbar(
view: View, view: View,
message: String, message: String,
length: Int = Snackbar.LENGTH_SHORT, length: Int,
builder: Snackbar.() -> Unit = {} builder: Snackbar.() -> Unit
) = Snackbar.make(view, message, length).apply(builder).show() ) = Snackbar.make(view, message, length).apply(builder).show()
override fun invoke(activity: BaseUIActivity<*, *>) { override fun invoke(activity: BaseUIActivity<*, *>) {
@ -39,5 +40,4 @@ class SnackbarEvent private constructor(
msg.getText(activity.resources).toString(), msg.getText(activity.resources).toString(),
length, builder) length, builder)
} }
} }

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.ui.superuser package com.topjohnwu.magisk.ui.superuser
import android.content.res.Resources
import androidx.databinding.ObservableArrayList import androidx.databinding.ObservableArrayList
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
@ -19,6 +18,7 @@ import com.topjohnwu.magisk.events.SnackbarEvent
import com.topjohnwu.magisk.events.dialog.BiometricEvent import com.topjohnwu.magisk.events.dialog.BiometricEvent
import com.topjohnwu.magisk.events.dialog.SuperuserRevokeDialog import com.topjohnwu.magisk.events.dialog.SuperuserRevokeDialog
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.asTransitive
import com.topjohnwu.magisk.view.TappableHeadlineItem import com.topjohnwu.magisk.view.TappableHeadlineItem
import com.topjohnwu.magisk.view.TextItem import com.topjohnwu.magisk.view.TextItem
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -27,8 +27,7 @@ import kotlinx.coroutines.withContext
import me.tatarka.bindingcollectionadapter2.collections.MergeObservableList import me.tatarka.bindingcollectionadapter2.collections.MergeObservableList
class SuperuserViewModel( class SuperuserViewModel(
private val db: PolicyDao, private val db: PolicyDao
private val resources: Resources
) : BaseViewModel(), TappableHeadlineItem.Listener { ) : BaseViewModel(), TappableHeadlineItem.Listener {
private val itemNoData = TextItem(R.string.superuser_policy_none) private val itemNoData = TextItem(R.string.superuser_policy_none)
@ -107,7 +106,7 @@ class SuperuserViewModel(
fun updatePolicy(policy: SuPolicy, isLogging: Boolean) = viewModelScope.launch { fun updatePolicy(policy: SuPolicy, isLogging: Boolean) = viewModelScope.launch {
db.update(policy) db.update(policy)
val str = when { val res = when {
isLogging -> when { isLogging -> when {
policy.logging -> R.string.su_snack_log_on policy.logging -> R.string.su_snack_log_on
else -> R.string.su_snack_log_off else -> R.string.su_snack_log_off
@ -117,7 +116,7 @@ class SuperuserViewModel(
else -> R.string.su_snack_notif_off else -> R.string.su_snack_notif_off
} }
} }
SnackbarEvent(resources.getString(str, policy.appName)).publish() SnackbarEvent(res.asTransitive(policy.appName)).publish()
} }
fun togglePolicy(item: PolicyRvItem, enable: Boolean) { fun togglePolicy(item: PolicyRvItem, enable: Boolean) {
@ -131,7 +130,7 @@ class SuperuserViewModel(
db.update(app) db.update(app)
val res = if (app.policy == SuPolicy.ALLOW) R.string.su_snack_grant val res = if (app.policy == SuPolicy.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)).publish() SnackbarEvent(res.asTransitive(item.item.appName)).publish()
} }
} }

View File

@ -37,7 +37,6 @@ sealed class TransitiveText {
companion object { companion object {
val EMPTY = String("") val EMPTY = String("")
} }
} }