Minor SettingsItem cleanup

This commit is contained in:
topjohnwu 2024-07-11 02:44:27 -07:00
parent 7173693d1b
commit 4ab7bc0d97
2 changed files with 25 additions and 24 deletions

View File

@ -14,29 +14,34 @@ import com.topjohnwu.magisk.view.MagiskDialog
sealed class BaseSettingsItem : ObservableRvItem() { sealed class BaseSettingsItem : ObservableRvItem() {
interface Handler {
fun onItemPressed(view: View, item: BaseSettingsItem, andThen: () -> Unit)
fun onItemAction(view: View, item: BaseSettingsItem)
}
override val layoutRes get() = R.layout.item_settings override val layoutRes get() = R.layout.item_settings
open val icon: Int get() = 0 open val icon: Int get() = 0
open val title: TextHolder get() = TextHolder.EMPTY open val title: TextHolder get() = TextHolder.EMPTY
@get:Bindable @get:Bindable
open val description: TextHolder get() = TextHolder.EMPTY open val description: TextHolder get() = TextHolder.EMPTY
open val showSwitch get() = false
@get:Bindable
open val isChecked get() = false
@get:Bindable @get:Bindable
var isEnabled = true var isEnabled = true
set(value) = set(value, field, { field = it }, BR.enabled, BR.description) set(value) = set(value, field, { field = it }, BR.enabled, BR.description)
open fun onToggle(view: View, handler: Handler, checked: Boolean) {}
open fun onPressed(view: View, handler: Handler) { open fun onPressed(view: View, handler: Handler) {
handler.onItemPressed(view, this) handler.onItemPressed(view, this) {
handler.onItemAction(view, this)
}
} }
open fun refresh() {} open fun refresh() {}
interface Handler { // Only for toggle
fun onItemPressed(view: View, item: BaseSettingsItem, andThen: () -> Unit = {}) open val showSwitch get() = false
fun onItemAction(view: View, item: BaseSettingsItem) @get:Bindable
} open val isChecked get() = false
fun onToggle(view: View, handler: Handler, checked: Boolean) =
set(checked, isChecked, { onPressed(view, handler) })
abstract class Value<T> : BaseSettingsItem() { abstract class Value<T> : BaseSettingsItem() {
@ -54,9 +59,6 @@ sealed class BaseSettingsItem : ObservableRvItem() {
override val showSwitch get() = true override val showSwitch get() = true
override val isChecked get() = value override val isChecked get() = value
override fun onToggle(view: View, handler: Handler, checked: Boolean) =
set(checked, value, { onPressed(view, handler) })
override fun onPressed(view: View, handler: Handler) { override fun onPressed(view: View, handler: Handler) {
// Make sure the checked state is synced // Make sure the checked state is synced
notifyPropertyChanged(BR.checked) notifyPropertyChanged(BR.checked)
@ -140,5 +142,4 @@ sealed class BaseSettingsItem : ObservableRvItem() {
abstract class Section : BaseSettingsItem() { abstract class Section : BaseSettingsItem() {
override val layoutRes = R.layout.item_settings_section override val layoutRes = R.layout.item_settings_section
} }
} }

View File

@ -87,23 +87,23 @@ class SettingsViewModel : BaseViewModel(), BaseSettingsItem.Handler {
return list return list
} }
override fun onItemPressed(view: View, item: BaseSettingsItem, andThen: () -> Unit) { override fun onItemPressed(view: View, item: BaseSettingsItem, doAction: () -> Unit) {
when (item) { when (item) {
DownloadPath -> withExternalRW(andThen) DownloadPath -> withExternalRW(doAction)
UpdateChecker -> withPostNotificationPermission(andThen) UpdateChecker -> withPostNotificationPermission(doAction)
Authentication -> AuthEvent(andThen).publish() Authentication -> AuthEvent(doAction).publish()
Theme -> SettingsFragmentDirections.actionSettingsFragmentToThemeFragment().navigate() Hide, Restore -> withInstallPermission(doAction)
DenyListConfig -> SettingsFragmentDirections.actionSettingsFragmentToDenyFragment().navigate() else -> doAction()
SystemlessHosts -> createHosts()
Hide, Restore -> withInstallPermission(andThen)
AddShortcut -> AddHomeIconEvent().publish()
LanguageSystem -> launchAppLocaleSettings(view.activity)
else -> andThen()
} }
} }
override fun onItemAction(view: View, item: BaseSettingsItem) { override fun onItemAction(view: View, item: BaseSettingsItem) {
when (item) { when (item) {
Theme -> SettingsFragmentDirections.actionSettingsFragmentToThemeFragment().navigate()
LanguageSystem -> launchAppLocaleSettings(view.activity)
AddShortcut -> AddHomeIconEvent().publish()
SystemlessHosts -> createHosts()
DenyListConfig -> SettingsFragmentDirections.actionSettingsFragmentToDenyFragment().navigate()
UpdateChannel -> openUrlIfNecessary(view) UpdateChannel -> openUrlIfNecessary(view)
is Hide -> viewModelScope.launch { HideAPK.hide(view.activity, item.value) } is Hide -> viewModelScope.launch { HideAPK.hide(view.activity, item.value) }
Restore -> viewModelScope.launch { HideAPK.restore(view.activity) } Restore -> viewModelScope.launch { HideAPK.restore(view.activity) }