From c489247bc4b1422d9193b596c99f3c77113256a4 Mon Sep 17 00:00:00 2001 From: Sissy Dalampira Date: Thu, 4 Oct 2018 22:06:18 +0300 Subject: [PATCH] final changes --- .../calculator/activities/MainActivity.kt | 1 + .../activities/WidgetConfigureActivity.kt | 2 +- .../calculator/helpers/CalculatorImpl.kt | 14 +++++++++++++- .../calculator/helpers/Constants.kt | 1 + .../calculator/helpers/MyWidgetProvider.kt | 7 ++++--- .../calculator/operation/OperationFactory.kt | 1 + 6 files changed, 21 insertions(+), 5 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 2b648036..52e82604 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt @@ -34,6 +34,7 @@ class MainActivity : SimpleActivity(), Calculator { calc = CalculatorImpl(this, applicationContext) + btn_factorial.setOnClickListener{ calc.handleOperation(FACTORIAL); checkHaptic(it) } btn_plus.setOnClickListener { calc.handleOperation(PLUS); checkHaptic(it) } btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) } btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/WidgetConfigureActivity.kt index c133cfa9..0a98dd2b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/WidgetConfigureActivity.kt @@ -111,7 +111,7 @@ class WidgetConfigureActivity : SimpleActivity() { config_save.setTextColor(mTextColor) val viewIds = intArrayOf(R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, - R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_multiply, + R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_factorial, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals) result.setTextColor(mTextColor) formula.setTextColor(mTextColor) 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 ef49e15e..5706849d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -58,6 +58,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { if (sign == "√") { setFormula(sign + first) + } else if (sign == "!"){ + setFormula(first + sign) } else if (!sign.isEmpty()) { var formula = first + sign + second if (mWasPercentLast) { @@ -102,6 +104,11 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { calculateResult() } + private fun handleFactorial() { + mBaseValue = getDisplayedNumberAsDouble() + calculateResult() + } + private fun calculateResult() { updateFormula() if (mWasPercentLast) { @@ -118,7 +125,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { fun handleOperation(operation: String) { mWasPercentLast = operation == PERCENT - if (lastKey == DIGIT && operation != ROOT) { + if (lastKey == DIGIT && operation != ROOT && operation!= FACTORIAL) { handleResult() } @@ -130,6 +137,10 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { handleRoot() mResetValue = false } + if (operation == FACTORIAL) { + handleFactorial() + mResetValue = false + } } fun handleClear() { @@ -195,6 +206,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { PERCENT -> "%" POWER -> "^" ROOT -> "√" + FACTORIAL -> "!" else -> "" } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt index 8e8af427..5f30e061 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt @@ -12,6 +12,7 @@ const val ROOT = "root" const val DECIMAL = "decimal" const val CLEAR = "clear" const val RESET = "reset" +const val FACTORIAL = "factorial" const val NAN = "NaN" const val ZERO = "zero" diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/MyWidgetProvider.kt index ecf467dd..925c476d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/MyWidgetProvider.kt @@ -48,6 +48,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator { setupIntent(context, views, PERCENT, R.id.btn_percent) setupIntent(context, views, POWER, R.id.btn_power) setupIntent(context, views, ROOT, R.id.btn_root) + setupIntent(context, views, FACTORIAL, R.id.btn_factorial ) setupIntent(context, views, CLEAR, R.id.btn_clear) setupIntent(context, views, RESET, R.id.btn_reset) @@ -80,7 +81,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator { private fun updateTextColors(views: RemoteViews, color: Int) { val viewIds = intArrayOf(R.id.formula, R.id.result, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, - R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, + R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_factorial, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals) for (i in viewIds) { @@ -91,7 +92,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator { override fun onReceive(context: Context, intent: Intent) { val action = intent.action when (action) { - DECIMAL, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, EQUALS, CLEAR, RESET, PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> myAction(action, context) + DECIMAL, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, EQUALS, CLEAR, RESET, PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT, FACTORIAL -> myAction(action, context) else -> super.onReceive(context, intent) } } @@ -116,7 +117,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator { EQUALS -> calc!!.handleEquals() CLEAR -> calc!!.handleClear() RESET -> calc!!.handleReset() - PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> calc!!.handleOperation(action) + PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT, FACTORIAL -> calc!!.handleOperation(action) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/operation/OperationFactory.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/operation/OperationFactory.kt index 48694337..9c8f422f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/operation/OperationFactory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/operation/OperationFactory.kt @@ -14,6 +14,7 @@ object OperationFactory { PERCENT -> PercentOperation(baseValue, secondValue) POWER -> PowerOperation(baseValue, secondValue) ROOT -> RootOperation(baseValue) + FACTORIAL -> ParOperation(baseValue) else -> null } }