Settings refactor (#1615)
* Refactor main preferences to use DSL * Refactor account preferences to use DSL * Use DSL in rest of the preference screens * Preferences cleanup * Fix preference dependencies
This commit is contained in:
parent
4188b6ea09
commit
c64df0fd1d
14 changed files with 686 additions and 637 deletions
|
|
@ -0,0 +1,83 @@
|
|||
package com.keylesspalace.tusky.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.preference.*
|
||||
import com.keylesspalace.tusky.EmojiPreference
|
||||
|
||||
class PreferenceParent(
|
||||
val context: Context,
|
||||
val addPref: (pref: Preference) -> Unit
|
||||
)
|
||||
|
||||
inline fun PreferenceParent.preference(builder: Preference.() -> Unit): Preference {
|
||||
val pref = Preference(context)
|
||||
addPref(pref)
|
||||
builder(pref)
|
||||
return pref
|
||||
}
|
||||
|
||||
inline fun PreferenceParent.listPreference(builder: ListPreference.() -> Unit): ListPreference {
|
||||
val pref = ListPreference(context)
|
||||
addPref(pref)
|
||||
builder(pref)
|
||||
return pref
|
||||
}
|
||||
|
||||
inline fun PreferenceParent.emojiPreference(builder: EmojiPreference.() -> Unit): EmojiPreference {
|
||||
val pref = EmojiPreference(context)
|
||||
addPref(pref)
|
||||
builder(pref)
|
||||
return pref
|
||||
}
|
||||
|
||||
inline fun PreferenceParent.switchPreference(
|
||||
builder: SwitchPreference.() -> Unit
|
||||
): SwitchPreference {
|
||||
val pref = SwitchPreference(context)
|
||||
addPref(pref)
|
||||
builder(pref)
|
||||
return pref
|
||||
}
|
||||
|
||||
inline fun PreferenceParent.editTextPreference(
|
||||
builder: EditTextPreference.() -> Unit
|
||||
): EditTextPreference {
|
||||
val pref = EditTextPreference(context)
|
||||
addPref(pref)
|
||||
builder(pref)
|
||||
return pref
|
||||
}
|
||||
|
||||
inline fun PreferenceParent.checkBoxPreference(
|
||||
builder: CheckBoxPreference.() -> Unit
|
||||
): CheckBoxPreference {
|
||||
val pref = CheckBoxPreference(context)
|
||||
addPref(pref)
|
||||
builder(pref)
|
||||
return pref
|
||||
}
|
||||
|
||||
inline fun PreferenceParent.preferenceCategory(
|
||||
@StringRes title: Int,
|
||||
builder: PreferenceParent.(PreferenceCategory) -> Unit
|
||||
) {
|
||||
val category = PreferenceCategory(context)
|
||||
addPref(category)
|
||||
category.setTitle(title)
|
||||
val newParent = PreferenceParent(context) { category.addPreference(it) }
|
||||
builder(newParent, category)
|
||||
}
|
||||
|
||||
inline fun PreferenceFragmentCompat.makePreferenceScreen(
|
||||
builder: PreferenceParent.() -> Unit
|
||||
): PreferenceScreen {
|
||||
val context = requireContext()
|
||||
val screen = preferenceManager.createPreferenceScreen(context)
|
||||
val parent = PreferenceParent(context) { screen.addPreference(it) }
|
||||
// For some functions (like dependencies) it's much easier for us if we attach screen first
|
||||
// and change it later
|
||||
preferenceScreen = screen
|
||||
builder(parent)
|
||||
return screen
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue