Skip to content

Commit 2cfeb0a

Browse files
committed
Make adaptive color scheme opt in, default to website colors
1 parent 70838dc commit 2cfeb0a

39 files changed

Lines changed: 184 additions & 8 deletions

File tree

app/src/main/kotlin/settings/SettingsFragment.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class SettingsFragment : Fragment() {
3333

3434
initMapStyleButton()
3535

36+
binding.useAdaptiveColors.isChecked = prefs.useAdaptiveColors
37+
binding.useAdaptiveColors.setOnCheckedChangeListener { _, isChecked ->
38+
prefs.useAdaptiveColors = isChecked
39+
refreshAllColors()
40+
}
41+
3642
initMarkerBackgroundButton()
3743
initMarkerIconButton()
3844

@@ -290,4 +296,28 @@ class SettingsFragment : Fragment() {
290296
}
291297
}
292298
}
299+
300+
private fun refreshAllColors() {
301+
binding.markerBackgroundColor.text =
302+
"#${prefs.markerBackgroundColor(requireContext()).toHexString()}"
303+
binding.markerBackgroundColor.setTextColor(prefs.markerBackgroundColor(requireContext()))
304+
binding.markerIconColor.text =
305+
"#${prefs.markerIconColor(requireContext()).toHexString()}"
306+
binding.markerIconColor.setTextColor(prefs.markerIconColor(requireContext()))
307+
binding.badgeBackgroundColor.text =
308+
"#${prefs.badgeBackgroundColor(requireContext()).toHexString()}"
309+
binding.badgeBackgroundColor.setTextColor(prefs.badgeBackgroundColor(requireContext()))
310+
binding.badgeTextColor.text =
311+
"#${prefs.badgeTextColor(requireContext()).toHexString()}"
312+
binding.badgeTextColor.setTextColor(prefs.badgeTextColor(requireContext()))
313+
binding.buttonBackgroundColor.text =
314+
"#${prefs.buttonBackgroundColor(requireContext()).toHexString()}"
315+
binding.buttonBackgroundColor.setTextColor(prefs.buttonBackgroundColor(requireContext()))
316+
binding.buttonIconColor.text =
317+
"#${prefs.buttonIconColor(requireContext()).toHexString()}"
318+
binding.buttonIconColor.setTextColor(prefs.buttonIconColor(requireContext()))
319+
binding.buttonBorderColor.text =
320+
"#${prefs.buttonBorderColor(requireContext()).toHexString()}"
321+
binding.buttonBorderColor.setTextColor(prefs.buttonBorderColor(requireContext()))
322+
}
293323
}

app/src/main/kotlin/settings/SharedPreferencesExt.kt

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,28 @@ fun MapStyle.uri(context: Context): String {
114114
}
115115
}
116116

117+
fun SharedPreferences.mapStyleIsDark(): Boolean {
118+
return when (mapStyle) {
119+
MapStyle.Dark, MapStyle.CartoDarkMatter -> true
120+
MapStyle.Auto -> {
121+
false
122+
}
123+
else -> false
124+
}
125+
}
126+
117127
fun SharedPreferences.markerBackgroundColor(context: Context): Int {
118-
return getInt("markerBackgroundColor", context.getPrimaryContainerColor())
128+
val customColor = getInt("markerBackgroundColor", -1)
129+
if (customColor != -1) return customColor
130+
131+
if (useAdaptiveColors) {
132+
return context.getPrimaryContainerColor()
133+
}
134+
135+
val isDark = mapStyleIsDark() ||
136+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
137+
138+
return if (isDark) 0xFF0e95af.toInt() else 0xFF0e95af.toInt()
119139
}
120140

121141
fun SharedPreferences.setMarkerBackgroundColor(color: Int?) {
@@ -151,7 +171,17 @@ fun SharedPreferences.setBoostedMarkerBackgroundColor(color: Int?) {
151171
}
152172

153173
fun SharedPreferences.markerIconColor(context: Context): Int {
154-
return getInt("markerIconColor", context.getOnPrimaryContainerColor())
174+
val customColor = getInt("markerIconColor", -1)
175+
if (customColor != -1) return customColor
176+
177+
if (useAdaptiveColors) {
178+
return context.getOnPrimaryContainerColor()
179+
}
180+
181+
val isDark = mapStyleIsDark() ||
182+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
183+
184+
return if (isDark) 0xFFFFFFFF.toInt() else 0xFFFFFFFF.toInt() // White for both
155185
}
156186

157187
fun SharedPreferences.setMarkerIconColor(color: Int?) {
@@ -168,7 +198,17 @@ fun SharedPreferences.setMarkerIconColor(color: Int?) {
168198
}
169199

170200
fun SharedPreferences.badgeBackgroundColor(context: Context): Int {
171-
return getInt("badgeBackgroundColor", context.getOnPrimaryContainerColor())
201+
val customColor = getInt("badgeBackgroundColor", -1)
202+
if (customColor != -1) return customColor
203+
204+
if (useAdaptiveColors) {
205+
return context.getOnPrimaryContainerColor()
206+
}
207+
208+
val isDark = mapStyleIsDark() ||
209+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
210+
211+
return if (isDark) 0xFF00a63e.toInt() else 0xFF00a63e.toInt()
172212
}
173213

174214
fun SharedPreferences.setBadgeBackgroundColor(color: Int?) {
@@ -185,7 +225,17 @@ fun SharedPreferences.setBadgeBackgroundColor(color: Int?) {
185225
}
186226

187227
fun SharedPreferences.badgeTextColor(context: Context): Int {
188-
return getInt("badgeTextColor", context.getPrimaryContainerColor())
228+
val customColor = getInt("badgeTextColor", -1)
229+
if (customColor != -1) return customColor
230+
231+
if (useAdaptiveColors) {
232+
return context.getPrimaryContainerColor()
233+
}
234+
235+
val isDark = mapStyleIsDark() ||
236+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
237+
238+
return if (isDark) 0xFFFFFFFF.toInt() else 0xFFFFFFFF.toInt()
189239
}
190240

191241
fun SharedPreferences.setBadgeTextColor(color: Int?) {
@@ -224,7 +274,17 @@ fun SharedPreferences.apiUrlV4(vararg pathSegments: String): HttpUrl {
224274
private const val KEY_BUTTON_BACKGROUND_COLOR = "buttonBackgroundColor"
225275

226276
fun SharedPreferences.buttonBackgroundColor(context: Context): Int {
227-
return getInt(KEY_BUTTON_BACKGROUND_COLOR, context.getTertiaryContainerColor())
277+
val customColor = getInt(KEY_BUTTON_BACKGROUND_COLOR, -1)
278+
if (customColor != -1) return customColor
279+
280+
if (useAdaptiveColors) {
281+
return context.getTertiaryContainerColor()
282+
}
283+
284+
val isDark = mapStyleIsDark() ||
285+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
286+
287+
return if (isDark) 0xFF1f2937.toInt() else 0xFF1f2937.toInt()
228288
}
229289

230290
fun SharedPreferences.setButtonBackgroundColor(color: Int?) {
@@ -243,7 +303,17 @@ fun SharedPreferences.setButtonBackgroundColor(color: Int?) {
243303
private const val KEY_BUTTON_ICON_COLOR = "buttonIconColor"
244304

245305
fun SharedPreferences.buttonIconColor(context: Context): Int {
246-
return getInt(KEY_BUTTON_ICON_COLOR, context.getOnTertiaryContainerColor())
306+
val customColor = getInt(KEY_BUTTON_ICON_COLOR, -1)
307+
if (customColor != -1) return customColor
308+
309+
if (useAdaptiveColors) {
310+
return context.getOnTertiaryContainerColor()
311+
}
312+
313+
val isDark = mapStyleIsDark() ||
314+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
315+
316+
return if (isDark) 0xFFFFFFFF.toInt() else 0xFFFFFFFF.toInt()
247317
}
248318

249319
fun SharedPreferences.setButtonIconColor(color: Int?) {
@@ -261,8 +331,24 @@ fun SharedPreferences.setButtonIconColor(color: Int?) {
261331

262332
private const val KEY_BUTTON_BORDER_COLOR = "buttonBorderColor"
263333

334+
var SharedPreferences.useAdaptiveColors: Boolean
335+
get() = getBoolean("useAdaptiveColors", false)
336+
set(value) {
337+
edit { putBoolean("useAdaptiveColors", value) }
338+
}
339+
264340
fun SharedPreferences.buttonBorderColor(context: Context): Int {
265-
return getInt(KEY_BUTTON_BORDER_COLOR, context.getOnTertiaryContainerColor())
341+
val customColor = getInt(KEY_BUTTON_BORDER_COLOR, -1)
342+
if (customColor != -1) return customColor
343+
344+
if (useAdaptiveColors) {
345+
return context.getOnTertiaryContainerColor()
346+
}
347+
348+
val isDark = mapStyleIsDark() ||
349+
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
350+
351+
return if (isDark) 0xFFFFFFFF.toInt() else 0xFFFFFFFF.toInt()
266352
}
267353

268354
fun SharedPreferences.setButtonBorderColor(color: Int?) {
@@ -276,4 +362,4 @@ fun SharedPreferences.setButtonBorderColor(color: Int?) {
276362
)
277363
}
278364
}
279-
}
365+
}

app/src/main/res/layout/settings_fragment.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@
6363

6464
</LinearLayout>
6565

66+
<LinearLayout
67+
android:layout_width="match_parent"
68+
android:layout_height="wrap_content"
69+
android:gravity="center_vertical"
70+
android:minHeight="64dp"
71+
android:orientation="horizontal"
72+
android:paddingStart="16dp"
73+
android:paddingEnd="16dp">
74+
75+
<TextView
76+
android:layout_width="0dp"
77+
android:layout_height="wrap_content"
78+
android:layout_weight="1"
79+
android:text="@string/use_adaptive_colors"
80+
android:textColor="?colorOnSurface"
81+
android:textSize="16sp" />
82+
83+
<com.google.android.material.materialswitch.MaterialSwitch
84+
android:id="@+id/useAdaptiveColors"
85+
android:layout_width="wrap_content"
86+
android:layout_height="wrap_content" />
87+
88+
</LinearLayout>
89+
6690
<LinearLayout
6791
android:id="@+id/changeMarkerBackgroundColor"
6892
android:layout_width="match_parent"

app/src/main/res/values-af/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">Gebruik aanpassingskleuren</string>
6465
<string name="reset">Reset</string>
6566
</resources>

app/src/main/res/values-ar/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">استخدم الألوان التكيفية</string>
6465
<string name="reset">Reset</string>
6566
</resources>

app/src/main/res/values-bg/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">Използвайте адаптивни цветове</string>
6465
<string name="reset">Reset</string>
6566
</resources>

app/src/main/res/values-bn/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">অভিযোজক রঙ ব্যবহার করুন</string>
6465
<string name="reset">Reset</string>
6566
</resources>

app/src/main/res/values-ca/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">Utilitza colors adaptatius</string>
6465
<string name="reset">Reset</string>
6566
</resources>

app/src/main/res/values-cs/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">Použít adaptivní barvy</string>
6465
<string name="reset">Reset</string>
6566
</resources>

app/src/main/res/values-da/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<string name="button_background">Button background</string>
6262
<string name="button_icon">Button icon</string>
6363
<string name="button_border">Button border</string>
64+
<string name="use_adaptive_colors">Brug adaptive farver</string>
6465
<string name="reset">Reset</string>
6566
</resources>

0 commit comments

Comments
 (0)