2020-06-08 09:21:12 +02:00
|
|
|
package com.keylesspalace.tusky.settings
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import androidx.annotation.StringRes
|
|
|
|
import androidx.preference.*
|
2020-09-02 12:27:51 +02:00
|
|
|
import com.keylesspalace.tusky.components.preference.EmojiPreference
|
|
|
|
import okhttp3.OkHttpClient
|
2020-06-08 09:21:12 +02:00
|
|
|
|
|
|
|
class PreferenceParent(
|
|
|
|
val context: Context,
|
|
|
|
val addPref: (pref: Preference) -> Unit
|
|
|
|
)
|
|
|
|
|
|
|
|
inline fun PreferenceParent.preference(builder: Preference.() -> Unit): Preference {
|
|
|
|
val pref = Preference(context)
|
|
|
|
builder(pref)
|
2020-06-14 19:58:05 +02:00
|
|
|
addPref(pref)
|
2020-06-08 09:21:12 +02:00
|
|
|
return pref
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun PreferenceParent.listPreference(builder: ListPreference.() -> Unit): ListPreference {
|
|
|
|
val pref = ListPreference(context)
|
|
|
|
builder(pref)
|
2020-06-14 19:58:05 +02:00
|
|
|
addPref(pref)
|
2020-06-08 09:21:12 +02:00
|
|
|
return pref
|
|
|
|
}
|
|
|
|
|
2020-09-02 12:27:51 +02:00
|
|
|
inline fun PreferenceParent.emojiPreference(okHttpClient: OkHttpClient, builder: EmojiPreference.() -> Unit): EmojiPreference {
|
|
|
|
val pref = EmojiPreference(context, okHttpClient)
|
2020-06-08 09:21:12 +02:00
|
|
|
builder(pref)
|
2020-06-14 19:58:05 +02:00
|
|
|
addPref(pref)
|
2020-06-08 09:21:12 +02:00
|
|
|
return pref
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun PreferenceParent.switchPreference(
|
|
|
|
builder: SwitchPreference.() -> Unit
|
|
|
|
): SwitchPreference {
|
|
|
|
val pref = SwitchPreference(context)
|
|
|
|
builder(pref)
|
2020-06-14 19:58:05 +02:00
|
|
|
addPref(pref)
|
2020-06-08 09:21:12 +02:00
|
|
|
return pref
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun PreferenceParent.editTextPreference(
|
|
|
|
builder: EditTextPreference.() -> Unit
|
|
|
|
): EditTextPreference {
|
|
|
|
val pref = EditTextPreference(context)
|
|
|
|
builder(pref)
|
2020-06-14 19:58:05 +02:00
|
|
|
addPref(pref)
|
2020-06-08 09:21:12 +02:00
|
|
|
return pref
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun PreferenceParent.checkBoxPreference(
|
|
|
|
builder: CheckBoxPreference.() -> Unit
|
|
|
|
): CheckBoxPreference {
|
|
|
|
val pref = CheckBoxPreference(context)
|
|
|
|
builder(pref)
|
2020-06-14 19:58:05 +02:00
|
|
|
addPref(pref)
|
2020-06-08 09:21:12 +02:00
|
|
|
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
|
|
|
|
}
|