WIP prefs UI

This commit is contained in:
charlag 2022-04-10 22:12:12 +02:00
parent 210a98367a
commit 3130ffd8ea
No known key found for this signature in database
GPG Key ID: 5B96E7C76F0CA558
6 changed files with 487 additions and 490 deletions

View File

@ -22,32 +22,16 @@ import android.util.Log
import androidx.annotation.DrawableRes
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.AccountListActivity
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.listPreference
import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.preference
import com.keylesspalace.tusky.settings.preferenceCategory
import com.keylesspalace.tusky.settings.switchPreference
import com.keylesspalace.tusky.util.ThemeUtils
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeRes
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
@ -65,203 +49,203 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
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
}
}
}
}
// 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 fun openNotificationPrefs() {

View File

@ -17,15 +17,9 @@ package com.keylesspalace.tusky.components.preference
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.components.notifications.NotificationHelper
import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.preferenceCategory
import com.keylesspalace.tusky.settings.switchPreference
import javax.inject.Inject
class NotificationPreferencesFragment : PreferenceFragmentCompat(), Injectable {
@ -36,132 +30,132 @@ class NotificationPreferencesFragment : PreferenceFragmentCompat(), Injectable {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val activeAccount = accountManager.activeAccount ?: return
val context = requireContext()
makePreferenceScreen {
switchPreference {
setTitle(R.string.pref_title_notifications_enabled)
key = PrefKeys.NOTIFICATIONS_ENABLED
isIconSpaceReserved = false
isChecked = activeAccount.notificationsEnabled
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsEnabled = newValue as Boolean }
if (NotificationHelper.areNotificationsEnabled(context, accountManager)) {
NotificationHelper.enablePullNotifications(context)
} else {
NotificationHelper.disablePullNotifications(context)
}
true
}
}
preferenceCategory(R.string.pref_title_notification_filters) { category ->
category.dependency = PrefKeys.NOTIFICATIONS_ENABLED
category.isIconSpaceReserved = false
switchPreference {
setTitle(R.string.pref_title_notification_filter_follows)
key = PrefKeys.NOTIFICATIONS_FILTER_FOLLOWS
isIconSpaceReserved = false
isChecked = activeAccount.notificationsFollowed
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsFollowed = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_filter_follow_requests)
key = PrefKeys.NOTIFICATION_FILTER_FOLLOW_REQUESTS
isIconSpaceReserved = false
isChecked = activeAccount.notificationsFollowRequested
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsFollowRequested = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_filter_reblogs)
key = PrefKeys.NOTIFICATION_FILTER_REBLOGS
isIconSpaceReserved = false
isChecked = activeAccount.notificationsReblogged
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsReblogged = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_filter_favourites)
key = PrefKeys.NOTIFICATION_FILTER_FAVS
isIconSpaceReserved = false
isChecked = activeAccount.notificationsFavorited
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsFavorited = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_filter_poll)
key = PrefKeys.NOTIFICATION_FILTER_POLLS
isIconSpaceReserved = false
isChecked = activeAccount.notificationsPolls
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsPolls = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_filter_subscriptions)
key = PrefKeys.NOTIFICATION_FILTER_SUBSCRIPTIONS
isIconSpaceReserved = false
isChecked = activeAccount.notificationsSubscriptions
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsSubscriptions = newValue as Boolean }
true
}
}
}
preferenceCategory(R.string.pref_title_notification_alerts) { category ->
category.dependency = PrefKeys.NOTIFICATIONS_ENABLED
category.isIconSpaceReserved = false
switchPreference {
setTitle(R.string.pref_title_notification_alert_sound)
key = PrefKeys.NOTIFICATION_ALERT_SOUND
isIconSpaceReserved = false
isChecked = activeAccount.notificationSound
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationSound = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_alert_vibrate)
key = PrefKeys.NOTIFICATION_ALERT_VIBRATE
isIconSpaceReserved = false
isChecked = activeAccount.notificationVibration
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationVibration = newValue as Boolean }
true
}
}
switchPreference {
setTitle(R.string.pref_title_notification_alert_light)
key = PrefKeys.NOTIFICATION_ALERT_LIGHT
isIconSpaceReserved = false
isChecked = activeAccount.notificationLight
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationLight = newValue as Boolean }
true
}
}
}
}
// makePreferenceScreen {
// switchPreference {
// setTitle(R.string.pref_title_notifications_enabled)
// key = PrefKeys.NOTIFICATIONS_ENABLED
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsEnabled
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsEnabled = newValue as Boolean }
// if (NotificationHelper.areNotificationsEnabled(context, accountManager)) {
// NotificationHelper.enablePullNotifications(context)
// } else {
// NotificationHelper.disablePullNotifications(context)
// }
// true
// }
// }
//
// preferenceCategory(R.string.pref_title_notification_filters) { category ->
// category.dependency = PrefKeys.NOTIFICATIONS_ENABLED
// category.isIconSpaceReserved = false
//
// switchPreference {
// setTitle(R.string.pref_title_notification_filter_follows)
// key = PrefKeys.NOTIFICATIONS_FILTER_FOLLOWS
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsFollowed
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsFollowed = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_filter_follow_requests)
// key = PrefKeys.NOTIFICATION_FILTER_FOLLOW_REQUESTS
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsFollowRequested
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsFollowRequested = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_filter_reblogs)
// key = PrefKeys.NOTIFICATION_FILTER_REBLOGS
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsReblogged
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsReblogged = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_filter_favourites)
// key = PrefKeys.NOTIFICATION_FILTER_FAVS
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsFavorited
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsFavorited = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_filter_poll)
// key = PrefKeys.NOTIFICATION_FILTER_POLLS
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsPolls
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsPolls = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_filter_subscriptions)
// key = PrefKeys.NOTIFICATION_FILTER_SUBSCRIPTIONS
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationsSubscriptions
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationsSubscriptions = newValue as Boolean }
// true
// }
// }
// }
//
// preferenceCategory(R.string.pref_title_notification_alerts) { category ->
// category.dependency = PrefKeys.NOTIFICATIONS_ENABLED
// category.isIconSpaceReserved = false
//
// switchPreference {
// setTitle(R.string.pref_title_notification_alert_sound)
// key = PrefKeys.NOTIFICATION_ALERT_SOUND
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationSound
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationSound = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_alert_vibrate)
// key = PrefKeys.NOTIFICATION_ALERT_VIBRATE
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationVibration
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationVibration = newValue as Boolean }
// true
// }
// }
//
// switchPreference {
// setTitle(R.string.pref_title_notification_alert_light)
// key = PrefKeys.NOTIFICATION_ALERT_LIGHT
// isIconSpaceReserved = false
// isChecked = activeAccount.notificationLight
// setOnPreferenceChangeListener { _, newValue ->
// updateAccount { it.notificationLight = newValue as Boolean }
// true
// }
// }
// }
// }
}
private inline fun updateAccount(changer: (AccountEntity) -> Unit) {

View File

@ -19,25 +19,16 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.settings.AppTheme
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.settings.emojiPreference
import com.keylesspalace.tusky.settings.listPreference
import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.preference
import com.keylesspalace.tusky.settings.preferenceCategory
import com.keylesspalace.tusky.settings.switchPreference
import com.keylesspalace.tusky.util.ThemeUtils
import com.keylesspalace.tusky.util.deserialize
import com.keylesspalace.tusky.util.getNonNullString
import com.keylesspalace.tusky.util.serialize
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.utils.colorInt
@ -54,14 +45,20 @@ class PreferencesFragment : Fragment(), Injectable {
lateinit var accountManager: AccountManager
private val iconSize by lazy { resources.getDimensionPixelSize(R.dimen.preference_icon_size) }
private var httpProxyPref: Preference? = null
// private var httpProxyPref: Preference? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
val view = FrameLayout(inflater.context)
makePreferenceScreen(view) {
preferenceCategory(R.string.pref_title_appearance_settings) {
}
}
return view
}
// override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
@ -288,29 +285,29 @@ class PreferencesFragment : Fragment(), Injectable {
override fun onResume() {
super.onResume()
updateHttpProxySummary()
// updateHttpProxySummary()
}
private fun updateHttpProxySummary() {
preferenceManager.sharedPreferences?.let { sharedPreferences ->
val httpProxyEnabled = sharedPreferences.getBoolean(PrefKeys.HTTP_PROXY_ENABLED, false)
val httpServer = sharedPreferences.getNonNullString(PrefKeys.HTTP_PROXY_SERVER, "")
try {
val httpPort = sharedPreferences.getNonNullString(PrefKeys.HTTP_PROXY_PORT, "-1")
.toInt()
if (httpProxyEnabled && httpServer.isNotBlank() && httpPort > 0 && httpPort < 65535) {
httpProxyPref?.summary = "$httpServer:$httpPort"
return
}
} catch (e: NumberFormatException) {
// user has entered wrong port, fall back to empty summary
}
httpProxyPref?.summary = ""
}
}
// private fun updateHttpProxySummary() {
// preferenceManager.sharedPreferences?.let { sharedPreferences ->
// val httpProxyEnabled = sharedPreferences.getBoolean(PrefKeys.HTTP_PROXY_ENABLED, false)
// val httpServer = sharedPreferences.getNonNullString(PrefKeys.HTTP_PROXY_SERVER, "")
//
// try {
// val httpPort = sharedPreferences.getNonNullString(PrefKeys.HTTP_PROXY_PORT, "-1")
// .toInt()
//
// if (httpProxyEnabled && httpServer.isNotBlank() && httpPort > 0 && httpPort < 65535) {
// httpProxyPref?.summary = "$httpServer:$httpPort"
// return
// }
// } catch (e: NumberFormatException) {
// // user has entered wrong port, fall back to empty summary
// }
//
// httpProxyPref?.summary = ""
// }
// }
companion object {
fun newInstance(): PreferencesFragment {

View File

@ -17,39 +17,34 @@ package com.keylesspalace.tusky.components.preference
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.settings.editTextPreference
import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.switchPreference
import kotlin.system.exitProcess
class ProxyPreferencesFragment : PreferenceFragmentCompat() {
private var pendingRestart = false
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
makePreferenceScreen {
switchPreference {
setTitle(R.string.pref_title_http_proxy_enable)
isIconSpaceReserved = false
key = PrefKeys.HTTP_PROXY_ENABLED
setDefaultValue(false)
}
editTextPreference {
setTitle(R.string.pref_title_http_proxy_server)
key = PrefKeys.HTTP_PROXY_SERVER
isIconSpaceReserved = false
setSummaryProvider { text }
}
editTextPreference {
setTitle(R.string.pref_title_http_proxy_port)
key = PrefKeys.HTTP_PROXY_PORT
isIconSpaceReserved = false
setSummaryProvider { text }
}
}
// makePreferenceScreen {
// switchPreference {
// setTitle(R.string.pref_title_http_proxy_enable)
// isIconSpaceReserved = false
// key = PrefKeys.HTTP_PROXY_ENABLED
// setDefaultValue(false)
// }
//
// editTextPreference {
// setTitle(R.string.pref_title_http_proxy_server)
// key = PrefKeys.HTTP_PROXY_SERVER
// isIconSpaceReserved = false
// setSummaryProvider { text }
// }
//
// editTextPreference {
// setTitle(R.string.pref_title_http_proxy_port)
// key = PrefKeys.HTTP_PROXY_PORT
// isIconSpaceReserved = false
// setSummaryProvider { text }
// }
// }
}
override fun onPause() {

View File

@ -17,33 +17,28 @@ package com.keylesspalace.tusky.components.preference
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.settings.checkBoxPreference
import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.preferenceCategory
class TabFilterPreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
makePreferenceScreen {
preferenceCategory(R.string.title_home) { category ->
category.isIconSpaceReserved = false
checkBoxPreference {
setTitle(R.string.pref_title_show_boosts)
key = PrefKeys.TAB_FILTER_HOME_BOOSTS
setDefaultValue(true)
isIconSpaceReserved = false
}
checkBoxPreference {
setTitle(R.string.pref_title_show_replies)
key = PrefKeys.TAB_FILTER_HOME_REPLIES
setDefaultValue(false)
isIconSpaceReserved = false
}
}
}
// makePreferenceScreen {
// preferenceCategory(R.string.title_home) { category ->
// category.isIconSpaceReserved = false
//
// checkBoxPreference {
// setTitle(R.string.pref_title_show_boosts)
// key = PrefKeys.TAB_FILTER_HOME_BOOSTS
// setDefaultValue(true)
// isIconSpaceReserved = false
// }
//
// checkBoxPreference {
// setTitle(R.string.pref_title_show_replies)
// key = PrefKeys.TAB_FILTER_HOME_REPLIES
// setDefaultValue(false)
// isIconSpaceReserved = false
// }
// }
// }
}
companion object {

View File

@ -1,7 +1,15 @@
package com.keylesspalace.tusky.settings
import android.content.Context
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.core.view.marginLeft
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment
import androidx.preference.CheckBoxPreference
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
@ -10,82 +18,106 @@ import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreference
import at.connyduck.sparkbutton.helpers.Utils
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.components.preference.EmojiPreference
import okhttp3.OkHttpClient
class PreferenceParent(
val context: Context,
val addPref: (pref: Preference) -> Unit
val addPref: (pref: View) -> Unit
)
inline fun PreferenceParent.preference(builder: Preference.() -> Unit): Preference {
val pref = Preference(context)
builder(pref)
addPref(pref)
return pref
}
inline fun PreferenceParent.listPreference(builder: ListPreference.() -> Unit): ListPreference {
val pref = ListPreference(context)
builder(pref)
addPref(pref)
return pref
}
inline fun PreferenceParent.emojiPreference(okHttpClient: OkHttpClient, builder: EmojiPreference.() -> Unit): EmojiPreference {
val pref = EmojiPreference(context, okHttpClient)
builder(pref)
addPref(pref)
return pref
}
inline fun PreferenceParent.switchPreference(
builder: SwitchPreference.() -> Unit
): SwitchPreference {
val pref = SwitchPreference(context)
builder(pref)
addPref(pref)
return pref
}
inline fun PreferenceParent.editTextPreference(
builder: EditTextPreference.() -> Unit
): EditTextPreference {
val pref = EditTextPreference(context)
builder(pref)
addPref(pref)
return pref
}
inline fun PreferenceParent.checkBoxPreference(
builder: CheckBoxPreference.() -> Unit
): CheckBoxPreference {
val pref = CheckBoxPreference(context)
builder(pref)
addPref(pref)
return pref
}
//inline fun PreferenceParent.preference(builder: Preference.() -> Unit): Preference {
// val pref = Preference(context)
// builder(pref)
// addPref(pref)
// return pref
//}
//
//inline fun PreferenceParent.listPreference(builder: ListPreference.() -> Unit): ListPreference {
// val pref = ListPreference(context)
// builder(pref)
// addPref(pref)
// return pref
//}
//
//inline fun PreferenceParent.emojiPreference(
// okHttpClient: OkHttpClient,
// builder: EmojiPreference.() -> Unit
//): EmojiPreference {
// val pref = EmojiPreference(context, okHttpClient)
// builder(pref)
// addPref(pref)
// return pref
//}
//
//inline fun PreferenceParent.switchPreference(
// builder: SwitchPreference.() -> Unit
//): SwitchPreference {
// val pref = SwitchPreference(context)
// builder(pref)
// addPref(pref)
// return pref
//}
//
//inline fun PreferenceParent.editTextPreference(
// builder: EditTextPreference.() -> Unit
//): EditTextPreference {
// val pref = EditTextPreference(context)
// builder(pref)
// addPref(pref)
// return pref
//}
//
//inline fun PreferenceParent.checkBoxPreference(
// builder: CheckBoxPreference.() -> Unit
//): CheckBoxPreference {
// val pref = CheckBoxPreference(context)
// builder(pref)
// addPref(pref)
// return pref
//}
//
inline fun PreferenceParent.preferenceCategory(
@StringRes title: Int,
builder: PreferenceParent.(PreferenceCategory) -> Unit
builder: PreferenceParent.() -> Unit
) {
val category = PreferenceCategory(context)
val category = LinearLayout(context)
addPref(category)
category.setTitle(title)
val newParent = PreferenceParent(context) { category.addPreference(it) }
builder(newParent, category)
val titleView = TextView(context).apply {
layoutParams = ViewGroup.MarginLayoutParams(
ViewGroup.MarginLayoutParams.WRAP_CONTENT,
ViewGroup.MarginLayoutParams.WRAP_CONTENT
).apply {
leftMargin = dpToPx(20)
topMargin = 10
rightMargin = 10
bottomMargin = 10
}
val typedValue = TypedValue()
context.theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
setTextColor(typedValue.data)
setText(title)
}
category.addView(titleView)
val newParent = PreferenceParent(context) { category.addView(it) }
builder(newParent)
}
inline fun PreferenceFragmentCompat.makePreferenceScreen(
inline fun Fragment.makePreferenceScreen(
viewGroup: ViewGroup,
builder: PreferenceParent.() -> Unit
): PreferenceScreen {
) {
val context = requireContext()
val screen = preferenceManager.createPreferenceScreen(context)
val parent = PreferenceParent(context) { screen.addPreference(it) }
val parent = PreferenceParent(context) { viewGroup.addView(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
}
fun View.dpToPx(dp: Int) = Utils.dpToPx(this.context, dp)