From b364590d4b0f6bc247e3dc3f5c0acf276a2086b8 Mon Sep 17 00:00:00 2001 From: Ariel Costas Date: Sat, 3 Sep 2022 22:49:02 +0200 Subject: [PATCH] Implement using a color or another depending on light/dark mode setting Signed-off-by: Ariel Costas --- .../java/corewala/buran/ui/GemActivity.kt | 57 +++++++++++-------- .../corewala/buran/ui/settings/ColorThemes.kt | 30 ++++++++++ .../buran/ui/settings/SettingsFragment.kt | 43 +++++++------- 3 files changed, 82 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/corewala/buran/ui/settings/ColorThemes.kt diff --git a/app/src/main/java/corewala/buran/ui/GemActivity.kt b/app/src/main/java/corewala/buran/ui/GemActivity.kt index c2413fc..406b0fc 100644 --- a/app/src/main/java/corewala/buran/ui/GemActivity.kt +++ b/app/src/main/java/corewala/buran/ui/GemActivity.kt @@ -28,7 +28,6 @@ import androidx.databinding.DataBindingUtil import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar -import corewala.* import corewala.buran.BuildConfig import corewala.buran.Buran import corewala.buran.OmniTerm @@ -48,7 +47,14 @@ import corewala.buran.ui.content_text.TextDialog import corewala.buran.ui.gemtext_adapter.AbstractGemtextAdapter import corewala.buran.ui.modals_menus.history.HistoryDialog import corewala.buran.ui.modals_menus.overflow.OverflowPopup +import corewala.buran.ui.settings.COLORSCHEME_DARK +import corewala.buran.ui.settings.COLORSCHEME_LIGHT +import corewala.buran.ui.settings.PAIRS import corewala.buran.ui.settings.SettingsActivity +import corewala.hideKeyboard +import corewala.showKeyboard +import corewala.toURI +import corewala.visibleRetainingSpace import java.io.File import java.io.FileInputStream import java.io.FileOutputStream @@ -360,32 +366,35 @@ class GemActivity : AppCompatActivity() { Buran.DEFAULT_SEARCH_BASE ) ?: Buran.DEFAULT_SEARCH_BASE - if( - !searchBase.startsWith("gemini://") - or searchBase.contains(" ") - or !searchBase.contains(".") - or !searchBase.endsWith("?") - ){ - searchBase = Buran.DEFAULT_SEARCH_BASE - } + if ( + !searchBase.startsWith("gemini://") + or searchBase.contains(" ") + or !searchBase.contains(".") + or !searchBase.endsWith("?") + ) { + searchBase = Buran.DEFAULT_SEARCH_BASE + } - when { - prefs.contains("background_colour") -> { - when (val backgroundColor = prefs.getString("background_colour", "#XXXXXX")) { - "#XXXXXX" -> binding.rootCoord.background = null - else -> binding.rootCoord.background = ColorDrawable(Color.parseColor("$backgroundColor")) - } - } - } + if (prefs.contains("bg_name")) { + val backgroundName = prefs.getString("bg_name", "none") + val colorscheme = if (prefs.getString("theme", null) == "theme_Dark") { + COLORSCHEME_DARK + } else { + COLORSCHEME_LIGHT + } + val color = + PAIRS[backgroundName]?.byTheme(colorscheme) ?: PAIRS["none"]!!.byTheme(colorscheme) + binding.rootCoord.background = ColorDrawable(Color.parseColor(color)) + } - val showInlineIcons = prefs.getBoolean( - "show_inline_icons", - true - ) - adapter.inlineIcons(showInlineIcons) + val showInlineIcons = prefs.getBoolean( + "show_inline_icons", + true + ) + adapter.inlineIcons(showInlineIcons) - val showLinkButtons = prefs.getBoolean( - "show_link_buttons", + val showLinkButtons = prefs.getBoolean( + "show_link_buttons", false ) adapter.linkButtons(showLinkButtons) diff --git a/app/src/main/java/corewala/buran/ui/settings/ColorThemes.kt b/app/src/main/java/corewala/buran/ui/settings/ColorThemes.kt new file mode 100644 index 0000000..3538e07 --- /dev/null +++ b/app/src/main/java/corewala/buran/ui/settings/ColorThemes.kt @@ -0,0 +1,30 @@ +package corewala.buran.ui.settings + +val COLORSCHEME_DARK = "dark"; +val COLORSCHEME_LIGHT = "light"; + +data class ColorPair(val light: String, val dark: String) { + fun byTheme(theme: String): String { + return if (theme == COLORSCHEME_DARK) dark else light + } +} + +val PAIRS: Map = mapOf( + Pair("none", ColorPair("#ffffff", "#000000")), + Pair("red", ColorPair("#fee2e2", "#7f1d1d")), + Pair("orange", ColorPair("#ffedd5", "#7c2d12")), + Pair("yellow", ColorPair("#fef9c3", "#713f12")), + Pair("green", ColorPair("#dcfce7", "#14532d")), + Pair("sky", ColorPair("#e0f2fe", "#0c4a6e")), + Pair("purple", ColorPair("#f3e8ff", "#581c87")) +) + +fun getNameByColor(color: String, colorscheme: String): String { + for ((key, value) in PAIRS) { + if (value.byTheme(colorscheme).toUpperCase() == color.toUpperCase()) { + return key + break + } + } + return PAIRS["none"]!!.byTheme(colorscheme) +} \ No newline at end of file diff --git a/app/src/main/java/corewala/buran/ui/settings/SettingsFragment.kt b/app/src/main/java/corewala/buran/ui/settings/SettingsFragment.kt index 6ea7b38..2bbdde7 100644 --- a/app/src/main/java/corewala/buran/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/corewala/buran/ui/settings/SettingsFragment.kt @@ -24,7 +24,7 @@ const val PREFS_SET_CLIENT_CERT_REQ = 20 class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener { - lateinit var prefs: SharedPreferences + lateinit var prefs: SharedPreferences private lateinit var clientCertPref: Preference @@ -276,36 +276,31 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan // Background colour picker val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity) val backgroundColourPreference = Preference(context) - backgroundColourPreference.key = "background_colour" + val colorscheme = if (themePreference.value == "theme_Dark") { + COLORSCHEME_DARK + } else { + COLORSCHEME_LIGHT + } + backgroundColourPreference.setTitle(R.string.prefs_override_page_background_title) backgroundColourPreference.setSummary(R.string.prefs_override_page_background) backgroundColourPreference.setOnPreferenceClickListener { ColorSheet().colorPicker( - selectedColor = sharedPreferences.getInt( - "background_colour_id", - ColorSheet.NO_COLOR + selectedColor = Color.parseColor( + PAIRS[sharedPreferences.getString( + "bg_name", + null + )]?.byTheme(colorscheme) ?: PAIRS["none"]!!.byTheme("none") ), - colors = listOf( - Color.parseColor("#fee2e2"), - Color.parseColor("#ffedd5"), - Color.parseColor("#fef9c3"), - Color.parseColor("#dcfce7"), - Color.parseColor("#e0f2fe"), - Color.parseColor("#f3e8ff"), - - Color.parseColor("#7f1d1d"), - Color.parseColor("#7c2d12"), - Color.parseColor("#713f12"), - Color.parseColor("#14532d"), - Color.parseColor("#0c4a6e"), - Color.parseColor("#581c87"), - - ).toIntArray(), - noColorOption = true, + colors = PAIRS.map { + Color.parseColor(it.value.byTheme(colorscheme)) + }.toIntArray(), + noColorOption = false, listener = { + val chosenHex = ColorSheetUtils.colorToHex(it) val editor = sharedPreferences.edit() - editor.putString(backgroundColourPreference.key, ColorSheetUtils.colorToHex(it)) - editor.putInt("background_colour_id", it) + val namebycolor = getNameByColor(chosenHex, colorscheme) + editor.putString("bg_name", namebycolor) editor.commit() backgroundColourPreference.callChangeListener(it) }