Skip to content

Commit

Permalink
Merge pull request #129 from skydoves/fix/iconspinneradapters
Browse files Browse the repository at this point in the history
Fix the spinner arrow doesn't show when the adapter is IconSpinnerAdapter
  • Loading branch information
skydoves authored Aug 27, 2022
2 parents 6dfbbd3 + 5965edf commit 48878be
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.skydoves.powerspinner

object Versions {
internal const val ANDROID_GRADLE_PLUGIN = "7.2.0"
internal const val ANDROID_GRADLE_SPOTLESS = "6.3.0"
internal const val ANDROID_GRADLE_PLUGIN = "7.2.1"
internal const val ANDROID_GRADLE_SPOTLESS = "6.7.0"
internal const val GRADLE_NEXUS_PUBLISH_PLUGIN = "1.1.0"
internal const val KOTLIN = "1.6.10"
internal const val KOTLIN_GRADLE_DOKKA = "1.6.21"
internal const val KOTLIN_BINARY_VALIDATOR = "0.8.0"
internal const val KOTLIN = "1.7.10"
internal const val KOTLIN_GRADLE_DOKKA = "1.7.10"
internal const val KOTLIN_BINARY_VALIDATOR = "0.11.0"

internal const val APPCOMPAT = "1.4.1"
internal const val MATERIAL = "1.6.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion powerspinner/api/powerspinner.api
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class com/skydoves/powerspinner/IconSpinnerAdapter : androidx/recyc
}

public final class com/skydoves/powerspinner/IconSpinnerAdapter$IconSpinnerViewHolder : androidx/recyclerview/widget/RecyclerView$ViewHolder {
public fun <init> (Lcom/skydoves/powerspinner/databinding/PowerspinnerItemDefaultPowerBinding;)V
public fun <init> (Lcom/skydoves/powerspinner/IconSpinnerAdapter;Lcom/skydoves/powerspinner/databinding/PowerspinnerItemDefaultPowerBinding;)V
}

public final class com/skydoves/powerspinner/IconSpinnerItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class DefaultSpinnerAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DefaultSpinnerViewHolder {
val binding =
PowerspinnerItemDefaultPowerBinding.inflate(
LayoutInflater.from(parent.context), parent,
LayoutInflater.from(parent.context),
parent,
false
)
return DefaultSpinnerViewHolder(binding).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.powerspinner.databinding.PowerspinnerItemDefaultPowerBinding
Expand All @@ -48,7 +49,8 @@ public class IconSpinnerAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IconSpinnerViewHolder {
val binding =
PowerspinnerItemDefaultPowerBinding.inflate(
LayoutInflater.from(parent.context), parent,
LayoutInflater.from(parent.context),
parent,
false
)
return IconSpinnerViewHolder(binding).apply {
Expand All @@ -75,19 +77,7 @@ public class IconSpinnerAdapter(
if (index == NO_SELECTED_INDEX) return
val item = spinnerItems[index]
spinnerView.compoundDrawablePadding = item.iconPadding ?: spinnerView.compoundDrawablePadding
val icon = item.iconRes?.let {
ResourcesCompat.getDrawable(spinnerView.resources, it, null)
} ?: item.icon
when (item.iconGravity) {
Gravity.START ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
Gravity.END ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
Gravity.TOP ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
Gravity.BOTTOM ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, icon)
}
spinnerView.applyCompoundDrawables(spinnerView, item)
val oldIndex = this.index
this.index = index
this.spinnerView.notifyItemSelected(index, item.text)
Expand All @@ -101,7 +91,7 @@ public class IconSpinnerAdapter(

override fun getItemCount(): Int = this.spinnerItems.size

public class IconSpinnerViewHolder(private val binding: PowerspinnerItemDefaultPowerBinding) :
public inner class IconSpinnerViewHolder(private val binding: PowerspinnerItemDefaultPowerBinding) :
RecyclerView.ViewHolder(binding.root) {

internal fun bind(item: IconSpinnerItem, spinnerView: PowerSpinnerView) {
Expand All @@ -112,29 +102,41 @@ public class IconSpinnerAdapter(
setTextSize(TypedValue.COMPLEX_UNIT_PX, item.textSize ?: spinnerView.textSize)
setTextColor(item.textColor ?: spinnerView.currentTextColor)
compoundDrawablePadding = item.iconPadding ?: spinnerView.compoundDrawablePadding
val icon = item.iconRes?.let {
ResourcesCompat.getDrawable(spinnerView.resources, it, null)
} ?: item.icon
when (item.iconGravity) {
Gravity.START ->
setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
Gravity.END ->
setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
Gravity.TOP ->
setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
Gravity.BOTTOM ->
setCompoundDrawablesWithIntrinsicBounds(null, null, null, icon)
applyCompoundDrawables(spinnerView, item)
binding.root.setPadding(
spinnerView.paddingLeft,
spinnerView.paddingTop,
spinnerView.paddingRight,
spinnerView.paddingBottom
)
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
}
binding.root.setPadding(
spinnerView.paddingLeft,
spinnerView.paddingTop,
spinnerView.paddingRight,
spinnerView.paddingBottom
)
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
}
}

@JvmSynthetic
internal fun AppCompatTextView.applyCompoundDrawables(
spinnerView: PowerSpinnerView,
item: IconSpinnerItem
) {
val icon = item.iconRes?.let {
ResourcesCompat.getDrawable(spinnerView.resources, it, null)
} ?: item.icon
val start = compoundDrawablesRelative[0]
val top = compoundDrawablesRelative[1]
val end = compoundDrawablesRelative[2]
val bottom = compoundDrawablesRelative[3]
when (item.iconGravity) {
Gravity.START ->
setCompoundDrawablesRelativeWithIntrinsicBounds(icon, top, end, bottom)
Gravity.END ->
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, icon, bottom)
Gravity.TOP ->
setCompoundDrawablesRelativeWithIntrinsicBounds(start, icon, end, bottom)
Gravity.BOTTOM ->
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, icon)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ public data class IconSpinnerItem @JvmOverloads constructor(
val textTypeface: Typeface? = null,
val gravity: Int? = null,
val textSize: Float? = null,
@ColorInt val textColor: Int? = null,
@ColorInt val textColor: Int? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ internal class PowerSpinnerPersistence private constructor() {

/** gets selected index from the preference. */
fun getSelectedIndex(name: String): Int = sharedPreferenceManager.getInt(
INDEX + name, NO_SELECTED_INDEX
INDEX + name,
NO_SELECTED_INDEX
)

/** puts selected index from the preference. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,33 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver {
}
}
when (this.arrowGravity) {
SpinnerGravity.START -> setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
SpinnerGravity.TOP -> setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
SpinnerGravity.END -> setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
SpinnerGravity.BOTTOM -> setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable)
SpinnerGravity.START -> setCompoundDrawablesRelativeWithIntrinsicBounds(
drawable,
null,
null,
null
)
SpinnerGravity.TOP -> setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
drawable,
null,
null
)
SpinnerGravity.END -> setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
null,
drawable,
null
)
SpinnerGravity.BOTTOM -> setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
null,
null,
drawable
)
}
} else {
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ package com.skydoves.powerspinner.internals
internal inline fun String?.whatIfNotNullOrEmpty(
whatIf: (String) -> Unit
) {

this.whatIfNotNullOrEmpty(
whatIf = whatIf,
whatIfNot = { }
Expand All @@ -44,7 +43,6 @@ internal inline fun String?.whatIfNotNullOrEmpty(
whatIf: (String) -> Unit,
whatIfNot: () -> Unit
) {

if (!this.isNullOrEmpty()) {
whatIf(this)
} else {
Expand Down
2 changes: 1 addition & 1 deletion spotless/spotless.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spotless {
kotlin {
target "**/*.kt"
targetExclude "**/build/**/*.kt"
ktlint().userData(['indent_size': '2', 'continuation_indent_size': '2'])
ktlint().setUseExperimental(true).editorConfigOverride(['indent_size': '2', 'continuation_indent_size': '2'])
licenseHeaderFile "$rootDir/spotless/spotless.license.kt"
trimTrailingWhitespace()
endWithNewline()
Expand Down

0 comments on commit 48878be

Please sign in to comment.