Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
- add setter for default values - add reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-android committed Feb 25, 2019
1 parent 5057925 commit 15e54a2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class MainActivity : AppCompatActivity() {

picker.apply {
setOptions(getOptions())
setDefaultSelectedValues(1 to 4)
setOnRangeSelectedListener { _, leftPoint, rightPoint ->
// Toast.makeText(context, "${picker.getSelectedOptions()}", Toast.LENGTH_SHORT).show()
// Toast.makeText(context, "${picker.getSelectedOptions()}", Toast.LENGTH_SHORT).show()
// Toast.makeText(context, "${leftPoint.first} + ${rightPoint.first}", Toast.LENGTH_SHORT).show()
}
}

btn_get_values.setOnClickListener {
picker.resetSelectedValues()
Toast.makeText(this, "${picker.getSelectedOptions()}", Toast.LENGTH_SHORT).show()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class RangePickerView : View {

//default selected rectangles
if (pair.second.isSelected && isFirstDraw) {
if (dataOfAnimation.firstPreviousIndex == -1 || dataOfAnimation.secondPreviousIndex == -1) {
if (dataOfAnimation.firstDefaultIndex == -1 || dataOfAnimation.secondDefaultIndex == -1) {
if (selectedCount == 0) {
dataOfAnimation.firstPreviousIndex = index
firstSelectedRect.set(coordinateRect)
Expand Down Expand Up @@ -591,7 +591,10 @@ class RangePickerView : View {
private fun optionToPair(size: Int, index: Int, option: Option): Pair<Option, RectShape> {
return option to RectShape().apply {
// Default selected first and the last
isSelected = (index == 0 || index == size)
if (dataOfAnimation.firstPreviousIndex == -1 || dataOfAnimation.secondPreviousIndex == -1) {
isSelected = (index == dataOfAnimation.firstPreviousIndex
|| index == dataOfAnimation.secondPreviousIndex)
}
cornerRadius = this@RangePickerView.cornerRadius
}
}
Expand Down Expand Up @@ -629,6 +632,52 @@ class RangePickerView : View {
}
}

fun resetSelectedValues() {
tapMode = dataOfAnimation.defaultTapMode
updateSelectedIndexes()
options.forEachIndexed { index, item ->
item.second.isSelected = (index == dataOfAnimation.firstDefaultIndex
|| index == dataOfAnimation.secondDefaultIndex)
}

animateView()
}

fun setDefaultSelectedValues(position: Pair<Int, Int>) {
val size = options.size - 1
if (position.first >= 0 && position.second <= size) {
dataOfAnimation.firstDefaultIndex = position.first
dataOfAnimation.secondDefaultIndex = position.second
updateSelectedIndexes(position)
options[position.first].second.isSelected = true
options[position.second].second.isSelected = true
} else {
val isFirstNotSuites = position.first < 0
val isSecondNotSuites = position.second > size

if (isFirstNotSuites && isSecondNotSuites) {
throw IndexOutOfBoundsException("Positions of selected elements must be from 0 to $size!")
} else if (isFirstNotSuites) {
throw IndexOutOfBoundsException("Position of first element must be equal or greater than 0!")
} else if (isSecondNotSuites) {
throw IndexOutOfBoundsException("Position of second element must be equal or lover than $size!")
}
}
}

private fun updateSelectedIndexes(position: Pair<Int, Int>? = null) {
val firstIndex = position?.first ?: dataOfAnimation.firstDefaultIndex
val secondIndex = position?.second ?: dataOfAnimation.secondDefaultIndex

if (position == null) {
dataOfAnimation.firstNewIndex = firstIndex
dataOfAnimation.secondNewIndex = secondIndex
} else {
dataOfAnimation.firstPreviousIndex = firstIndex
dataOfAnimation.secondPreviousIndex = secondIndex
}
}

interface OnRangeSelectedListener {
fun onRangeSelected(
view: RangePickerView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class DataRangeAnimation {

var firstNewIndex: Int = -1
var secondNewIndex: Int = -1

var firstDefaultIndex: Int = -1
var secondDefaultIndex: Int = -1
var defaultTapMode: TapMode = TapMode.NONE
}

0 comments on commit 15e54a2

Please sign in to comment.