WIP prefs: account prefs

This commit is contained in:
charlag 2022-06-08 17:59:51 +02:00
parent f8dd5c0e9d
commit 942afc8503
No known key found for this signature in database
GPG Key ID: 5B96E7C76F0CA558
4 changed files with 212 additions and 460 deletions

View File

@ -16,28 +16,49 @@
package com.keylesspalace.tusky.components.preference
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ScrollView
import androidx.annotation.DrawableRes
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.AccountListActivity
import com.keylesspalace.tusky.BaseActivity
import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.FiltersActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.TabPreferenceActivity
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
import com.keylesspalace.tusky.components.instancemute.InstanceListActivity
import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.entity.Filter
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.settings.PreferenceOption
import com.keylesspalace.tusky.settings.clickPreference
import com.keylesspalace.tusky.settings.listPreference
import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.preferenceCategory
import com.keylesspalace.tusky.settings.switchPreference
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
class AccountPreferencesFragment : Fragment(), Injectable {
@Inject
lateinit var accountManager: AccountManager
@ -47,207 +68,141 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
@Inject
lateinit var eventHub: EventHub
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = requireContext()
// makePreferenceScreen {
// preference {
// setTitle(R.string.pref_title_edit_notification_settings)
// icon = IconicsDrawable(context, GoogleMaterial.Icon.gmd_notifications).apply {
// sizeRes = R.dimen.preference_icon_size
// colorInt = ThemeUtils.getColor(context, R.attr.iconColor)
// }
// setOnPreferenceClickListener {
// openNotificationPrefs()
// true
// }
// }
//
// preference {
// setTitle(R.string.title_tab_preferences)
// setIcon(R.drawable.ic_tabs)
// setOnPreferenceClickListener {
// val intent = Intent(context, TabPreferenceActivity::class.java)
// activity?.startActivity(intent)
// activity?.overridePendingTransition(
// R.anim.slide_from_right,
// R.anim.slide_to_left
// )
// true
// }
// }
//
// preference {
// setTitle(R.string.action_view_mutes)
// setIcon(R.drawable.ic_mute_24dp)
// setOnPreferenceClickListener {
// val intent = Intent(context, AccountListActivity::class.java)
// intent.putExtra("type", AccountListActivity.Type.MUTES)
// activity?.startActivity(intent)
// activity?.overridePendingTransition(
// R.anim.slide_from_right,
// R.anim.slide_to_left
// )
// true
// }
// }
//
// preference {
// setTitle(R.string.action_view_blocks)
// icon = IconicsDrawable(context, GoogleMaterial.Icon.gmd_block).apply {
// sizeRes = R.dimen.preference_icon_size
// colorInt = ThemeUtils.getColor(context, R.attr.iconColor)
// }
// setOnPreferenceClickListener {
// val intent = Intent(context, AccountListActivity::class.java)
// intent.putExtra("type", AccountListActivity.Type.BLOCKS)
// activity?.startActivity(intent)
// activity?.overridePendingTransition(
// R.anim.slide_from_right,
// R.anim.slide_to_left
// )
// true
// }
// }
//
// preference {
// setTitle(R.string.title_domain_mutes)
// setIcon(R.drawable.ic_mute_24dp)
// setOnPreferenceClickListener {
// val intent = Intent(context, InstanceListActivity::class.java)
// activity?.startActivity(intent)
// activity?.overridePendingTransition(
// R.anim.slide_from_right,
// R.anim.slide_to_left
// )
// true
// }
// }
//
// preferenceCategory(R.string.pref_publishing) {
// listPreference {
// setTitle(R.string.pref_default_post_privacy)
// setEntries(R.array.post_privacy_names)
// setEntryValues(R.array.post_privacy_values)
// key = PrefKeys.DEFAULT_POST_PRIVACY
// setSummaryProvider { entry }
// val visibility = accountManager.activeAccount?.defaultPostPrivacy
// ?: Status.Visibility.PUBLIC
// value = visibility.serverString()
// setIcon(getIconForVisibility(visibility))
// setOnPreferenceChangeListener { _, newValue ->
// setIcon(getIconForVisibility(Status.Visibility.byString(newValue as String)))
// syncWithServer(visibility = newValue)
// eventHub.dispatch(PreferenceChangedEvent(key))
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_default_media_sensitivity)
// setIcon(R.drawable.ic_eye_24dp)
// key = PrefKeys.DEFAULT_MEDIA_SENSITIVITY
// isSingleLineTitle = false
// val sensitivity = accountManager.activeAccount?.defaultMediaSensitivity
// ?: false
// setDefaultValue(sensitivity)
// setIcon(getIconForSensitivity(sensitivity))
// setOnPreferenceChangeListener { _, newValue ->
// setIcon(getIconForSensitivity(newValue as Boolean))
// syncWithServer(sensitive = newValue)
// eventHub.dispatch(PreferenceChangedEvent(key))
// true
// }
// }
// }
//
// preferenceCategory(R.string.pref_title_timelines) {
// switchPreference {
// key = PrefKeys.MEDIA_PREVIEW_ENABLED
// setTitle(R.string.pref_title_show_media_preview)
// isSingleLineTitle = false
// isChecked = accountManager.activeAccount?.mediaPreviewEnabled ?: true
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.mediaPreviewEnabled = newValue as Boolean }
// eventHub.dispatch(PreferenceChangedEvent(key))
// true
// }
// }
//
// switchPreference {
// key = PrefKeys.ALWAYS_SHOW_SENSITIVE_MEDIA
// setTitle(R.string.pref_title_alway_show_sensitive_media)
// isSingleLineTitle = false
// isChecked = accountManager.activeAccount?.alwaysShowSensitiveMedia ?: false
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.alwaysShowSensitiveMedia = newValue as Boolean }
// eventHub.dispatch(PreferenceChangedEvent(key))
// true
// }
// }
//
// switchPreference {
// key = PrefKeys.ALWAYS_OPEN_SPOILER
// setTitle(R.string.pref_title_alway_open_spoiler)
// isSingleLineTitle = false
// isChecked = accountManager.activeAccount?.alwaysOpenSpoiler ?: false
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.alwaysOpenSpoiler = newValue as Boolean }
// eventHub.dispatch(PreferenceChangedEvent(key))
// true
// }
// }
// }
//
// preferenceCategory(R.string.pref_title_timeline_filters) {
// preference {
// setTitle(R.string.pref_title_public_filter_keywords)
// setOnPreferenceClickListener {
// launchFilterActivity(
// Filter.PUBLIC,
// R.string.pref_title_public_filter_keywords
// )
// true
// }
// }
//
// preference {
// setTitle(R.string.title_notifications)
// setOnPreferenceClickListener {
// launchFilterActivity(Filter.NOTIFICATIONS, R.string.title_notifications)
// true
// }
// }
//
// preference {
// setTitle(R.string.title_home)
// setOnPreferenceClickListener {
// launchFilterActivity(Filter.HOME, R.string.title_home)
// true
// }
// }
//
// preference {
// setTitle(R.string.pref_title_thread_filter_keywords)
// setOnPreferenceClickListener {
// launchFilterActivity(
// Filter.THREAD,
// R.string.pref_title_thread_filter_keywords
// )
// true
// }
// }
//
// preference {
// setTitle(R.string.title_accounts)
// setOnPreferenceClickListener {
// launchFilterActivity(Filter.ACCOUNT, R.string.title_accounts)
// true
// }
// }
// }
// }
private var updateTrigger: (() -> Unit)? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val viewRoot = ScrollView(inflater.context)
val rootLayout = LinearLayout(inflater.context).apply {
orientation = LinearLayout.VERTICAL
viewRoot.addView(this)
}
updateTrigger = makePreferenceScreen(rootLayout) {
clickPreference(
getString(R.string.pref_title_edit_notification_settings),
{ makeIcon(GoogleMaterial.Icon.gmd_notifications) }
) {
openNotificationPrefs()
}
clickPreference(
getString(R.string.title_tab_preferences),
{ makeIcon(R.drawable.ic_tabs) }
) {
val activity = requireActivity() as BaseActivity
val intent = Intent(context, TabPreferenceActivity::class.java)
activity.startActivityWithSlideInAnimation(intent)
}
clickPreference(
getString(R.string.action_view_mutes),
{ makeIcon(R.drawable.ic_mute_24dp) }
) {
val activity = requireActivity() as BaseActivity
val intent = AccountListActivity.newIntent(context, AccountListActivity.Type.MUTES)
activity.startActivityWithSlideInAnimation(intent)
}
clickPreference(
getString(R.string.action_view_blocks),
{ makeIcon(GoogleMaterial.Icon.gmd_block) }
) {
val activity = requireActivity() as BaseActivity
val intent = AccountListActivity.newIntent(context, AccountListActivity.Type.BLOCKS)
activity.startActivityWithSlideInAnimation(intent)
}
clickPreference(
getString(R.string.title_domain_mutes),
{ makeIcon(R.drawable.ic_mute_24dp) }
) {
val activity = requireActivity() as BaseActivity
val intent = Intent(context, InstanceListActivity::class.java)
activity.startActivityWithSlideInAnimation(intent)
}
preferenceCategory(R.string.pref_publishing) {
val privacyOptions = listOf(
"public" to R.string.post_privacy_public,
"unlisted" to R.string.post_privacy_unlisted,
"private" to R.string.post_privacy_followers_only
).map(::PreferenceOption)
listPreference(
getString(R.string.pref_default_post_privacy),
privacyOptions,
{ activeAccount.defaultPostPrivacy.serverString() },
{ getIconForVisibility(activeAccount.defaultPostPrivacy) }
) { newValue ->
syncWithServer(visibility = newValue)
eventHub.dispatch(PreferenceChangedEvent(PrefKeys.DEFAULT_POST_PRIVACY))
}
switchPreference(
getString(R.string.pref_default_media_sensitivity),
{ activeAccount.defaultMediaSensitivity },
{ getIconForSensitivity(activeAccount.defaultMediaSensitivity) }
) {
updateAccount { account -> account.mediaPreviewEnabled = it }
eventHub.dispatch(PreferenceChangedEvent(PrefKeys.DEFAULT_MEDIA_SENSITIVITY))
}
}
preferenceCategory(R.string.pref_title_timelines) {
switchPreference(
getString(R.string.pref_title_timelines),
{ activeAccount.mediaPreviewEnabled },
) {
updateAccount { account -> account.mediaPreviewEnabled = it }
eventHub.dispatch(PreferenceChangedEvent(PrefKeys.MEDIA_PREVIEW_ENABLED))
}
switchPreference(
getString(R.string.pref_title_alway_show_sensitive_media),
{ activeAccount.alwaysShowSensitiveMedia }
) {
updateAccount { account -> account.alwaysShowSensitiveMedia = it }
eventHub.dispatch(PreferenceChangedEvent(PrefKeys.ALWAYS_SHOW_SENSITIVE_MEDIA))
}
switchPreference(
getString(R.string.pref_title_alway_open_spoiler),
{ activeAccount.alwaysOpenSpoiler }
) {
updateAccount { account -> account.alwaysOpenSpoiler = it }
eventHub.dispatch(PreferenceChangedEvent(PrefKeys.ALWAYS_OPEN_SPOILER))
}
}
preferenceCategory(R.string.pref_title_timeline_filters) {
clickPreference(getString(R.string.pref_title_public_filter_keywords)) {
launchFilterActivity(Filter.PUBLIC, R.string.pref_title_public_filter_keywords)
}
clickPreference(getString(R.string.title_notifications)) {
launchFilterActivity(Filter.NOTIFICATIONS, R.string.title_notifications)
}
clickPreference(getString(R.string.pref_title_thread_filter_keywords)) {
launchFilterActivity(Filter.THREAD, R.string.pref_title_thread_filter_keywords)
}
clickPreference(getString(R.string.title_accounts)) {
launchFilterActivity(Filter.ACCOUNT, R.string.title_accounts)
}
}
}
return viewRoot
}
private val activeAccount: AccountEntity
get() = accountManager.activeAccount!!
private fun openNotificationPrefs() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intent = Intent()
@ -256,7 +211,8 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
startActivity(intent)
} else {
activity?.let {
val intent = PreferencesActivity.newIntent(it, PreferencesActivity.NOTIFICATION_PREFERENCES)
val intent =
PreferencesActivity.newIntent(it, PreferencesActivity.NOTIFICATION_PREFERENCES)
it.startActivity(intent)
it.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
}
@ -267,6 +223,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
accountManager.activeAccount?.let { account ->
changer(account)
accountManager.saveAccount(account)
updateTrigger?.invoke()
}
}
@ -276,12 +233,10 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
override fun onResponse(call: Call<Account>, response: Response<Account>) {
val account = response.body()
if (response.isSuccessful && account != null) {
accountManager.activeAccount?.let {
updateAccount {
it.defaultPostPrivacy = account.source?.privacy
?: Status.Visibility.PUBLIC
it.defaultMediaSensitivity = account.source?.sensitive ?: false
accountManager.saveAccount(it)
}
} else {
Log.e("AccountPreferences", "failed updating settings on server")
@ -304,24 +259,24 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
}
}
private fun getIconForVisibility(visibility: Status.Visibility): Drawable {
@DrawableRes
private fun getIconForVisibility(visibility: Status.Visibility): Int {
return when (visibility) {
val res = when (visibility) {
Status.Visibility.PRIVATE -> R.drawable.ic_lock_outline_24dp
Status.Visibility.UNLISTED -> R.drawable.ic_lock_open_24dp
else -> R.drawable.ic_public_24dp
}
return makeIcon(res)
}
private fun getIconForSensitivity(sensitive: Boolean): Drawable {
@DrawableRes
private fun getIconForSensitivity(sensitive: Boolean): Int {
return if (sensitive) {
val res = if (sensitive) {
R.drawable.ic_hide_media_24dp
} else {
R.drawable.ic_eye_24dp
}
return makeIcon(res)
}
private fun launchFilterActivity(filterContext: String, titleResource: Int) {

View File

@ -48,9 +48,10 @@ import com.keylesspalace.tusky.util.ThemeUtils
import com.keylesspalace.tusky.util.deserialize
import com.keylesspalace.tusky.util.serialize
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.IconicsSize
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizePx
import com.mikepenz.iconics.utils.size
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -69,8 +70,6 @@ class PreferencesFragment : Fragment(), Injectable {
lateinit var prefStore: PrefStore
lateinit var prefs: PrefData
private val iconSize by lazy { resources.getDimensionPixelSize(R.dimen.preference_icon_size) }
private var updateTrigger: (() -> Unit)? = null
private fun updatePrefs(updater: (PrefData) -> PrefData) {
lifecycleScope.launch {
@ -202,7 +201,7 @@ class PreferencesFragment : Fragment(), Injectable {
getString(R.string.pref_title_app_theme),
themeOptions,
{ prefs.appTheme },
makeIcon(GoogleMaterial.Icon.gmd_palette),
{ makeIcon(GoogleMaterial.Icon.gmd_palette) },
) {
updatePrefs { data -> data.copy(appTheme = it) }
}
@ -215,7 +214,7 @@ class PreferencesFragment : Fragment(), Injectable {
{
emojiSelector.summary
},
makeIcon(GoogleMaterial.Icon.gmd_sentiment_satisfied),
{ makeIcon(GoogleMaterial.Icon.gmd_sentiment_satisfied) },
) {
emojiSelector.showSelectionDialog()
@ -230,7 +229,7 @@ class PreferencesFragment : Fragment(), Injectable {
getString(R.string.pref_title_language),
languageOptions,
{ prefs.language },
makeIcon(GoogleMaterial.Icon.gmd_translate)
{ makeIcon(GoogleMaterial.Icon.gmd_translate) }
) {
updatePrefs { data -> data.copy(language = it) }
}
@ -246,7 +245,7 @@ class PreferencesFragment : Fragment(), Injectable {
getString(R.string.pref_status_text_size),
textSizeOptions,
{ prefs.statusTextSize },
makeIcon(GoogleMaterial.Icon.gmd_format_size)
{ makeIcon(GoogleMaterial.Icon.gmd_format_size) }
) {
updatePrefs { data -> data.copy(statusTextSize = it) }
}
@ -284,7 +283,7 @@ class PreferencesFragment : Fragment(), Injectable {
switchPreference(
getString(R.string.pref_title_bot_overlay),
{ prefs.showBotOverlay },
makeIcon(R.drawable.ic_bot_24dp),
{ makeIcon(R.drawable.ic_bot_24dp) },
) {
updatePrefs { data -> data.copy(showBotOverlay = it) }
}
@ -339,238 +338,24 @@ class PreferencesFragment : Fragment(), Injectable {
}
}
// override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
// makePreferenceScreen {
// preferenceCategory(R.string.pref_title_appearance_settings) {
// listPreference {
// setDefaultValue(AppTheme.NIGHT.value)
// setEntries(R.array.app_theme_names)
// entryValues = AppTheme.stringValues()
// key = PrefKeys.APP_THEME
// setSummaryProvider { entry }
// setTitle(R.string.pref_title_app_theme)
// icon = makeIcon(GoogleMaterial.Icon.gmd_palette)
// }
//
// emojiPreference(okhttpclient) {
// setDefaultValue("system_default")
// setIcon(R.drawable.ic_emoji_24dp)
// key = PrefKeys.EMOJI
// setSummary(R.string.system_default)
// setTitle(R.string.emoji_style)
// icon = makeIcon(GoogleMaterial.Icon.gmd_sentiment_satisfied)
// }
//
// listPreference {
// setDefaultValue("default")
// setEntries(R.array.language_entries)
// setEntryValues(R.array.language_values)
// key = PrefKeys.LANGUAGE
// setSummaryProvider { entry }
// setTitle(R.string.pref_title_language)
// icon = makeIcon(GoogleMaterial.Icon.gmd_translate)
// }
//
// listPreference {
// setDefaultValue("medium")
// setEntries(R.array.status_text_size_names)
// setEntryValues(R.array.status_text_size_values)
// key = PrefKeys.STATUS_TEXT_SIZE
// setSummaryProvider { entry }
// setTitle(R.string.pref_status_text_size)
// icon = makeIcon(GoogleMaterial.Icon.gmd_format_size)
// }
//
// listPreference {
// setDefaultValue("top")
// setEntries(R.array.pref_main_nav_position_options)
// setEntryValues(R.array.pref_main_nav_position_values)
// key = PrefKeys.MAIN_NAV_POSITION
// setSummaryProvider { entry }
// setTitle(R.string.pref_main_nav_position)
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.HIDE_TOP_TOOLBAR
// setTitle(R.string.pref_title_hide_top_toolbar)
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.FAB_HIDE
// setTitle(R.string.pref_title_hide_follow_button)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.ABSOLUTE_TIME_VIEW
// setTitle(R.string.pref_title_absolute_time)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(true)
// key = PrefKeys.SHOW_BOT_OVERLAY
// setTitle(R.string.pref_title_bot_overlay)
// isSingleLineTitle = false
// setIcon(R.drawable.ic_bot_24dp)
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.ANIMATE_GIF_AVATARS
// setTitle(R.string.pref_title_animate_gif_avatars)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.ANIMATE_CUSTOM_EMOJIS
// setTitle(R.string.pref_title_animate_custom_emojis)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(true)
// key = PrefKeys.USE_BLURHASH
// setTitle(R.string.pref_title_gradient_for_media)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.SHOW_CARDS_IN_TIMELINES
// setTitle(R.string.pref_title_show_cards_in_timelines)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(true)
// key = PrefKeys.SHOW_NOTIFICATIONS_FILTER
// setTitle(R.string.pref_title_show_notifications_filter)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(true)
// key = PrefKeys.CONFIRM_REBLOGS
// setTitle(R.string.pref_title_confirm_reblogs)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.CONFIRM_FAVOURITES
// setTitle(R.string.pref_title_confirm_favourites)
// isSingleLineTitle = false
// }
//
// switchPreference {
// setDefaultValue(true)
// key = PrefKeys.ENABLE_SWIPE_FOR_TABS
// setTitle(R.string.pref_title_enable_swipe_for_tabs)
// isSingleLineTitle = false
// }
// }
//
// preferenceCategory(R.string.pref_title_browser_settings) {
// switchPreference {
// setDefaultValue(false)
// key = PrefKeys.CUSTOM_TABS
// setTitle(R.string.pref_title_custom_tabs)
// isSingleLineTitle = false
// }
// }
//
// preferenceCategory(R.string.pref_title_timeline_filters) {
// preference {
// setTitle(R.string.pref_title_status_tabs)
// setOnPreferenceClickListener {
// activity?.let { activity ->
// val intent = PreferencesActivity.newIntent(activity, PreferencesActivity.TAB_FILTER_PREFERENCES)
// activity.startActivity(intent)
// activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
// }
// true
// }
// }
// }
//
// preferenceCategory(R.string.pref_title_wellbeing_mode) {
// switchPreference {
// title = getString(R.string.limit_notifications)
// setDefaultValue(false)
// key = PrefKeys.WELLBEING_LIMITED_NOTIFICATIONS
// setOnPreferenceChangeListener { _, value ->
// for (account in accountManager.accounts) {
// val notificationFilter = deserialize(account.notificationsFilter).toMutableSet()
//
// if (value == true) {
// notificationFilter.add(Notification.Type.FAVOURITE)
// notificationFilter.add(Notification.Type.FOLLOW)
// notificationFilter.add(Notification.Type.REBLOG)
// } else {
// notificationFilter.remove(Notification.Type.FAVOURITE)
// notificationFilter.remove(Notification.Type.FOLLOW)
// notificationFilter.remove(Notification.Type.REBLOG)
// }
//
// account.notificationsFilter = serialize(notificationFilter)
// accountManager.saveAccount(account)
// }
// true
// }
// }
//
// switchPreference {
// title = getString(R.string.wellbeing_hide_stats_posts)
// setDefaultValue(false)
// key = PrefKeys.WELLBEING_HIDE_STATS_POSTS
// }
//
// switchPreference {
// title = getString(R.string.wellbeing_hide_stats_profile)
// setDefaultValue(false)
// key = PrefKeys.WELLBEING_HIDE_STATS_PROFILE
// }
// }
//
// preferenceCategory(R.string.pref_title_proxy_settings) {
// httpProxyPref = preference {
// setTitle(R.string.pref_title_http_proxy_settings)
// setOnPreferenceClickListener {
// activity?.let { activity ->
// val intent = PreferencesActivity.newIntent(activity, PreferencesActivity.PROXY_PREFERENCES)
// activity.startActivity(intent)
// activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
// }
// true
// }
// }
// }
// }
// }
private fun makeIcon(icon: GoogleMaterial.Icon): IconicsDrawable {
val context = requireContext()
return IconicsDrawable(context, icon).apply {
sizePx = iconSize
colorInt = ThemeUtils.getColor(context, R.attr.iconColor)
}
}
private fun makeIcon(@DrawableRes res: Int): Drawable {
val context = requireContext()
return AppCompatResources.getDrawable(context, res)!!.apply {
setTint(ThemeUtils.getColor(context, R.attr.iconColor))
}
}
companion object {
fun newInstance(): PreferencesFragment {
return PreferencesFragment()
}
}
}
fun Fragment.makeIcon(icon: GoogleMaterial.Icon): IconicsDrawable {
val context = requireContext()
return IconicsDrawable(context, icon).apply {
size = IconicsSize.res(R.dimen.preference_icon_size)
colorInt = ThemeUtils.getColor(context, R.attr.iconColor)
}
}
fun Fragment.makeIcon(@DrawableRes res: Int): Drawable {
val context = requireContext()
return AppCompatResources.getDrawable(context, res)!!.apply {
setTint(ThemeUtils.getColor(context, R.attr.iconColor))
}
}

View File

@ -47,6 +47,7 @@ private fun itemLayout(context: Context): LinearLayout {
fun PreferenceParent.checkBoxPreference(
title: String,
selected: () -> Boolean,
icon: IconProvider = NoIconProvider,
onSelection: (Boolean) -> Unit
) {
val layout = inflateItemLayout().apply {
@ -63,6 +64,7 @@ fun PreferenceParent.checkBoxPreference(
layout.prefCutomContainer.addView(checkbox)
registerUpdate {
layout.setIcon(icon())
checkbox.isChecked = selected()
}
@ -71,6 +73,7 @@ fun PreferenceParent.checkBoxPreference(
fun PreferenceParent.clickPreference(
title: String,
icon: IconProvider = NoIconProvider,
onClick: () -> Unit,
) {
val layout = inflateItemLayout().apply {
@ -78,25 +81,29 @@ fun PreferenceParent.clickPreference(
setShowSummary(false)
}
layout.root.setOnClickListener { onClick() }
registerUpdate {
layout.setIcon(icon())
}
addPref(layout.root)
}
fun PreferenceParent.switchPreference(
title: String,
isChecked: () -> Boolean,
icon: Drawable? = null,
icon: IconProvider = NoIconProvider,
onSelection: (Boolean) -> Unit
) {
val layout = inflateItemLayout().apply {
setTitle(title)
setShowSummary(false)
icon?.let { setIcon(it) }
}
val switch = SwitchMaterial(context)
layout.prefCutomContainer.addView(switch)
registerUpdate {
switch.isChecked = isChecked()
layout.setIcon(icon())
}
layout.root.setOnClickListener { onSelection(!isChecked()) }
switch.setOnCheckedChangeListener { _, isChecked -> onSelection(isChecked) }
@ -110,6 +117,7 @@ fun PreferenceParent.switchPreference(
fun PreferenceParent.editTextPreference(
title: String,
value: () -> String,
icon: IconProvider = NoIconProvider,
onNewValue: (String) -> Unit
) {
val layout = inflateItemLayout()
@ -138,8 +146,9 @@ fun PreferenceParent.editTextPreference(
}
layout.prefTitle.text = title
registerUpdate {
layout.prefSummary.text = value()
layout.prefSummary.visible(value().isNotBlank())
layout.setSummary(value())
layout.setShowSummary(value().isNotBlank())
layout.setIcon(icon())
}
addPref(layout.root)
}
@ -153,20 +162,23 @@ fun <T> PreferenceParent.PreferenceOption(pair: Pair<T, Int>): PreferenceOption<
infix fun <T> T.named(name: String) = PreferenceOption(name, this)
typealias IconProvider = () -> Drawable?
val NoIconProvider: IconProvider = { null }
fun <T> PreferenceParent.listPreference(
title: String,
options: List<PreferenceOption<T>>,
selected: () -> T,
icon: Drawable? = null,
icon: IconProvider = NoIconProvider,
onSelection: (T) -> Unit,
) {
val layout = inflateItemLayout().apply {
setTitle(title)
setShowSummary(true)
icon?.let { setIcon(it) }
}
registerUpdate {
layout.setIcon(icon())
val selectedOptionIndex = options.indexOfFirst { it.value == selected() }
layout.setSummary(options[selectedOptionIndex].name)
@ -190,13 +202,12 @@ fun <T> PreferenceParent.listPreference(
fun PreferenceParent.customListPreference(
title: String,
selected: () -> String,
icon: Drawable? = null,
icon: IconProvider = NoIconProvider,
onClick: () -> Unit
) {
val layout = inflateItemLayout().apply {
setTitle(title)
setShowSummary(true)
icon?.let { setIcon(it) }
}
layout.root.setOnClickListener {
@ -204,6 +215,7 @@ fun PreferenceParent.customListPreference(
}
registerUpdate {
layout.setIcon(icon())
layout.setSummary(selected())
}
addPref(layout.root)
@ -284,6 +296,6 @@ private fun ItemPrefBinding.setShowSummary(show: Boolean) {
prefSummary.isVisible = show
}
private fun ItemPrefBinding.setIcon(icon: Drawable) {
private fun ItemPrefBinding.setIcon(icon: Drawable?) {
prefIcon.setImageDrawable(icon)
}

View File

@ -11,8 +11,8 @@
<ImageView
android:id="@+id/pref_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_width="@dimen/preference_icon_size"
android:layout_height="@dimen/preference_icon_size"
android:layout_marginRight="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"