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/CanvasExtensions.kt b/styled-core/src/main/java/co/sodalabs/view/CanvasExtensions.kt index 31753d4..df79830 100644 --- a/styled-core/src/main/java/co/sodalabs/view/CanvasExtensions.kt +++ b/styled-core/src/main/java/co/sodalabs/view/CanvasExtensions.kt @@ -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) } \ No newline at end of file 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.