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,56 @@
|
|||
package com.keylesspalace.tusky.settings
|
||||
|
||||
enum class AppTheme(val value: String) {
|
||||
NIGHT("night"),
|
||||
DAY("day"),
|
||||
BLACK("black"),
|
||||
AUTO("auto"),
|
||||
AUTO_SYSTEM("auto_system");
|
||||
|
||||
companion object {
|
||||
fun stringValues() = values().map { it.value }.toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
object PrefKeys {
|
||||
// Note: not all of these keys are actually used as SharedPreferences keys but we must give
|
||||
// each preference a key for it to work.
|
||||
|
||||
const val APP_THEME = "appTheme"
|
||||
const val EMOJI = "emojiCompat"
|
||||
const val FAB_HIDE = "fabHide"
|
||||
const val LANGUAGE = "language"
|
||||
const val STATUS_TEXT_SIZE = "statusTextSize"
|
||||
const val ABSOLUTE_TIME_VIEW = "absoluteTimeView"
|
||||
const val SHOW_BOT_OVERLAY = "showBotOverlay"
|
||||
const val ANIMATE_GIF_AVATARS = "animateGifAvatars"
|
||||
const val USE_BLURHASH = "useBlurhash"
|
||||
const val SHOW_NOTIFICATIONS_FILTER = "showNotificationsFilter"
|
||||
const val SHOW_CARDS_IN_TIMELINES = "showCardsInTimelines"
|
||||
const val ENABLE_SWIPE_FOR_TABS = "enableSwipeForTabs"
|
||||
|
||||
const val CUSTOM_TABS = "customTabs"
|
||||
|
||||
const val HTTP_PROXY_ENABLED = "httpProxyEnabled"
|
||||
const val HTTP_PROXY_SERVER = "httpProxyServer"
|
||||
const val HTTP_PROXY_PORT = "httpProxyPort"
|
||||
|
||||
const val DEFAULT_POST_PRIVACY = "defaultPostPrivacy"
|
||||
const val DEFAULT_MEDIA_SENSITIVITY = "defaultMediaSensitivity"
|
||||
const val MEDIA_PREVIEW_ENABLED = "mediaPreviewEnabled"
|
||||
const val ALWAYS_SHOW_SENSITIVE_MEDIA = "alwaysShowSensitiveMedia"
|
||||
const val ALWAYS_OPEN_SPOILER = "alwaysOpenSpoiler"
|
||||
|
||||
const val NOTIFICATIONS_ENABLED = "notificationsEnabled"
|
||||
const val NOTIFICATION_ALERT_LIGHT = "notificationAlertLight"
|
||||
const val NOTIFICATION_ALERT_VIBRATE = "notificationAlertVibrate"
|
||||
const val NOTIFICATION_ALERT_SOUND = "notificationAlertSound"
|
||||
const val NOTIFICATION_FILTER_POLLS = "notificationFilterPolls"
|
||||
const val NOTIFICATION_FILTER_FAVS = "notificationFilterFavourites"
|
||||
const val NOTIFICATION_FILTER_REBLOGS = "notificationFilterReblogs"
|
||||
const val NOTIFICATION_FILTER_FOLLOW_REQUESTS = "notificationFilterFollowRequests"
|
||||
const val NOTIFICATIONS_FILTER_FOLLOWS = "notificationFilterFollows"
|
||||
|
||||
const val TAB_FILTER_HOME_REPLIES = "tabFilterHomeBoosts"
|
||||
const val TAB_FILTER_HOME_BOOSTS = "tabFilterHomeReplies"
|
||||
}
|
||||
|
|
@ -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