Minor readability improvements

This commit is contained in:
Naveen 2022-05-31 10:24:22 +05:30
parent 65ee3c38c3
commit 27989149b8
2 changed files with 34 additions and 20 deletions

View file

@ -37,28 +37,28 @@ class MainActivity : SimpleActivity(), Calculator {
calc = CalculatorImpl(this, applicationContext)
btn_plus.setOnClickListener { calc.handleOperation(PLUS); checkHaptic(it) }
btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) }
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) }
btn_divide.setOnClickListener { calc.handleOperation(DIVIDE); checkHaptic(it) }
btn_percent.setOnClickListener { calc.handleOperation(PERCENT); checkHaptic(it) }
btn_power.setOnClickListener { calc.handleOperation(POWER); checkHaptic(it) }
btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) }
btn_plus.setOnClickOperation(PLUS)
btn_minus.setOnClickOperation(MINUS)
btn_multiply.setOnClickOperation(MULTIPLY)
btn_divide.setOnClickOperation(DIVIDE)
btn_percent.setOnClickOperation(PERCENT)
btn_power.setOnClickOperation(POWER)
btn_root.setOnClickOperation(ROOT)
btn_minus.setOnLongClickListener { calc.turnToNegative() }
btn_minus.setOnLongClickListener {
calc.turnToNegative()
btn_clear.setVibratingOnClick { calc.handleClear() }
btn_clear.setOnLongClickListener {
calc.handleReset()
true
}
btn_clear.setOnClickListener { calc.handleClear(); checkHaptic(it) }
btn_clear.setOnLongClickListener { calc.handleReset(); true }
getButtonIds().forEach {
it.setOnClickListener { view ->
calc.numpadClicked(view.id); checkHaptic(view)
it.setVibratingOnClick { view ->
calc.numpadClicked(view.id)
}
}
btn_equals.setOnClickListener { calc.handleEquals(); checkHaptic(it) }
btn_equals.setVibratingOnClick { calc.handleEquals() }
formula.setOnLongClickListener { copyToClipboard(false) }
result.setOnLongClickListener { copyToClipboard(true) }
@ -210,4 +210,17 @@ class MainActivity : SimpleActivity(), Calculator {
calc.updateSeparators(decimalSeparator, groupingSeparator)
btn_decimal.text = decimalSeparator
}
private fun View.setVibratingOnClick(callback: (view: View) -> Unit) {
setOnClickListener {
callback(it)
checkHaptic(it)
}
}
private fun View.setOnClickOperation(operation: String) {
setVibratingOnClick {
calc.handleOperation(operation)
}
}
}

View file

@ -21,10 +21,11 @@ class NumberFormatHelper(
return formatter.format(d)
}
fun addGroupingSeparators(str: String) = doubleToString(
removeGroupingSeparator(str).toDouble()
)
fun addGroupingSeparators(str: String): String {
return doubleToString(removeGroupingSeparator(str).toDouble())
}
fun removeGroupingSeparator(str: String) =
str.replace(groupingSeparator, "").replace(decimalSeparator, DOT)
fun removeGroupingSeparator(str: String): String {
return str.replace(groupingSeparator, "").replace(decimalSeparator, DOT)
}
}