Skip to content

Commit

Permalink
Fix the recursion case by changing the toggle.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcw165 committed Dec 6, 2018
1 parent 5654fac commit 7bcbccd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ kotlin.incremental=true
## Version name will get injected into the AndroidManifest.xml file at build time.
## The version code will be derived.
## Must match MILESTONE.MAJOR.MINOR.BUILD format.
styled_view_universal_artifact_version=1.1.0
styled_view_universal_artifact_version=1.1.1
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import android.graphics.Canvas
/**
* @link https://github.com/android/android-ktx/blob/89ee2e1cde1e1b0226ed944b9abd55cee0f9b9d4/src/main/java/androidx/core/graphics/Canvas.kt
*/
@Deprecated("Replaced by android-ktx")
inline fun Canvas.runSafely(
noinline lambda: Canvas.() -> Unit
fun Canvas.runSafely(
lambda: Canvas.() -> Unit
) {
val c = save()
lambda()
lambda.invoke(this)
restoreToCount(c)
}
4 changes: 4 additions & 0 deletions styled-core/src/main/java/co/sodalabs/view/TestView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package co.sodalabs.view

class TestView {
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,9 @@ class StyledSwitchView : ToggleableView {
/**
* Boolean state of this Switch.
*/
override var isOn: Boolean = false
set(value) {
field = value
if (thumbAnimator?.isStarted == false && thumbAnimator?.isStarted == false) {
setupThumbBounds(thumbBounds, thumbOnCenterX, thumbOffCenterX, thumbRadius, value)
}
onToggledListener?.invoke(this, value)
postInvalidate()
}
private var isActualOn: Boolean = false
override val isOn: Boolean
get() = isActualOn

constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
Expand All @@ -191,6 +185,19 @@ class StyledSwitchView : ToggleableView {
initProperties(attrs)
}

override fun setOnOff(value: Boolean, userTriggered: Boolean) {
if (value != isActualOn) {
isActualOn = value
if (thumbAnimator?.isStarted == false && thumbAnimator?.isStarted == false) {
setupThumbBounds(thumbBounds, thumbOnCenterX, thumbOffCenterX, thumbRadius, value)
}

onToggledListener?.invoke(this, value, userTriggered)

postInvalidate()
}
}

private fun initView() {
paint.isAntiAlias = true
paint.strokeJoin = Paint.Join.MITER
Expand All @@ -200,7 +207,7 @@ class StyledSwitchView : ToggleableView {
val attributes = context.theme.obtainStyledAttributes(attrs, R.styleable.StyledSwitchView, 0, 0)
for (i in 0 until attributes.indexCount) {
when (attributes.getIndex(i)) {
R.styleable.StyledSwitchView_swOn -> isOn = attributes.getBoolean(R.styleable.StyledSwitchView_swOn, false)
R.styleable.StyledSwitchView_swOn -> setOnOff(attributes.getBoolean(R.styleable.StyledSwitchView_swOn, false), false)
R.styleable.StyledSwitchView_swColorDisabled -> colorDisabled = attributes.getColor(R.styleable.StyledSwitchView_swColorDisabled,
Color.parseColor("#D3D3D3"))
R.styleable.StyledSwitchView_swTextOn -> labelOn = attributes.getString(R.styleable.StyledSwitchView_swTextOn)
Expand Down Expand Up @@ -422,9 +429,9 @@ class StyledSwitchView : ToggleableView {
override fun performClick(): Boolean {
super.performClick()

val newIsOn = !isOn
animateThumbnail(newIsOn)
isOn = newIsOn
val newValue = !isOn
animateThumbnail(newValue)
setOnOff(newValue, userTriggered = true)

return true
}
Expand Down Expand Up @@ -475,9 +482,9 @@ class StyledSwitchView : ToggleableView {
if (span < 200) {
performClick()
} else {
val newIsOn = x >= outlineRectBound.centerX()
animateThumbnail(newIsOn)
isOn = newIsOn
val newValue = x >= outlineRectBound.centerX()
animateThumbnail(newValue)
setOnOff(newValue, userTriggered = true)
}
invalidate()
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import android.content.Context
import android.util.AttributeSet
import android.view.View

typealias OnToggleListener = (toggleableView: ToggleableView, isOn: Boolean) -> Unit
typealias OnToggleListener = (toggleableView: ToggleableView, isOn: Boolean, userTriggered: Boolean) -> Unit

open class ToggleableView : View {
abstract class ToggleableView : View {

/**
* Boolean state of this Switch.
*/
open var isOn: Boolean = false
open val isOn: Boolean = false

abstract fun setOnOff(value: Boolean, userTriggered: Boolean)

/**
* Listener used to dispatch switch events.
Expand Down

0 comments on commit 7bcbccd

Please sign in to comment.