From d12c63f149968d9c70f78f87adbc45fb7df59155 Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Tue, 1 May 2018 20:46:54 +0200
Subject: [PATCH] Reuse theming logic for notification sender colors

Signed-off-by: Janne Koschinski <janne@kuschku.de>
---
 .../service/QuasselNotificationBackend.kt     | 56 ++++++++++++++-----
 app/src/main/res/values/colors.xml            | 17 ------
 2 files changed, 43 insertions(+), 30 deletions(-)

diff --git a/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt b/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt
index 7a7dcf937..b625227ee 100644
--- a/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt
+++ b/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt
@@ -20,6 +20,7 @@
 package de.kuschku.quasseldroid.service
 
 import android.content.Context
+import android.support.annotation.ColorInt
 import android.text.SpannableStringBuilder
 import de.kuschku.libquassel.protocol.*
 import de.kuschku.libquassel.quassel.BufferInfo
@@ -32,14 +33,15 @@ import de.kuschku.libquassel.util.irc.HostmaskHelper
 import de.kuschku.quasseldroid.GlideApp
 import de.kuschku.quasseldroid.R
 import de.kuschku.quasseldroid.persistence.QuasselDatabase
+import de.kuschku.quasseldroid.settings.AppearanceSettings
 import de.kuschku.quasseldroid.settings.MessageSettings
 import de.kuschku.quasseldroid.settings.NotificationSettings
 import de.kuschku.quasseldroid.settings.Settings
 import de.kuschku.quasseldroid.util.AvatarHelper
 import de.kuschku.quasseldroid.util.NotificationMessage
 import de.kuschku.quasseldroid.util.QuasseldroidNotificationManager
-import de.kuschku.quasseldroid.util.helper.getColorCompat
 import de.kuschku.quasseldroid.util.helper.loadWithFallbacks
+import de.kuschku.quasseldroid.util.helper.styledAttributes
 import de.kuschku.quasseldroid.util.irc.format.ContentFormatter
 import de.kuschku.quasseldroid.util.ui.TextDrawable
 import javax.inject.Inject
@@ -50,21 +52,33 @@ class QuasselNotificationBackend @Inject constructor(
   private val contentFormatter: ContentFormatter,
   private val notificationHandler: QuasseldroidNotificationManager
 ) : NotificationManager {
-  private val senderColors = listOf(
-    R.color.senderColor0, R.color.senderColor1, R.color.senderColor2, R.color.senderColor3,
-    R.color.senderColor4, R.color.senderColor5, R.color.senderColor6, R.color.senderColor7,
-    R.color.senderColor8, R.color.senderColor9, R.color.senderColorA, R.color.senderColorB,
-    R.color.senderColorC, R.color.senderColorD, R.color.senderColorE, R.color.senderColorF
-  ).map(context::getColorCompat).toIntArray()
+  private var notificationSettings: NotificationSettings
+  private var appearanceSettings: AppearanceSettings
+  private var messageSettings: MessageSettings
 
-  private lateinit var notificationSettings: NotificationSettings
-
-  private lateinit var messageSettings: MessageSettings
-
-  private val selfColor = context.getColorCompat(android.R.color.background_dark)
+  @ColorInt
+  private var selfColor: Int
+  private var senderColors: IntArray
 
   init {
-    updateSettings()
+    notificationSettings = Settings.notification(context)
+    appearanceSettings = Settings.appearance(context)
+    messageSettings = Settings.message(context)
+
+    context.setTheme(AppearanceSettings.DEFAULT.theme.style)
+    senderColors = context.theme.styledAttributes(
+      R.attr.senderColor0, R.attr.senderColor1, R.attr.senderColor2, R.attr.senderColor3,
+      R.attr.senderColor4, R.attr.senderColor5, R.attr.senderColor6, R.attr.senderColor7,
+      R.attr.senderColor8, R.attr.senderColor9, R.attr.senderColorA, R.attr.senderColorB,
+      R.attr.senderColorC, R.attr.senderColorD, R.attr.senderColorE, R.attr.senderColorF
+    ) {
+      IntArray(16) {
+        getColor(it, 0)
+      }
+    }
+    selfColor = context.theme.styledAttributes(R.attr.colorForegroundSecondary) {
+      getColor(0, 0)
+    }
   }
 
   override fun init(session: Session) {
@@ -112,7 +126,23 @@ class QuasselNotificationBackend @Inject constructor(
 
   fun updateSettings() {
     notificationSettings = Settings.notification(context)
+    appearanceSettings = Settings.appearance(context)
     messageSettings = Settings.message(context)
+
+    context.setTheme(AppearanceSettings.DEFAULT.theme.style)
+    senderColors = context.theme.styledAttributes(
+      R.attr.senderColor0, R.attr.senderColor1, R.attr.senderColor2, R.attr.senderColor3,
+      R.attr.senderColor4, R.attr.senderColor5, R.attr.senderColor6, R.attr.senderColor7,
+      R.attr.senderColor8, R.attr.senderColor9, R.attr.senderColorA, R.attr.senderColorB,
+      R.attr.senderColorC, R.attr.senderColorD, R.attr.senderColorE, R.attr.senderColorF
+    ) {
+      IntArray(16) {
+        getColor(it, 0)
+      }
+    }
+    selfColor = context.theme.styledAttributes(R.attr.colorForegroundSecondary) {
+      getColor(0, 0)
+    }
   }
 
   @Synchronized
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
index 994f06dc2..ef2e68b51 100644
--- a/app/src/main/res/values/colors.xml
+++ b/app/src/main/res/values/colors.xml
@@ -40,23 +40,6 @@
   <color name="ripple_dark">#33ffffff</color>
   <color name="ripple_light">#1f000000</color>
 
-  <color name="senderColor0">#F44336</color>
-  <color name="senderColor1">#2196F3</color>
-  <color name="senderColor2">#7CB342</color>
-  <color name="senderColor3">#7B1FA2</color>
-  <color name="senderColor4">#DA8E00</color>
-  <color name="senderColor5">#4CAF50</color>
-  <color name="senderColor6">#3F51B5</color>
-  <color name="senderColor7">#E91E63</color>
-  <color name="senderColor8">#b94600</color>
-  <color name="senderColor9">#9E9D24</color>
-  <color name="senderColorA">#558B2F</color>
-  <color name="senderColorB">#009688</color>
-  <color name="senderColorC">#0277BD</color>
-  <color name="senderColorD">#00838F</color>
-  <color name="senderColorE">#9C27B0</color>
-  <color name="senderColorF">#C51162</color>
-
   <!-- mirc colors -->
   <color name="mircColor00">#ffffff</color>
   <color name="mircColor01">#000000</color>
-- 
GitLab