Skip to content

Commit 8892f11

Browse files
committed
Fixed crash when using raw color value for some attributes
1 parent bd68d2e commit 8892f11

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v3.2.2
2+
- Fixed crash when using raw color value for `icdIconColor` and `icdSelectedIconColor`.
3+
14
### v3.2.1
25
- Updated Kotlin version to 1.3.72.
36

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Library
2-
libVersion=3.2.1
2+
libVersion=3.2.2
33

44
# Icon packs
55
iconPackDefaultVersion=1.0.1

lib/src/main/kotlin/com/maltaisn/icondialog/IconDialog.kt

+15-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.annotation.SuppressLint
2020
import android.app.Dialog
2121
import android.content.Context
2222
import android.content.DialogInterface
23+
import android.content.res.TypedArray
2324
import android.graphics.Color
2425
import android.graphics.Rect
2526
import android.graphics.drawable.Drawable
@@ -32,7 +33,6 @@ import android.view.Window
3233
import android.view.inputmethod.EditorInfo
3334
import android.view.inputmethod.InputMethodManager
3435
import android.widget.*
35-
import androidx.annotation.ColorRes
3636
import androidx.appcompat.content.res.AppCompatResources
3737
import androidx.appcompat.view.ContextThemeWrapper
3838
import androidx.core.content.res.ResourcesCompat
@@ -120,8 +120,8 @@ class IconDialog : DialogFragment(), IconDialogContract.View {
120120
maxDialogWidth = it.getDimensionPixelSize(R.styleable.IconDialog_icdMaxWidth, -1)
121121
maxDialogHeight = it.getDimensionPixelSize(R.styleable.IconDialog_icdMaxHeight, -1)
122122
iconSize = it.getDimensionPixelSize(R.styleable.IconDialog_icdIconSize, -1)
123-
iconColorNormal = getColor(it.getResourceId(R.styleable.IconDialog_icdIconColor, 0))
124-
iconColorSelected = getColor(it.getResourceId(R.styleable.IconDialog_icdSelectedIconColor, 0))
123+
iconColorNormal = it.getColorCompat(R.styleable.IconDialog_icdIconColor)
124+
iconColorSelected = it.getColorCompat(R.styleable.IconDialog_icdSelectedIconColor)
125125
}
126126

127127
progressHandler = Handler()
@@ -220,8 +220,14 @@ class IconDialog : DialogFragment(), IconDialogContract.View {
220220
* Inflate color state list with compat library and return default color.
221221
* `ContextCompat.getColor` doesn't seem to use compat library.
222222
*/
223-
private fun getColor(@ColorRes color: Int) =
224-
AppCompatResources.getColorStateList(requireContext(), color).defaultColor
223+
private fun TypedArray.getColorCompat(index: Int): Int {
224+
val resId = this.getResourceId(index, 0)
225+
return if (resId == 0) {
226+
this.getColor(index, 0)
227+
} else {
228+
AppCompatResources.getColorStateList(requireContext(), resId).defaultColor
229+
}
230+
}
225231

226232
override fun onSaveInstanceState(state: Bundle) {
227233
super.onSaveInstanceState(state)
@@ -372,7 +378,8 @@ class IconDialog : DialogFragment(), IconDialogContract.View {
372378
}
373379

374380
override fun bindView(icon: Icon, selected: Boolean) {
375-
val drawable = DrawableCompat.wrap(icon.drawable ?: unavailableIconDrawable).mutate()
381+
val drawable = DrawableCompat.wrap(icon.drawable
382+
?: unavailableIconDrawable).mutate()
376383
iconImv.setImageDrawable(drawable)
377384
DrawableCompat.setTint(drawable, if (selected) iconColorSelected else iconColorNormal)
378385
if (icon.drawable != null) {
@@ -419,7 +426,8 @@ class IconDialog : DialogFragment(), IconDialogContract.View {
419426
override fun getItemId(pos: Int) = presenter?.getItemId(pos) ?: 0
420427
override fun getItemViewType(pos: Int) = presenter?.getItemType(pos) ?: 0
421428
override fun isHeader(pos: Int) = presenter?.isHeader(pos) == true
422-
override fun getHeaderPositionForItem(pos: Int) = presenter?.getHeaderPositionForItem(pos) ?: 0
429+
override fun getHeaderPositionForItem(pos: Int) = presenter?.getHeaderPositionForItem(pos)
430+
?: 0
423431
}
424432

425433
/**

lib/src/main/res/layout/icd_dialog_icon.xml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
app:layout_constraintStart_toStartOf="parent"
3939
app:layout_constraintTop_toTopOf="parent"
4040
app:layout_constraintVertical_chainStyle="spread_inside"
41+
tools:text="@string/icd_title"
4142
/>
4243

4344
<androidx.appcompat.widget.AppCompatImageView

lib/src/main/res/layout/icd_item_icon.xml

+2
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@
2525
tools:padding="8dp"
2626
tools:src="@drawable/icd_ic_unavailable"
2727
tools:ignore="ContentDescription"
28+
tools:tint="@color/material_on_surface_emphasis_medium"
29+
tools:tintMode="src_in"
2830
/>

0 commit comments

Comments
 (0)