Skip to content

Commit

Permalink
Add method to prevent default icon tint #83
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkeppeler committed Aug 29, 2021
1 parent 66d2f8d commit 8373c98
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
12 changes: 12 additions & 0 deletions options/src/main/java/com/maxkeppeler/sheets/options/Option.kt
Expand Up @@ -90,6 +90,9 @@ class Option internal constructor() {
internal var longClickListener: OptionLongClickListener? = null
private set

internal var preventIconTint: Boolean? = null
private set

constructor(@StringRes textRes: Int) : this() {
this.titleTextRes = textRes
}
Expand Down Expand Up @@ -259,6 +262,15 @@ class Option internal constructor() {
return this
}

/**
* Sheets applies by default a one-colored tint on the drawable representing an option.
* You can prevent this behavior in order to keep the original colors of the drawable.
*/
fun preventIconTint(preventIconTint: Boolean): Option {
this.preventIconTint = preventIconTint
return this
}

/** Set default icon color. */
fun defaultIconColor(@ColorInt color: Int): Option {
this.defaultIconColor = color
Expand Down
Expand Up @@ -22,6 +22,7 @@ import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.RippleDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -37,10 +38,11 @@ import com.maxkeppeler.sheets.options.databinding.SheetsOptionsListItemBinding
internal class OptionsAdapter(
private val ctx: Context,
private val options: MutableList<Option>,
private val globalPreventIconTint: Boolean?,
private val type: DisplayMode,
private val multipleChoice: Boolean,
private val collapsedItems: Boolean,
private val listener: OptionsSelectionListener
private val listener: OptionsSelectionListener,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

companion object {
Expand All @@ -49,7 +51,8 @@ internal class OptionsAdapter(
private const val SELECTOR_STATE_SELECTED_INDEX = 1
}

private val selectedOptions = mutableMapOf<Int, Triple<ImageView, SheetsContent, SheetsContent>>()
private val selectedOptions =
mutableMapOf<Int, Triple<ImageView, SheetsContent, SheetsContent>>()

private val iconsColor = getIconColor(ctx)
private val textColor = getTextColor(ctx)
Expand Down Expand Up @@ -189,7 +192,12 @@ internal class OptionsAdapter(
}
}

private fun showDisabled(title: SheetsContent, subtitle: SheetsContent, icon: ImageView, root: View) {
private fun showDisabled(
title: SheetsContent,
subtitle: SheetsContent,
icon: ImageView,
root: View,
) {
title.setTextColor(disabledTextColor)
subtitle.setTextColor(disabledTextColor)
icon.setColorFilter(disabledIconsColor)
Expand Down Expand Up @@ -240,13 +248,18 @@ internal class OptionsAdapter(
it)
} ?: textColor

val iconsColor =
defaultIconColor ?: defaultIconColorRes?.let { ContextCompat.getColor(ctx, it) }
?: iconsColor
val preventIconTint = option.preventIconTint ?: globalPreventIconTint
val iconsColor = defaultIconColor
?: defaultIconColorRes?.let { ContextCompat.getColor(ctx, it) }
?: takeUnless { preventIconTint == true }?.let { iconsColor }

title.setTextColor(titleColor)
subtitle.setTextColor(subtitleColor)
icon.setColorFilter(iconsColor)
iconsColor?.let { color ->
icon.setColorFilter(color)
} ?: run {
icon.clearColorFilter()
}
}
if (multipleChoice) {
root.isSelected = false
Expand All @@ -256,16 +269,18 @@ internal class OptionsAdapter(
private fun View.changeRippleAndStateColor(
rippleColor: Int = highlightColor,
stateIndex: Int = SELECTOR_STATE_SELECTED_INDEX,
stateBackgroundColor: Int = highlightColor
stateBackgroundColor: Int = highlightColor,
) {
// Ripple drawable
(background as RippleDrawable).apply {
setColor(ColorStateList.valueOf(rippleColor))
// Selector drawable
(getDrawable(1) as StateListDrawable).apply {
// Selected state drawable
(getStateDrawable(stateIndex) as GradientDrawable).apply {
color = ColorStateList.valueOf(stateBackgroundColor)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
(getStateDrawable(stateIndex) as GradientDrawable).apply {
color = ColorStateList.valueOf(stateBackgroundColor)
}
}
}
}
Expand Down Expand Up @@ -296,11 +311,11 @@ internal class OptionsAdapter(
showSelected(title, subtitle, icon, root)
}
} else {
selectedOptions.forEach {
showDeselected(index,
it.value.second,
it.value.third,
it.value.first,
selectedOptions.forEach { option ->
showDeselected(option.key,
option.value.second,
option.value.third,
option.value.first,
root)
}
selectedOptions.clear()
Expand Down
Expand Up @@ -76,6 +76,7 @@ class OptionsSheet : Sheet() {
private var maxChoicesStrict = true
private var displayButtons = false
private var gridColumns: Int? = null
private var preventIconTint: Boolean? = null

private val saveAllowed: Boolean
get() {
Expand Down Expand Up @@ -134,6 +135,15 @@ class OptionsSheet : Sheet() {
this.mode = displayMode
}

/**
* Sheets applies by default a one-colored tint on the various option drawables.
* You can prevent this behavior in order to keep the original colors of the drawables.
* You can override this global settings on per-option basis through the [Option] class.
*/
fun preventIconTint(preventIconTint: Boolean) {
this.preventIconTint = preventIconTint
}

/**
* Set the [OptionListener].
*
Expand Down Expand Up @@ -369,6 +379,7 @@ class OptionsSheet : Sheet() {
OptionsAdapter(
requireActivity(),
options,
preventIconTint,
mode,
multipleChoices,
collapsedItems,
Expand Down

0 comments on commit 8373c98

Please sign in to comment.