Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the recursion case by changing the toggle. #27

Merged
merged 2 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
tcw165 marked this conversation as resolved.
Show resolved Hide resolved

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)
tcw165 marked this conversation as resolved.
Show resolved Hide resolved

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