Skip to content

Commit 69e17fc

Browse files
authored
feat: handle unstyled darkmode on Android (#305)
1 parent 315fd51 commit 69e17fc

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

.changeset/shaggy-fishes-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-native-bottom-tabs': patch
3+
---
4+
5+
feat: handle automatic darkmode changes on Android

packages/react-native-bottom-tabs/android/src/main/java/com/rcttabview/RCTTabView.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.rcttabview
33
import android.annotation.SuppressLint
44
import android.content.Context
55
import android.content.res.ColorStateList
6+
import android.content.res.Configuration
67
import android.graphics.drawable.ColorDrawable
78
import android.graphics.drawable.Drawable
89
import android.os.Build
@@ -34,7 +35,7 @@ import com.google.android.material.navigation.NavigationBarView.LABEL_VISIBILITY
3435
import com.google.android.material.transition.platform.MaterialFadeThrough
3536

3637
class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
37-
private val bottomNavigation = BottomNavigationView(context)
38+
private var bottomNavigation = BottomNavigationView(context)
3839
val layoutHolder = FrameLayout(context)
3940

4041
var onTabSelectedListener: ((key: String) -> Unit)? = null
@@ -56,6 +57,8 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
5657
private var fontFamily: String? = null
5758
private var fontWeight: Int? = null
5859
private var lastReportedSize: Size? = null
60+
private var hasCustomAppearance = false
61+
private var uiModeConfiguration: Int = Configuration.UI_MODE_NIGHT_UNDEFINED
5962

6063
private val imageLoader = ImageLoader.Builder(context)
6164
.components {
@@ -78,6 +81,7 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
7881
LayoutParams.MATCH_PARENT,
7982
LayoutParams.WRAP_CONTENT
8083
))
84+
uiModeConfiguration = resources.configuration.uiMode
8185

8286
post {
8387
addOnLayoutChangeListener { _, left, top, right, bottom,
@@ -306,6 +310,7 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
306310

307311
fun setRippleColor(color: ColorStateList) {
308312
bottomNavigation.itemRippleColor = color
313+
hasCustomAppearance = true
309314
}
310315

311316
@SuppressLint("CheckResult")
@@ -343,20 +348,24 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
343348

344349
bottomNavigation.itemBackground = colorDrawable
345350
bottomNavigation.backgroundTintList = ColorStateList.valueOf(backgroundColor)
351+
hasCustomAppearance = true
346352
}
347353

348354
fun setActiveTintColor(color: Int?) {
349355
activeTintColor = color
350356
updateTintColors()
357+
hasCustomAppearance = true
351358
}
352359

353360
fun setInactiveTintColor(color: Int?) {
354361
inactiveTintColor = color
355362
updateTintColors()
363+
hasCustomAppearance = true
356364
}
357365

358366
fun setActiveIndicatorColor(color: ColorStateList) {
359367
bottomNavigation.itemActiveIndicatorColor = color
368+
hasCustomAppearance = true
360369
}
361370

362371
fun setFontSize(size: Int) {
@@ -431,6 +440,22 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
431440
}
432441
}
433442

443+
override fun onConfigurationChanged(newConfig: Configuration?) {
444+
super.onConfigurationChanged(newConfig)
445+
if (uiModeConfiguration == newConfig?.uiMode || hasCustomAppearance) {
446+
return
447+
}
448+
449+
// If appearance wasn't changed re-create the bottom navigation view when configuration changes.
450+
// React Native opts out ouf Activity re-creation when configuration changes, this workarounds that.
451+
// We also opt-out of this recreation when custom styles are used.
452+
removeView(bottomNavigation)
453+
bottomNavigation = BottomNavigationView(context)
454+
addView(bottomNavigation)
455+
updateItems(items)
456+
uiModeConfiguration = newConfig?.uiMode ?: uiModeConfiguration
457+
}
458+
434459
override fun onDetachedFromWindow() {
435460
super.onDetachedFromWindow()
436461
imageLoader.shutdown()

0 commit comments

Comments
 (0)