diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt index 6a1c8f3..95a226e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt @@ -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) + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/NumberFormatHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/NumberFormatHelper.kt index e543eb0..eb8d3e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/NumberFormatHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/NumberFormatHelper.kt @@ -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) + } }