Browse Source

ViewThemeUtils: move things that do not belong in this class

Color calculations go to ColorUtil, checking if dark theme goes to PlatformThemeUtil

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey 2 years ago
parent
commit
4abc3839e5

+ 1 - 0
app/src/main/java/com/nextcloud/talk/ui/theme/ServerThemeImpl.kt

@@ -25,6 +25,7 @@ package com.nextcloud.talk.ui.theme
 
 import com.nextcloud.talk.R
 import com.nextcloud.talk.models.json.capabilities.ThemingCapability
+import com.nextcloud.talk.utils.ui.ColorUtil
 
 internal class ServerThemeImpl(themingCapability: ThemingCapability?, colorUtil: ColorUtil) :
     ServerTheme {

+ 1 - 0
app/src/main/java/com/nextcloud/talk/ui/theme/ServerThemeProviderImpl.kt

@@ -26,6 +26,7 @@ package com.nextcloud.talk.ui.theme
 import com.nextcloud.talk.data.user.model.User
 import com.nextcloud.talk.models.json.capabilities.Capabilities
 import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
+import com.nextcloud.talk.utils.ui.ColorUtil
 import java.util.concurrent.ConcurrentHashMap
 import javax.inject.Inject
 

+ 13 - 28
app/src/main/java/com/nextcloud/talk/ui/theme/ViewThemeUtils.kt

@@ -23,7 +23,6 @@ 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.graphics.PorterDuff
 import android.graphics.drawable.Drawable
@@ -40,7 +39,6 @@ import androidx.appcompat.content.res.AppCompatResources
 import androidx.appcompat.widget.SwitchCompat
 import androidx.core.content.ContextCompat
 import androidx.core.content.res.ResourcesCompat
-import androidx.core.graphics.ColorUtils
 import androidx.core.view.children
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
 import com.google.android.material.button.MaterialButton
@@ -52,19 +50,13 @@ import com.google.android.material.tabs.TabLayout
 import com.google.android.material.textfield.TextInputLayout
 import com.nextcloud.talk.R
 import com.nextcloud.talk.utils.DrawableUtils
+import com.nextcloud.talk.utils.ui.ColorUtil
+import com.nextcloud.talk.utils.ui.PlatformThemeUtil.isDarkMode
 import com.yarolegovich.mp.MaterialPreferenceCategory
 import com.yarolegovich.mp.MaterialSwitchPreference
 import javax.inject.Inject
 
-class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
-
-    private fun isDarkMode(context: Context): Boolean = when (
-        context.resources.configuration.uiMode and
-            Configuration.UI_MODE_NIGHT_MASK
-    ) {
-        Configuration.UI_MODE_NIGHT_YES -> true
-        else -> false
-    }
+class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private val colorUtil: ColorUtil) {
 
     /**
      * Color for painting elements
@@ -266,6 +258,14 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
         }
     }
 
+    private fun progressColor(context: Context, color: Int): Int {
+        val lightness = when (isDarkMode(context)) {
+            true -> PROGRESS_LIGHTNESS_DARK_THEME
+            false -> PROGRESS_LIGHTNESS_LIGHT_THEME
+        }
+        return colorUtil.setLightness(color, lightness)
+    }
+
     fun colorEditText(editText: EditText) {
         withElementColor(editText) { color ->
             editText.setTextColor(color)
@@ -351,28 +351,13 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
         }
     }
 
-    private fun progressColor(context: Context, color: Int): Int {
-        val hsl = FloatArray(HSL_SIZE)
-        ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
-
-        if (isDarkMode(context)) {
-            hsl[INDEX_LIGHTNESS] = LIGHTNESS_DARK_THEME
-        } else {
-            hsl[INDEX_LIGHTNESS] = LIGHTNESS_LIGHT_THEME
-        }
-
-        return ColorUtils.HSLToColor(hsl)
-    }
-
     companion object {
         private val THEMEABLE_PLACEHOLDER_IDS = listOf(
             R.drawable.ic_mimetype_package_x_generic,
             R.drawable.ic_mimetype_folder
         )
         private const val TRACK_ALPHA: Int = 77
-        private const val HSL_SIZE: Int = 3
-        private const val INDEX_LIGHTNESS: Int = 2
-        private const val LIGHTNESS_LIGHT_THEME: Float = 0.76f
-        private const val LIGHTNESS_DARK_THEME: Float = 0.28f
+        private const val PROGRESS_LIGHTNESS_LIGHT_THEME: Float = 0.76f
+        private const val PROGRESS_LIGHTNESS_DARK_THEME: Float = 0.28f
     }
 }

+ 17 - 6
app/src/main/java/com/nextcloud/talk/ui/theme/ColorUtil.kt → app/src/main/java/com/nextcloud/talk/utils/ui/ColorUtil.kt

@@ -1,23 +1,24 @@
 /*
  * Nextcloud Talk application
  *
- * @author Andy Scherzinger
- * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
+ * @author Álvaro Brey
+ * Copyright (C) 2022 Álvaro Brey
+ * Copyright (C) 2022 Nextcloud GmbH
  *
  * 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.
+ * (at your option) any later version.
  *
  * This program 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
+ * 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
  */
-package com.nextcloud.talk.ui.theme
+package com.nextcloud.talk.utils.ui
 
 import android.content.Context
 import android.graphics.Color
@@ -57,6 +58,16 @@ class ColorUtil @Inject constructor(private val context: Context) {
         }
     }
 
+    fun setLightness(@ColorInt color: Int, lightness: Float): Int {
+        require(lightness in 0.0..1.0) { "Lightness must be between 0 and 1" }
+        val hsl = FloatArray(HSL_SIZE)
+        ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
+
+        hsl[INDEX_LIGHTNESS] = lightness
+
+        return ColorUtils.HSLToColor(hsl)
+    }
+
     @ColorInt
     private fun String?.parseColorOrFallback(fallback: () -> Int): Int {
         return this?.let { Color.parseColor(this) } ?: fallback()

+ 33 - 0
app/src/main/java/com/nextcloud/talk/utils/ui/PlatformThemeUtil.kt

@@ -0,0 +1,33 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Álvaro Brey
+ * Copyright (C) 2022 Álvaro Brey
+ * Copyright (C) 2022 Nextcloud GmbH
+ *
+ * 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.utils.ui
+
+import android.content.Context
+import android.content.res.Configuration
+
+object PlatformThemeUtil {
+    fun isDarkMode(context: Context): Boolean =
+        when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
+            Configuration.UI_MODE_NIGHT_YES -> true
+            else -> false
+        }
+}