From b42c99ad60b777d8832b919616487337dff8af79 Mon Sep 17 00:00:00 2001 From: Boy Wang Date: Thu, 6 Dec 2018 14:52:03 +0800 Subject: [PATCH] Fix the recursion case by changing the toggle. (#27) * Fix the recursion case by changing the toggle. * Revert a mistake of canvas extension. --- gradle.properties | 2 +- .../main/java/co/sodalabs/view/TestView.kt | 4 ++ .../java/co/sodalabs/view/StyledSwitchView.kt | 39 +++++++++++-------- .../java/co/sodalabs/view/ToggleableView.kt | 8 ++-- 4 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 styled-core/src/main/java/co/sodalabs/view/TestView.kt diff --git a/gradle.properties b/gradle.properties index 5754c84..1ebce77 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/styled-core/src/main/java/co/sodalabs/view/TestView.kt b/styled-core/src/main/java/co/sodalabs/view/TestView.kt new file mode 100644 index 0000000..7a88fce --- /dev/null +++ b/styled-core/src/main/java/co/sodalabs/view/TestView.kt @@ -0,0 +1,4 @@ +package co.sodalabs.view + +class TestView { +} \ No newline at end of file diff --git a/styled-switch-view/src/main/java/co/sodalabs/view/StyledSwitchView.kt b/styled-switch-view/src/main/java/co/sodalabs/view/StyledSwitchView.kt index b0ba296..9bfe206 100644 --- a/styled-switch-view/src/main/java/co/sodalabs/view/StyledSwitchView.kt +++ b/styled-switch-view/src/main/java/co/sodalabs/view/StyledSwitchView.kt @@ -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) @@ -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 @@ -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) @@ -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 } @@ -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 diff --git a/styled-switch-view/src/main/java/co/sodalabs/view/ToggleableView.kt b/styled-switch-view/src/main/java/co/sodalabs/view/ToggleableView.kt index fc80355..df6ebc5 100644 --- a/styled-switch-view/src/main/java/co/sodalabs/view/ToggleableView.kt +++ b/styled-switch-view/src/main/java/co/sodalabs/view/ToggleableView.kt @@ -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.