[Android] Widgets: persist custom colors and display them in color pickers
Nicolas Pomepuy
git at videolan.org
Wed May 18 09:57:57 UTC 2022
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed May 11 08:43:38 2022 +0200| [c18c37c24d3498b981c8379d4d076f8c1669b5bf] | committer: Duncan McNamara
Widgets: persist custom colors and display them in color pickers
> https://code.videolan.org/videolan/vlc-android/commit/c18c37c24d3498b981c8379d4d076f8c1669b5bf
---
.../src/main/java/org/videolan/tools/Settings.kt | 3 ++
.../gui/preferences/widgets/PreferencesWidgets.kt | 57 +++++++++++++++++++++-
2 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt
index 21dc626cd..663315706 100644
--- a/application/tools/src/main/java/org/videolan/tools/Settings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt
@@ -163,6 +163,9 @@ const val LAST_LOCK_ORIENTATION = "last_lock_orientation"
const val INITIAL_PERMISSION_ASKED = "initial_permission_asked"
const val PERMISSION_NEVER_ASK = "permission_never_ask"
+const val WIDGETS_BACKGROUND_LAST_COLORS = "widgets_background_last_colors"
+const val WIDGETS_FOREGROUND_LAST_COLORS = "widgets_foreground_last_colors"
+
class DeviceInfo(context: Context) {
val pm = context.packageManager
val tm = context.getSystemService<TelephonyManager>()!!
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/widgets/PreferencesWidgets.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/widgets/PreferencesWidgets.kt
index 8c2cea3c5..68fbe877e 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/widgets/PreferencesWidgets.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/widgets/PreferencesWidgets.kt
@@ -35,9 +35,15 @@ import androidx.preference.ListPreference
import androidx.preference.SeekBarPreference
import com.google.android.material.color.DynamicColors
import com.jaredrummler.android.colorpicker.ColorPreferenceCompat
+import com.squareup.moshi.JsonAdapter
+import com.squareup.moshi.Moshi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
+import org.videolan.tools.Settings
+import org.videolan.tools.WIDGETS_BACKGROUND_LAST_COLORS
+import org.videolan.tools.WIDGETS_FOREGROUND_LAST_COLORS
+import org.videolan.tools.putSingle
import org.videolan.vlc.R
import org.videolan.vlc.gui.preferences.BasePreferenceFragment
import org.videolan.vlc.repository.WidgetRepository
@@ -47,6 +53,8 @@ const val WIDGET_ID = "WIDGET_ID"
class PreferencesWidgets : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
+ private lateinit var settings: SharedPreferences
+ private lateinit var jsonAdapter: JsonAdapter<SavedColors>
internal lateinit var model: WidgetViewModel
private lateinit var backgroundPreference: ColorPreferenceCompat
private lateinit var foregroundPreference: ColorPreferenceCompat
@@ -68,6 +76,9 @@ class PreferencesWidgets : BasePreferenceFragment(), SharedPreferences.OnSharedP
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
+ val moshi: Moshi = Moshi.Builder().build()
+ jsonAdapter = moshi.adapter(SavedColors::class.java)
+ settings = Settings.getInstance(requireActivity())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -80,6 +91,8 @@ class PreferencesWidgets : BasePreferenceFragment(), SharedPreferences.OnSharedP
val id = (arguments?.getInt(WIDGET_ID) ?: -2)
if (id == -2) throw IllegalStateException("Invalid widget id")
model = ViewModelProvider(this, WidgetViewModel.Factory(requireActivity(), id))[WidgetViewModel::class.java]
+ updateSavedColors(true)
+ updateSavedColors(false)
model.widget.observe(requireActivity()) { widget ->
if (widget == null) return at observe
if (!DynamicColors.isDynamicColorAvailable() && widget.theme == 0) {
@@ -110,10 +123,14 @@ class PreferencesWidgets : BasePreferenceFragment(), SharedPreferences.OnSharedP
model.widget.value?.opacity = sharedPreferences.getInt(key, 100)
}
"background_color" -> {
- model.widget.value?.backgroundColor = sharedPreferences.getInt(key, ContextCompat.getColor(requireActivity(), R.color.black))
+ val newColor = sharedPreferences.getInt(key, ContextCompat.getColor(requireActivity(), R.color.black))
+ saveNewColor(false, newColor)
+ model.widget.value?.backgroundColor = newColor
}
"foreground_color" -> {
- model.widget.value?.foregroundColor = sharedPreferences.getInt(key, ContextCompat.getColor(requireActivity(), R.color.white))
+ val newColor = sharedPreferences.getInt(key, ContextCompat.getColor(requireActivity(), R.color.white))
+ saveNewColor(true, newColor)
+ model.widget.value?.foregroundColor = newColor
}
"widget_theme" -> {
val newValue = sharedPreferences.getString(key, "0")?.toInt() ?: 0
@@ -147,6 +164,38 @@ class PreferencesWidgets : BasePreferenceFragment(), SharedPreferences.OnSharedP
updateWidgetEntity()
}
+ /**
+ * Saves a new color to the shared pref to show it again in the color picker next time
+ *
+ * @param foreground is this for the foreground color?
+ * @param newColor the color to save
+ */
+ private fun saveNewColor(foreground:Boolean, newColor: Int) {
+ val pref = if (foreground)foregroundPreference else backgroundPreference
+ val key = if (foreground)WIDGETS_FOREGROUND_LAST_COLORS else WIDGETS_BACKGROUND_LAST_COLORS
+ if (!pref.presets.contains(newColor)) {
+ val colorListString = settings.getString(key, "")
+ val oldColors = if (!colorListString.isNullOrBlank()) ArrayList(jsonAdapter.fromJson(colorListString)!!.colors) else ArrayList()
+ oldColors.add(0, newColor)
+ val newColors = SavedColors((if (oldColors.size > 5) oldColors.slice(0..5) else oldColors).distinct())
+ settings.putSingle(key, jsonAdapter.toJson(newColors))
+ }
+ updateSavedColors(foreground)
+ }
+
+ /**
+ * Update a color picker colors with saved ones
+ *
+ * @param foreground is this for the foreground color?
+ */
+ private fun updateSavedColors(foreground:Boolean) {
+ val colorListString = settings.getString(if (foreground) WIDGETS_FOREGROUND_LAST_COLORS else WIDGETS_BACKGROUND_LAST_COLORS, "")
+ val oldColors = if (!colorListString.isNullOrBlank()) ArrayList(jsonAdapter.fromJson(colorListString)!!.colors) else ArrayList()
+ val pref = if (foreground)foregroundPreference else backgroundPreference
+ pref.presets = pref.presets.toMutableList().apply { addAll(oldColors) }.distinct().toIntArray()
+
+ }
+
private fun updateWidgetEntity() {
model.widget.value?.let { widget ->
lifecycleScope.launch {
@@ -156,3 +205,7 @@ class PreferencesWidgets : BasePreferenceFragment(), SharedPreferences.OnSharedP
}
}
+
+class SavedColors(
+ val colors: List<Int>,
+)
\ No newline at end of file
More information about the Android
mailing list