@@ -4,8 +4,10 @@ import android.content.Context
4
4
import android.content.res.ColorStateList
5
5
import android.graphics.drawable.BitmapDrawable
6
6
import android.graphics.drawable.Drawable
7
+ import android.util.TypedValue
7
8
import android.view.Choreographer
8
9
import android.view.MenuItem
10
+ import androidx.appcompat.content.res.AppCompatResources
9
11
import com.facebook.common.references.CloseableReference
10
12
import com.facebook.datasource.DataSources
11
13
import com.facebook.drawee.backends.pipeline.Fresco
@@ -27,6 +29,10 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
27
29
private var isAnimating = false
28
30
private var activeTintColor: Int? = null
29
31
private var inactiveTintColor: Int? = null
32
+ private val checkedStateSet = intArrayOf(android.R .attr.state_checked)
33
+ private val uncheckedStateSet = intArrayOf(- android.R .attr.state_checked)
34
+ private val disabledStateSet = intArrayOf(- android.R .attr.state_enabled)
35
+
30
36
31
37
private val layoutCallback = Choreographer .FrameCallback {
32
38
isLayoutEnqueued = false
@@ -143,22 +149,7 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
143
149
isAnimating = false
144
150
}
145
151
146
- private fun updateTintColors () {
147
- val activeColor = activeTintColor ? : return
148
- val inactiveColor = inactiveTintColor ? : return
149
-
150
- val states = arrayOf(
151
- intArrayOf(- android.R .attr.state_checked),
152
- intArrayOf(android.R .attr.state_checked)
153
- )
154
152
155
- val colors = intArrayOf(inactiveColor, activeColor)
156
-
157
- ColorStateList (states, colors).apply {
158
- this @ReactBottomNavigationView.itemTextColor = this
159
- this @ReactBottomNavigationView.itemIconTintList = this
160
- }
161
- }
162
153
163
154
fun setActiveTintColor (color : Int? ) {
164
155
activeTintColor = color
@@ -169,4 +160,29 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
169
160
inactiveTintColor = color
170
161
updateTintColors()
171
162
}
163
+
164
+ private fun updateTintColors () {
165
+ // getDeaultColor will always return a valid color but to satisfy the compiler we need to check for null
166
+ val colorPrimary = activeTintColor ? : getDefaultColorFor(android.R .attr.colorPrimary) ? : return
167
+ val colorSecondary =
168
+ inactiveTintColor ? : getDefaultColorFor(android.R .attr.textColorSecondary) ? : return
169
+ val states = arrayOf(uncheckedStateSet, checkedStateSet)
170
+ val colors = intArrayOf(colorSecondary, colorPrimary)
171
+
172
+ ColorStateList (states, colors).apply {
173
+ this @ReactBottomNavigationView.itemTextColor = this
174
+ this @ReactBottomNavigationView.itemIconTintList = this
175
+ }
176
+ }
177
+
178
+ private fun getDefaultColorFor (baseColorThemeAttr : Int ): Int? {
179
+ val value = TypedValue ()
180
+ if (! context.theme.resolveAttribute(baseColorThemeAttr, value, true )) {
181
+ return null
182
+ }
183
+ val baseColor = AppCompatResources .getColorStateList(
184
+ context, value.resourceId
185
+ )
186
+ return baseColor.defaultColor
187
+ }
172
188
}
0 commit comments