Handle clear button short taps

This commit is contained in:
Ensar Sarajčić 2023-09-13 16:57:45 +02:00
parent 128986bf7c
commit 89f8502a13
2 changed files with 17 additions and 5 deletions

View file

@ -42,7 +42,11 @@ class UnitConverterActivity : SimpleActivity() {
this.converter = converter
binding.viewUnitConverter.btnClear.setVibratingOnClickListener {
binding.viewUnitConverter.viewConverter.root.deleteCharacter()
}
binding.viewUnitConverter.btnClear.setOnLongClickListener {
binding.viewUnitConverter.viewConverter.root.clear()
true
}
getButtonIds().forEach {

View file

@ -9,7 +9,6 @@ import android.graphics.drawable.RippleDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.widget.TextViewCompat
import com.simplemobiletools.calculator.R
@ -105,6 +104,19 @@ class ConverterView @JvmOverloads constructor(
binding.bottomUnitText.text = "0"
}
fun deleteCharacter() {
var newValue = binding.topUnitText.text.dropLast(1)
newValue = newValue.trimEnd(groupingSeparator.single())
if (newValue == "") {
newValue = "0"
}
val value = formatter.removeGroupingSeparator(newValue.toString()).toDouble()
binding.topUnitText.text = formatter.doubleToString(value)
updateBottomValue()
}
fun numpadClicked(id: Int) {
when (id) {
R.id.btn_decimal -> decimalClicked()
@ -185,10 +197,6 @@ class ConverterView @JvmOverloads constructor(
}
}
private fun TextView.getValue(): Double {
return formatter.removeGroupingSeparator(text.toString()).toDouble()
}
private fun <T> KMutableProperty0<T>.swapWith(other: KMutableProperty0<T>) {
this.get().also {
this.set(other.get())