Skip to content

Commit

Permalink
Syntax fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yeocak committed Sep 4, 2021
1 parent b9068e7 commit b33709a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ class MultiOptionSwitch @JvmOverloads constructor(
R.styleable.MultiSwitch,
0, 0
).apply {
// Getting data from attrs.xml
// Set background by direction
background = if (getInt(R.styleable.MultiSwitch_direction, 0) == 0) {
HorizontalSwitchBackground()
} else {
VerticalSwitchBackground()
}

// Getting data from attrs.xml
optionCount = getInt(R.styleable.MultiSwitch_option_count, 3)
selector.selectedOption = getInt(R.styleable.MultiSwitch_default_selected_option, 1) - 1
background.backgroundVisible =
Expand All @@ -113,7 +114,17 @@ class MultiOptionSwitch @JvmOverloads constructor(
}
}

/**
You can select option between 1 and "optionCount".
*/
fun selectOption(optionIndex: Int) {
if (optionIndex < 1 || optionIndex > optionCount)
throw ArrayIndexOutOfBoundsException(
"Your multi option switch has only ${optionCount}. " +
"But you tried to select option ${optionIndex}. " +
"You should select to between [1,${optionCount}]"
)

selector.goPositionWithAnimation(
selectCoordinates[optionIndex - 1],
background.isHorizontal
Expand Down Expand Up @@ -192,7 +203,6 @@ class MultiOptionSwitch @JvmOverloads constructor(
selector.positionX =
(backgroundCoordinates.right - backgroundCoordinates.left) / 2 + backgroundCoordinates.left
selector.positionY =
// TODO
selectCoordinates[selector.selectedOption]
}

Expand All @@ -206,45 +216,40 @@ class MultiOptionSwitch @JvmOverloads constructor(

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (background.isHorizontal) {
if (event.action == MotionEvent.ACTION_MOVE) {
selector.positionX = event.x.coerceAtLeast(selectCoordinates.first())
.coerceAtMost(selectCoordinates.last())
fun goOptionWithAnimation(nearestOption: Int, isHorizontal: Boolean) {
selector.goPositionWithAnimation(
selectCoordinates[nearestOption],
isHorizontal
) {
selector.selectedOption = nearestOption
invalidate()
} else if (event.action == MotionEvent.ACTION_UP) {
val nearestOption = findNearestOptionPosition(selector.positionX)
selector.goPositionWithAnimation(
selectCoordinates[nearestOption],
true
) {
selector.selectedOption = nearestOption
invalidate()
}

optionChangedListener?.let {
it(selector.selectedOption + 1)
}
}
} else {
if (event.action == MotionEvent.ACTION_MOVE) {

optionChangedListener?.let {
it(selector.selectedOption + 1)
}
}

if (event.action == MotionEvent.ACTION_MOVE) {
if (background.isHorizontal) {
selector.positionX = event.x.coerceAtLeast(selectCoordinates.first())
.coerceAtMost(selectCoordinates.last())
} else {
selector.positionY = event.y.coerceAtLeast(selectCoordinates.first())
.coerceAtMost(selectCoordinates.last())
invalidate()
} else if (event.action == MotionEvent.ACTION_UP) {
}
invalidate()
} else if (event.action == MotionEvent.ACTION_UP) {
if (background.isHorizontal) {
val nearestOption = findNearestOptionPosition(selector.positionX)
goOptionWithAnimation(nearestOption, true)
} else {
val nearestOption = findNearestOptionPosition(selector.positionY)
selector.goPositionWithAnimation(
selectCoordinates[nearestOption],
false
) {
selector.selectedOption = nearestOption
invalidate()
}

optionChangedListener?.let {
it(selector.selectedOption + 1)
}
goOptionWithAnimation(nearestOption, false)
}

}

return true
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ Add it in your root build.gradle at the end of repositories:
Add the dependency:

dependencies {
implementation 'com.github.yeocak:KotlinMultiOptionSwitch:0.3.0'
implementation 'com.github.yeocak:KotlinMultiOptionSwitch:0.3.2'
}

0 comments on commit b33709a

Please sign in to comment.