2018-11-12 21:09:39 +01:00
|
|
|
/* Copyright 2018 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.fragment.preference
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2018-12-18 22:05:33 +01:00
|
|
|
import androidx.preference.Preference
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2018-11-12 21:09:39 +01:00
|
|
|
import com.keylesspalace.tusky.PreferencesActivity
|
|
|
|
import com.keylesspalace.tusky.R
|
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils
|
|
|
|
import com.keylesspalace.tusky.util.getNonNullString
|
|
|
|
import com.mikepenz.google_material_typeface_library.GoogleMaterial
|
|
|
|
import com.mikepenz.iconics.IconicsDrawable
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
fun PreferenceFragmentCompat.requirePreference(key: String): Preference {
|
|
|
|
return findPreference(key)!!
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
class PreferencesFragment : PreferenceFragmentCompat() {
|
|
|
|
|
|
|
|
private val iconSize by lazy {resources.getDimensionPixelSize(R.dimen.preference_icon_size)}
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences)
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val themePreference: Preference = requirePreference("appTheme")
|
2019-10-12 20:00:29 +02:00
|
|
|
themePreference.icon = IconicsDrawable(themePreference.context, GoogleMaterial.Icon.gmd_palette).sizePx(iconSize).color(ThemeUtils.getColor(themePreference.context, R.attr.preference_icon_tint))
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val emojiPreference: Preference = requirePreference("emojiCompat")
|
2019-10-12 20:00:29 +02:00
|
|
|
emojiPreference.icon = IconicsDrawable(emojiPreference.context, GoogleMaterial.Icon.gmd_sentiment_satisfied).sizePx(iconSize).color(ThemeUtils.getColor(emojiPreference.context, R.attr.preference_icon_tint))
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val textSizePreference: Preference = requirePreference("statusTextSize")
|
2019-10-12 20:00:29 +02:00
|
|
|
textSizePreference.icon = IconicsDrawable(textSizePreference.context, GoogleMaterial.Icon.gmd_format_size).sizePx(iconSize).color(ThemeUtils.getColor(textSizePreference.context, R.attr.preference_icon_tint))
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val timelineFilterPreferences: Preference = requirePreference("timelineFilterPreferences")
|
2018-11-16 13:31:03 +01:00
|
|
|
timelineFilterPreferences.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)
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val httpProxyPreferences: Preference = requirePreference("httpProxyPreferences")
|
2018-11-16 13:31:03 +01:00
|
|
|
httpProxyPreferences.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)
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val languagePreference: Preference = requirePreference("language")
|
2019-10-12 20:00:29 +02:00
|
|
|
languagePreference.icon = IconicsDrawable(languagePreference.context, GoogleMaterial.Icon.gmd_translate).sizePx(iconSize).color(ThemeUtils.getColor(languagePreference.context, R.attr.preference_icon_tint))
|
2019-04-21 22:59:33 +02:00
|
|
|
|
|
|
|
val botIndicatorPreference = requirePreference("showBotOverlay")
|
|
|
|
val botDrawable = botIndicatorPreference.context.getDrawable(R.drawable.ic_bot_24dp)
|
2019-10-12 20:00:29 +02:00
|
|
|
ThemeUtils.setDrawableTint(context, botDrawable, R.attr.preference_icon_tint)
|
2019-04-21 22:59:33 +02:00
|
|
|
botIndicatorPreference.icon = botDrawable
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
updateHttpProxySummary()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateHttpProxySummary() {
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val httpProxyPref: Preference = requirePreference("httpProxyPreferences")
|
2018-11-12 21:09:39 +01:00
|
|
|
|
|
|
|
val sharedPreferences = preferenceManager.sharedPreferences
|
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
val httpProxyEnabled = sharedPreferences.getBoolean("httpProxyEnabled", false)
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
val httpServer = sharedPreferences.getNonNullString("httpProxyServer", "")
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
try {
|
|
|
|
val httpPort = sharedPreferences.getNonNullString("httpProxyPort", "-1").toInt()
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
if (httpProxyEnabled && httpServer.isNotBlank() && httpPort > 0 && httpPort < 65535) {
|
|
|
|
httpProxyPref.summary = "$httpServer:$httpPort"
|
|
|
|
return
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
2019-03-08 20:33:42 +01:00
|
|
|
} catch (e: NumberFormatException) {
|
|
|
|
// user has entered wrong port, fall back to empty summary
|
|
|
|
}
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
httpProxyPref.summary = ""
|
2018-11-12 21:09:39 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
fun newInstance(): PreferencesFragment {
|
|
|
|
return PreferencesFragment()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|