@@ -3,6 +3,7 @@ package com.rcttabview
3
3
import android.annotation.SuppressLint
4
4
import android.content.Context
5
5
import android.content.res.ColorStateList
6
+ import android.content.res.Configuration
6
7
import android.graphics.drawable.ColorDrawable
7
8
import android.graphics.drawable.Drawable
8
9
import android.os.Build
@@ -34,7 +35,7 @@ import com.google.android.material.navigation.NavigationBarView.LABEL_VISIBILITY
34
35
import com.google.android.material.transition.platform.MaterialFadeThrough
35
36
36
37
class ReactBottomNavigationView (context : Context ) : LinearLayout(context) {
37
- private val bottomNavigation = BottomNavigationView (context)
38
+ private var bottomNavigation = BottomNavigationView (context)
38
39
val layoutHolder = FrameLayout (context)
39
40
40
41
var onTabSelectedListener: ((key: String ) -> Unit )? = null
@@ -56,6 +57,8 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
56
57
private var fontFamily: String? = null
57
58
private var fontWeight: Int? = null
58
59
private var lastReportedSize: Size ? = null
60
+ private var hasCustomAppearance = false
61
+ private var uiModeConfiguration: Int = Configuration .UI_MODE_NIGHT_UNDEFINED
59
62
60
63
private val imageLoader = ImageLoader .Builder (context)
61
64
.components {
@@ -78,6 +81,7 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
78
81
LayoutParams .MATCH_PARENT ,
79
82
LayoutParams .WRAP_CONTENT
80
83
))
84
+ uiModeConfiguration = resources.configuration.uiMode
81
85
82
86
post {
83
87
addOnLayoutChangeListener { _, left, top, right, bottom,
@@ -306,6 +310,7 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
306
310
307
311
fun setRippleColor (color : ColorStateList ) {
308
312
bottomNavigation.itemRippleColor = color
313
+ hasCustomAppearance = true
309
314
}
310
315
311
316
@SuppressLint(" CheckResult" )
@@ -343,20 +348,24 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
343
348
344
349
bottomNavigation.itemBackground = colorDrawable
345
350
bottomNavigation.backgroundTintList = ColorStateList .valueOf(backgroundColor)
351
+ hasCustomAppearance = true
346
352
}
347
353
348
354
fun setActiveTintColor (color : Int? ) {
349
355
activeTintColor = color
350
356
updateTintColors()
357
+ hasCustomAppearance = true
351
358
}
352
359
353
360
fun setInactiveTintColor (color : Int? ) {
354
361
inactiveTintColor = color
355
362
updateTintColors()
363
+ hasCustomAppearance = true
356
364
}
357
365
358
366
fun setActiveIndicatorColor (color : ColorStateList ) {
359
367
bottomNavigation.itemActiveIndicatorColor = color
368
+ hasCustomAppearance = true
360
369
}
361
370
362
371
fun setFontSize (size : Int ) {
@@ -431,6 +440,22 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
431
440
}
432
441
}
433
442
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
+
434
459
override fun onDetachedFromWindow () {
435
460
super .onDetachedFromWindow()
436
461
imageLoader.shutdown()
0 commit comments