From a7cb74eb700c27d23543237a4a945d18dd3d0b99 Mon Sep 17 00:00:00 2001 From: dagkalis Date: Thu, 26 Aug 2021 21:48:46 +0300 Subject: [PATCH 1/3] turn number to negative on minus-btn long-press --- .../calculator/activities/MainActivity.kt | 4 ++++ .../calculator/helpers/CalculatorImpl.kt | 15 +++++++++++++++ 2 files changed, 19 insertions(+) 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 007ac11..3dde21c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt @@ -42,6 +42,10 @@ class MainActivity : SimpleActivity(), Calculator { btn_power.setOnClickListener { calc.handleOperation(POWER); checkHaptic(it) } btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) } + btn_minus.setOnLongClickListener { + calc.turnToNegative(); + } + btn_clear.setOnClickListener { calc.handleClear(); checkHaptic(it) } btn_clear.setOnLongClickListener { calc.handleReset(); true } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt index 70c9b8f..aa972a5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -123,6 +123,21 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) { showNewResult(inputDisplayedFormula) } + fun turnToNegative() : Boolean{ + // turn to negative only if there is a baseValue alone ( not 0 ) + if (!inputDisplayedFormula.trimStart('-').any { it.toString() in operations } && inputDisplayedFormula.replace(",", "").toDouble() != 0.0){ + + if(inputDisplayedFormula.first() == '-') { + inputDisplayedFormula = inputDisplayedFormula.substring(1) + }else{ + inputDisplayedFormula = "-$inputDisplayedFormula" + } + showNewResult(inputDisplayedFormula) + return true + } + return false + } + // handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there private fun handlePercent() { var result = calculatePercentage(baseValue, getSecondValue(), lastOperation) From 97b9e4ab2c47d6ad3d809dbb470703b0a597d69a Mon Sep 17 00:00:00 2001 From: dagkalis Date: Sun, 29 Aug 2021 18:28:47 +0300 Subject: [PATCH 2/3] code reformat --- .../calculator/activities/MainActivity.kt | 2 +- .../calculator/helpers/CalculatorImpl.kt | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) 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 3dde21c..c363d6e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt @@ -43,7 +43,7 @@ class MainActivity : SimpleActivity(), Calculator { btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) } btn_minus.setOnLongClickListener { - calc.turnToNegative(); + calc.turnToNegative() } btn_clear.setOnClickListener { calc.handleClear(); checkHaptic(it) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt index aa972a5..fe0f495 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -123,18 +123,19 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) { showNewResult(inputDisplayedFormula) } - fun turnToNegative() : Boolean{ + fun turnToNegative(): Boolean { // turn to negative only if there is a baseValue alone ( not 0 ) - if (!inputDisplayedFormula.trimStart('-').any { it.toString() in operations } && inputDisplayedFormula.replace(",", "").toDouble() != 0.0){ - - if(inputDisplayedFormula.first() == '-') { + if (!inputDisplayedFormula.trimStart('-').any { it.toString() in operations } && inputDisplayedFormula.replace(",", "").toDouble() != 0.0) { + if (inputDisplayedFormula.first() == '-') { inputDisplayedFormula = inputDisplayedFormula.substring(1) - }else{ + } else { inputDisplayedFormula = "-$inputDisplayedFormula" } + showNewResult(inputDisplayedFormula) return true } + return false } From fb22d7ba312fd6f10a63bffc6ccae6c9fa748082 Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Wed, 3 Nov 2021 23:05:18 +0100 Subject: [PATCH 3/3] fixing a crash at long pressing C, then long pressing minus --- .../calculator/helpers/CalculatorImpl.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt index fe0f495..78edbcc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -124,12 +124,15 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) { } fun turnToNegative(): Boolean { - // turn to negative only if there is a baseValue alone ( not 0 ) + if (inputDisplayedFormula.isEmpty()) { + return false + } + if (!inputDisplayedFormula.trimStart('-').any { it.toString() in operations } && inputDisplayedFormula.replace(",", "").toDouble() != 0.0) { - if (inputDisplayedFormula.first() == '-') { - inputDisplayedFormula = inputDisplayedFormula.substring(1) + inputDisplayedFormula = if (inputDisplayedFormula.first() == '-') { + inputDisplayedFormula.substring(1) } else { - inputDisplayedFormula = "-$inputDisplayedFormula" + "-$inputDisplayedFormula" } showNewResult(inputDisplayedFormula)