Эх сурвалжийг харах

SettingsController: theme switch preferences

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey 2 жил өмнө
parent
commit
4c31f6f2e9

+ 14 - 0
app/src/main/java/com/nextcloud/talk/controllers/SettingsController.kt

@@ -518,6 +518,20 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
         }
 
         themeCategories()
+        themeSwitchPreferences()
+    }
+
+    private fun themeSwitchPreferences() {
+        binding.run {
+            listOf(
+                settingsScreenLock,
+                settingsScreenSecurity,
+                settingsIncognitoKeyboard,
+                settingsPhoneBookIntegration,
+                settingsReadPrivacy,
+                settingsProxyUseCredentials
+            ).forEach(viewThemeUtils::colorSwitchPreference)
+        }
     }
 
     private fun themeCategories() {

+ 45 - 0
app/src/main/java/com/nextcloud/talk/ui/theme/ViewThemeUtils.kt

@@ -24,13 +24,19 @@ package com.nextcloud.talk.ui.theme
 import android.content.Context
 import android.content.res.ColorStateList
 import android.content.res.Configuration
+import android.graphics.Color
 import android.view.View
 import android.widget.ImageView
 import android.widget.TextView
+import androidx.appcompat.widget.SwitchCompat
+import androidx.core.content.res.ResourcesCompat
+import androidx.core.view.children
 import com.google.android.material.button.MaterialButton
 import com.google.android.material.card.MaterialCardView
 import com.google.android.material.floatingactionbutton.FloatingActionButton
+import com.nextcloud.talk.R
 import com.yarolegovich.mp.MaterialPreferenceCategory
+import com.yarolegovich.mp.MaterialSwitchPreference
 import javax.inject.Inject
 
 class ViewThemeUtils @Inject constructor(val theme: ServerTheme) {
@@ -121,4 +127,43 @@ class ViewThemeUtils @Inject constructor(val theme: ServerTheme) {
             category.setTitleColor(color)
         }
     }
+
+    fun colorSwitchPreference(preference: MaterialSwitchPreference) {
+        val children = preference.children
+        val switch = children.find { it is SwitchCompat }
+        if (switch != null) {
+            val switchCompat = (switch as SwitchCompat)
+            colorSwitchCompat(switchCompat)
+        }
+    }
+
+    // TODO cleanup
+    fun colorSwitchCompat(switchCompat: SwitchCompat) {
+        withElementColor(switchCompat) { color ->
+
+            val context = switchCompat.context
+
+            val thumbUncheckedColor = ResourcesCompat.getColor(
+                context.resources,
+                R.color.switch_thumb_color_unchecked,
+                context.theme
+            )
+            val trackUncheckedColor = ResourcesCompat.getColor(
+                context.resources,
+                R.color.switch_track_color_unchecked,
+                context.theme
+            )
+
+            val trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color))
+            switchCompat.thumbTintList = ColorStateList(
+                arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()),
+                intArrayOf(color, thumbUncheckedColor)
+            )
+
+            switchCompat.trackTintList = ColorStateList(
+                arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()),
+                intArrayOf(trackColor, trackUncheckedColor)
+            )
+        }
+    }
 }

+ 4 - 0
app/src/main/res/values-night/colors.xml

@@ -76,4 +76,8 @@
 
     <color name="dialog_background">#353535</color>
     <color name="vote_dialog_background">#424242</color>
+
+    <color name="switch_thumb_color_unchecked">#cbcbcb</color>
+    <color name="switch_track_color_unchecked">#5a5a5a</color>
+
 </resources>

+ 3 - 0
app/src/main/res/values/colors.xml

@@ -111,4 +111,7 @@
     <color name="dialog_background">#FFFFFF</color>
     <color name="vote_dialog_background">#FFFFFF</color>
 
+    <color name="switch_thumb_color_unchecked">#ececec</color>
+    <color name="switch_track_color_unchecked">#b2b2b2</color>
+
 </resources>