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() {
interface Handler {
fun onItemPressed(view: View, item: BaseSettingsItem, andThen: () -> Unit)
fun onItemAction(view: View, item: BaseSettingsItem)
}
override val layoutRes get() = R.layout.item_settings
open val icon: Int get() = 0
open val title: TextHolder get() = TextHolder.EMPTY
@get:Bindable
open val description: TextHolder get() = TextHolder.EMPTY
open val showSwitch get() = false
@get:Bindable
open val isChecked get() = false
@get:Bindable
var isEnabled = true
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) {
handler.onItemPressed(view, this)
handler.onItemPressed(view, this) {
handler.onItemAction(view, this)
}
}
open fun refresh() {}
interface Handler {
fun onItemPressed(view: View, item: BaseSettingsItem, andThen: () -> Unit = {})
fun onItemAction(view: View, item: BaseSettingsItem)
}
// Only for toggle
open val showSwitch get() = false
@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() {
@ -54,9 +59,6 @@ sealed class BaseSettingsItem : ObservableRvItem() {
override val showSwitch get() = true
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) {
// Make sure the checked state is synced
notifyPropertyChanged(BR.checked)
@ -140,5 +142,4 @@ sealed class BaseSettingsItem : ObservableRvItem() {
abstract class Section : BaseSettingsItem() {
override val layoutRes = R.layout.item_settings_section
}
}

View File

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