Skip to content

Commit 3aa9bbf

Browse files
committed
feat(android): add default values for checked/unchecked states
1 parent 9fa21f9 commit 3aa9bbf

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

android/src/main/java/com/rcttabview/RCTTabView.kt

+31-15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import android.content.Context
44
import android.content.res.ColorStateList
55
import android.graphics.drawable.BitmapDrawable
66
import android.graphics.drawable.Drawable
7+
import android.util.TypedValue
78
import android.view.Choreographer
89
import android.view.MenuItem
10+
import androidx.appcompat.content.res.AppCompatResources
911
import com.facebook.common.references.CloseableReference
1012
import com.facebook.datasource.DataSources
1113
import com.facebook.drawee.backends.pipeline.Fresco
@@ -27,6 +29,10 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
2729
private var isAnimating = false
2830
private var activeTintColor: Int? = null
2931
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+
3036

3137
private val layoutCallback = Choreographer.FrameCallback {
3238
isLayoutEnqueued = false
@@ -143,22 +149,7 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
143149
isAnimating = false
144150
}
145151

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-
)
154152

155-
val colors = intArrayOf(inactiveColor, activeColor)
156-
157-
ColorStateList(states, colors).apply {
158-
this@ReactBottomNavigationView.itemTextColor = this
159-
this@ReactBottomNavigationView.itemIconTintList = this
160-
}
161-
}
162153

163154
fun setActiveTintColor(color: Int?) {
164155
activeTintColor = color
@@ -169,4 +160,29 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
169160
inactiveTintColor = color
170161
updateTintColors()
171162
}
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+
}
172188
}

0 commit comments

Comments
 (0)