From a135f5b31277dc3d2b6681e998be39eb68b4266a Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Wed, 19 Jan 2022 22:57:07 -0500 Subject: [PATCH] Fix dark theme selected row color --- .../java/io/heckel/ntfy/ui/DetailAdapter.kt | 3 ++- .../java/io/heckel/ntfy/ui/MainAdapter.kt | 5 ++++- app/src/main/java/io/heckel/ntfy/util/Util.kt | 20 +++++++++++++++++++ app/src/main/res/values/colors.xml | 13 ++++++------ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt index 84f9426..16bb94f 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt @@ -92,7 +92,8 @@ class DetailAdapter(private val activity: Activity, private val repository: Repo tagsView.visibility = View.GONE } if (selected.contains(notification.id)) { - itemView.setBackgroundResource(R.color.primarySelectedRowColor); + val backgroundColor = if (isDarkThemeOn(context, repository)) R.color.primaryDarkSelectedRowColor else R.color.primaryLightSelectedRowColor + itemView.setBackgroundResource(backgroundColor); } renderPriority(context, notification) maybeRenderAttachment(context, notification) diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainAdapter.kt b/app/src/main/java/io/heckel/ntfy/ui/MainAdapter.kt index 2e11bc0..5e69616 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainAdapter.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainAdapter.kt @@ -5,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.appcompat.app.AppCompatDelegate import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView @@ -13,6 +14,7 @@ import io.heckel.ntfy.R import io.heckel.ntfy.db.ConnectionState import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Subscription +import io.heckel.ntfy.util.isDarkThemeOn import io.heckel.ntfy.util.topicShortUrl import java.text.DateFormat import java.util.* @@ -100,7 +102,8 @@ class MainAdapter(private val repository: Repository, private val onClick: (Subs itemView.setOnClickListener { onClick(subscription) } itemView.setOnLongClickListener { onLongClick(subscription); true } if (selected.contains(subscription.id)) { - itemView.setBackgroundResource(R.color.primarySelectedRowColor); + val backgroundColor = if (isDarkThemeOn(context, repository)) R.color.primaryDarkSelectedRowColor else R.color.primaryLightSelectedRowColor + itemView.setBackgroundResource(backgroundColor) } } } diff --git a/app/src/main/java/io/heckel/ntfy/util/Util.kt b/app/src/main/java/io/heckel/ntfy/util/Util.kt index 779e7f8..3cd8a9e 100644 --- a/app/src/main/java/io/heckel/ntfy/util/Util.kt +++ b/app/src/main/java/io/heckel/ntfy/util/Util.kt @@ -4,14 +4,18 @@ import android.animation.ArgbEvaluator import android.animation.ValueAnimator import android.content.Context import android.content.Intent +import android.content.res.Configuration +import android.content.res.Configuration.UI_MODE_NIGHT_YES import android.net.Uri import android.os.Build import android.os.PowerManager import android.provider.OpenableColumns import android.provider.Settings import android.view.Window +import androidx.appcompat.app.AppCompatDelegate import io.heckel.ntfy.R import io.heckel.ntfy.db.Notification +import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Subscription import java.security.SecureRandom import java.text.DateFormat @@ -200,3 +204,19 @@ fun isIgnoringBatteryOptimizations(context: Context): Boolean { return true } +// Returns true if dark mode is on, see https://stackoverflow.com/a/60761189/1440785 +fun Context.isDarkThemeOn(): Boolean { + return resources.configuration.uiMode and + Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES +} + +fun isDarkThemeOn(context: Context, repository: Repository): Boolean { + val darkMode = repository.getDarkMode() + if (darkMode == AppCompatDelegate.MODE_NIGHT_YES) { + return true + } + if (darkMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && context.isDarkThemeOn()) { + return true + } + return false +} diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index a0f0353..6ba7967 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,12 +1,13 @@ #338574 - #338574 - #2A6E60 - #FFFFFF - #EEEEEE #C30000 - #C30000 - #E10000 + + #338574 + #FFFFFF + #EEEEEE + + #2A6E60 + #444444