Skip to content

Commit

Permalink
Merge pull request #7 from kojofosu/dev
Browse files Browse the repository at this point in the history
Fixes min and max values crashing
  • Loading branch information
kojofosu authored Oct 24, 2021
2 parents 4513e00 + 183c7f4 commit 9d203d0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
Expand All @@ -32,17 +33,25 @@ class HorizontalQuantitizer @JvmOverloads constructor(context: Context,

private var _animationDuration = 300L
private var _minValue:Int = 0
private var _maxValue:Int? = null
private var _maxValue:Int = Int.MAX_VALUE
private var _animateButtons: Boolean = true
private var _animationStyle: AnimationStyle = AnimationStyle.SWING

var minValue: Int
get() = _minValue
set(value) {
_minValue = value
if (value >= currentValue) {
binding.quantityTv.text =
Editable.Factory.getInstance().newEditable(value.toString())
currentValue = value
_minValue = value
} else {
_minValue = value
currentValue = value
}
}

var maxValue: Int?
var maxValue: Int
get() = _maxValue
set(value) {
_maxValue = value
Expand Down Expand Up @@ -93,19 +102,30 @@ class HorizontalQuantitizer @JvmOverloads constructor(context: Context,
/*decrease*/
binding.decreaseIb.setOnClickListener {
hideKeyboard()
doDec()
if (minValue >= currentValue) {
//do nothing
} else {
doDec()


//listener
listener?.activateOnDecrease(_animationDuration)
}

//listener
listener?.activateOnDecrease(_animationDuration)
}

/*increase*/
binding.increaseIb.setOnClickListener {
hideKeyboard()
doInc()
if (maxValue <= currentValue) {
//do nothing
} else {
doInc()

//listener
listener?.activateOnIncrease(_animationDuration)
}

//listener
listener?.activateOnIncrease(_animationDuration)
}

/*make edit text cursor visible when clicked*/
Expand All @@ -127,7 +147,18 @@ class HorizontalQuantitizer @JvmOverloads constructor(context: Context,
}

override fun afterTextChanged(s: Editable?) {
//TODO("Not yet implemented")
if (s.toString().isEmpty()) {
//do nothing
}else if (Integer.parseInt(s.toString()) < minValue) {
binding.quantityTv.text = Editable.Factory.getInstance().newEditable(minValue.toString())
currentValue = minValue
Toast.makeText(context, "Min value is $minValue", Toast.LENGTH_SHORT).show()
}else if (Integer.parseInt(s.toString()) > maxValue) {
binding.quantityTv.text = Editable.Factory.getInstance().newEditable(minValue.toString())
currentValue = minValue
Toast.makeText(context, "Max value is $maxValue", Toast.LENGTH_SHORT).show()

}
}

})
Expand All @@ -149,34 +180,26 @@ class HorizontalQuantitizer @JvmOverloads constructor(context: Context,
private fun doInc() {
if (_animateButtons) {
animatePlusButton()

}

binding.quantityTv.isCursorVisible = false // hide cursor if it's visible
val increasedValue: Int = currentValue.inc()
currentValue = increasedValue
animateInc()

if (currentValue >= maxValue!!) {
//Do nothing
// wobble(binding.quantityTv)
} else {
binding.quantityTv.isCursorVisible = false // hide cursor if it's visible
val increasedValue: Int = currentValue.inc()
currentValue = increasedValue
animateInc()
}
}

private fun doDec() {
if (_animateButtons) {
animateMinusButton()
}

if (currentValue <= minValue) {
//Do nothing
// wobble(binding.quantityTv)
} else {
binding.quantityTv.isCursorVisible = false // hide cursor if it's visible
val decreasedValue: Int = currentValue.dec()
currentValue = decreasedValue
animateDec()
}
binding.quantityTv.isCursorVisible = false // hide cursor if it's visible
val decreasedValue: Int = currentValue.dec()
currentValue = decreasedValue
animateDec()

}

private fun animateInc() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
Expand All @@ -32,17 +33,26 @@ class VerticalQuantitizer @JvmOverloads constructor(context: Context,

private var _animationDuration = 300L
private var _minValue:Int = 0
private var _maxValue:Int? = null
private var _maxValue:Int = Int.MAX_VALUE
private var _animateButtons = true
private var _animationStyle = AnimationStyle.SWING

var minValue: Int
get() = _minValue
set(value,) {
_minValue = value

if (value >= currentValue) {
binding.quantityTv.text =
Editable.Factory.getInstance().newEditable(value.toString())
currentValue = value
_minValue = value
} else {
_minValue = value
currentValue = value
}
}

var maxValue: Int?
var maxValue: Int
get() = _maxValue
set(value) {
_maxValue = value
Expand Down Expand Up @@ -99,19 +109,32 @@ class VerticalQuantitizer @JvmOverloads constructor(context: Context,
/*decrease*/
binding.decreaseIb.setOnClickListener {
hideKeyboard()
doDec()

//listener
listener?.activateOnDecrease(_animationDuration)

if (minValue >= currentValue) {
//do nothing
} else {
doDec()

//listener
listener?.activateOnDecrease(_animationDuration)
}

}

/*increase*/
binding.increaseIb.setOnClickListener {
hideKeyboard()
doInc()
if (maxValue <= currentValue) {
//do nothing
} else {
doInc()


//listener
listener?.activateOnIncrease(_animationDuration)
}

//listener
listener?.activateOnIncrease(_animationDuration)
}

/*make edit text cursor visible when clicked*/
Expand All @@ -133,7 +156,18 @@ class VerticalQuantitizer @JvmOverloads constructor(context: Context,
}

override fun afterTextChanged(s: Editable?) {
//TODO("Not yet implemented")
if (s.toString().isEmpty()) {
//do nothing
}else if (Integer.parseInt(s.toString()) < minValue) {
binding.quantityTv.text = Editable.Factory.getInstance().newEditable(minValue.toString())
currentValue = minValue
Toast.makeText(context, "Min value is $minValue", Toast.LENGTH_SHORT).show()
}else if (Integer.parseInt(s.toString()) > maxValue) {
binding.quantityTv.text = Editable.Factory.getInstance().newEditable(minValue.toString())
currentValue = minValue
Toast.makeText(context, "Max value is $maxValue", Toast.LENGTH_SHORT).show()

}
}

})
Expand All @@ -150,27 +184,21 @@ class VerticalQuantitizer @JvmOverloads constructor(context: Context,
}

private fun doInc() {
if (currentValue >= maxValue!!) {
//do nothing
// wobble(binding.quantityTv)
} else {
binding.quantityTv.isCursorVisible = false
val increasedValue: Int = currentValue.inc()
currentValue = increasedValue
animateInc()
}

binding.quantityTv.isCursorVisible = false
val increasedValue: Int = currentValue.inc()
currentValue = increasedValue
animateInc()

}

private fun doDec() {
if (currentValue <= minValue) {
//do nothing
// wobble(binding.quantityTv)
} else {
binding.quantityTv.isCursorVisible = false
val decreasedValue: Int = currentValue.dec()
currentValue = decreasedValue
animateDec()
}

binding.quantityTv.isCursorVisible = false
val decreasedValue: Int = currentValue.dec()
currentValue = decreasedValue
animateDec()

}

private fun animateInc() {
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/mcdev/quantitizer/ButtonsOnlyActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class ButtonsOnlyActivity : AppCompatActivity() {
val nvq = findViewById<NoValueQuantitizer>(R.id.nv_quant)

hq.apply {
value = 1
// value = 1
buttonAnimationEnabled = false
textAnimationStyle = AnimationStyle.SLIDE_IN
animationDuration = 400L

minValue = 3
maxValue = 7

// setPlusIconBackgroundColor("#C19A6B")
// setMinusIconBackgroundColor("#C19A6B")
// setMinusIconColor("#ffffff")
Expand All @@ -33,6 +37,10 @@ class ButtonsOnlyActivity : AppCompatActivity() {
value = 1
buttonAnimationEnabled = false
textAnimationStyle = AnimationStyle.FALL_IN

minValue = 5
maxValue = 8

// setPlusIconBackgroundColor("#C19A6B")
// setMinusIconBackgroundColor("#C19A6B")
// setMinusIconColor("#ffffff")
Expand Down

0 comments on commit 9d203d0

Please sign in to comment.