Skip to content

Commit

Permalink
Bugfix/double tap (#17)
Browse files Browse the repository at this point in the history
* Fix double tap bug (#16) and crash (#15) (was crashing after restoring state due to missing super call)
  • Loading branch information
Roman Zavarnitsyn authored Aug 29, 2018
1 parent bf51794 commit 100a314
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ allprojects {
Add the dependency to your lib/app `build.gradle`
```gradle
dependencies {
implementation 'com.github.okdroid:checkablechipview:1.0.1'
implementation 'com.github.okdroid:checkablechipview:1.0.2'
}
```

Expand Down
2 changes: 1 addition & 1 deletion lib/publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ apply plugin: 'maven-publish'

def artifactGroup = 'com.github.okdroid'
def artifactName = 'checkablechipview'
def artifactVersion = '1.0.1'
def artifactVersion = '1.0.2'

task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ import android.view.animation.AnimationUtils
import android.widget.Checkable
import android.widget.TextView
import androidx.core.animation.doOnEnd
import androidx.core.content.res.getColorOrThrow
import androidx.core.content.res.getDimensionOrThrow
import androidx.core.content.res.getDimensionPixelSizeOrThrow
import androidx.core.content.res.getDrawableOrThrow
import androidx.core.content.res.getStringOrThrow
import androidx.core.content.res.*
import androidx.core.content.withStyledAttributes
import androidx.core.graphics.withScale
import androidx.core.graphics.withTranslation
Expand Down Expand Up @@ -112,6 +108,8 @@ class CheckableChipView @JvmOverloads constructor(
*/
var onCheckedChangeListener: ((view: CheckableChipView, checked: Boolean) -> Unit)? = null

private var targetProgress: Float = 0f

private var progress: Float by viewProperty(0f) {
if (it == 0f || it == 1f) {
onCheckedChangeListener?.invoke(this, isChecked)
Expand Down Expand Up @@ -302,18 +300,18 @@ class CheckableChipView @JvmOverloads constructor(
* Starts the animation to enable/disable a filter and invokes a function when done.
*/
fun setCheckedAnimated(checked: Boolean, onEnd: (() -> Unit)?) {
val newProgress = if (checked) 1f else 0f
if (newProgress != progress) {
targetProgress = if (checked) 1f else 0f
if (targetProgress != progress) {
progressAnimator.apply {
removeAllListeners()
cancel()
setFloatValues(progress, newProgress)
setFloatValues(progress, targetProgress)
duration = if (checked) CHECKING_DURATION else UNCHECKING_DURATION
addUpdateListener {
progress = it.animatedValue as Float
}
doOnEnd {
progress = newProgress
progress = targetProgress
onEnd?.invoke()
}
start()
Expand All @@ -330,14 +328,15 @@ class CheckableChipView @JvmOverloads constructor(
return handled
}

override fun isChecked() = progress == 1f
override fun isChecked() = targetProgress == 1f

override fun toggle() {
isChecked = !isChecked
}

override fun setChecked(checked: Boolean) {
progress = if (checked) 1f else 0f
targetProgress = if (checked) 1f else 0f
progress = targetProgress
}

private fun createLayout(textWidth: Int) {
Expand Down Expand Up @@ -387,6 +386,15 @@ class CheckableChipView @JvmOverloads constructor(

class SavedState : BaseSavedState {

@Suppress("unused")
companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(source: Parcel): SavedState = SavedState(source)
override fun newArray(size: Int): Array<SavedState?> = arrayOfNulls(size)
}
}

var checked: Boolean = false

constructor(state: Parcelable) : super(state)
Expand All @@ -395,19 +403,12 @@ class CheckableChipView @JvmOverloads constructor(
checked = parcel.readInt() == 1
}

override fun describeContents() = 0

override fun writeToParcel(dest: Parcel, flags: Int) = dest.writeInt(if (checked) 1 else 0)

@Suppress("unused")
companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(source: Parcel) = SavedState(source)

override fun newArray(size: Int) = arrayOfNulls<SavedState>(size)
}
override fun writeToParcel(dest: Parcel, flags: Int) {
super.writeToParcel(dest, flags)
dest.writeInt(if (checked) 1 else 0)
}

override fun describeContents() = 0
}

/**
Expand Down

0 comments on commit 100a314

Please sign in to comment.