From 95feb32d0fa48b1612afdfb6dbbc04a84bc0f12f Mon Sep 17 00:00:00 2001 From: Danil Ochagov Date: Tue, 19 May 2020 15:03:53 +0300 Subject: [PATCH] Show Toast message instead of printing division by zero error --- .../calculator/helpers/CalculatorImpl.kt | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 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 9e8ad843..9520ee9e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.calculator.helpers import android.content.Context import android.util.Log +import android.widget.Toast import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.operation.OperationFactory @@ -57,19 +58,21 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { val sign = getSign(lastOperation) when { - baseValue == 0.0 && secondValue == 0.0 && sign == "/" -> { - setFormula(context.getString(R.string.formula_divide_by_zero_error)) + sign == "√" -> { + setFormula(sign + first) } - - sign == "√" -> setFormula(sign + first) - - sign == "!" -> setFormula(first + sign) - - else -> { - if (sign.isNotEmpty()) { - val formula = first + sign + second - setFormula(formula) + sign == "!" -> { + setFormula(first + sign) + } + sign.isNotEmpty() -> { + if (secondValue == 0.0 && sign == "/") { + Toast.makeText( + context, + context.getString(R.string.formula_divide_by_zero_error), + Toast.LENGTH_SHORT).show() } + + setFormula(first + sign + second) } } }