final changes

This commit is contained in:
Sissy Dalampira 2018-10-04 22:06:18 +03:00
parent 88565fc748
commit c489247bc4
6 changed files with 21 additions and 5 deletions

View File

@ -34,6 +34,7 @@ class MainActivity : SimpleActivity(), Calculator {
calc = CalculatorImpl(this, applicationContext) calc = CalculatorImpl(this, applicationContext)
btn_factorial.setOnClickListener{ calc.handleOperation(FACTORIAL); checkHaptic(it) }
btn_plus.setOnClickListener { calc.handleOperation(PLUS); checkHaptic(it) } btn_plus.setOnClickListener { calc.handleOperation(PLUS); checkHaptic(it) }
btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) } btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) }
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) } btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) }

View File

@ -111,7 +111,7 @@ class WidgetConfigureActivity : SimpleActivity() {
config_save.setTextColor(mTextColor) 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, 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) R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
result.setTextColor(mTextColor) result.setTextColor(mTextColor)
formula.setTextColor(mTextColor) formula.setTextColor(mTextColor)

View File

@ -58,6 +58,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
if (sign == "") { if (sign == "") {
setFormula(sign + first) setFormula(sign + first)
} else if (sign == "!"){
setFormula(first + sign)
} else if (!sign.isEmpty()) { } else if (!sign.isEmpty()) {
var formula = first + sign + second var formula = first + sign + second
if (mWasPercentLast) { if (mWasPercentLast) {
@ -102,6 +104,11 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
calculateResult() calculateResult()
} }
private fun handleFactorial() {
mBaseValue = getDisplayedNumberAsDouble()
calculateResult()
}
private fun calculateResult() { private fun calculateResult() {
updateFormula() updateFormula()
if (mWasPercentLast) { if (mWasPercentLast) {
@ -118,7 +125,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
fun handleOperation(operation: String) { fun handleOperation(operation: String) {
mWasPercentLast = operation == PERCENT mWasPercentLast = operation == PERCENT
if (lastKey == DIGIT && operation != ROOT) { if (lastKey == DIGIT && operation != ROOT && operation!= FACTORIAL) {
handleResult() handleResult()
} }
@ -130,6 +137,10 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
handleRoot() handleRoot()
mResetValue = false mResetValue = false
} }
if (operation == FACTORIAL) {
handleFactorial()
mResetValue = false
}
} }
fun handleClear() { fun handleClear() {
@ -195,6 +206,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
PERCENT -> "%" PERCENT -> "%"
POWER -> "^" POWER -> "^"
ROOT -> "" ROOT -> ""
FACTORIAL -> "!"
else -> "" else -> ""
} }

View File

@ -12,6 +12,7 @@ const val ROOT = "root"
const val DECIMAL = "decimal" const val DECIMAL = "decimal"
const val CLEAR = "clear" const val CLEAR = "clear"
const val RESET = "reset" const val RESET = "reset"
const val FACTORIAL = "factorial"
const val NAN = "NaN" const val NAN = "NaN"
const val ZERO = "zero" const val ZERO = "zero"

View File

@ -48,6 +48,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
setupIntent(context, views, PERCENT, R.id.btn_percent) setupIntent(context, views, PERCENT, R.id.btn_percent)
setupIntent(context, views, POWER, R.id.btn_power) setupIntent(context, views, POWER, R.id.btn_power)
setupIntent(context, views, ROOT, R.id.btn_root) 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, CLEAR, R.id.btn_clear)
setupIntent(context, views, RESET, R.id.btn_reset) setupIntent(context, views, RESET, R.id.btn_reset)
@ -80,7 +81,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
private fun updateTextColors(views: RemoteViews, color: Int) { 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, 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) R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
for (i in viewIds) { for (i in viewIds) {
@ -91,7 +92,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val action = intent.action val action = intent.action
when (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) else -> super.onReceive(context, intent)
} }
} }
@ -116,7 +117,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
EQUALS -> calc!!.handleEquals() EQUALS -> calc!!.handleEquals()
CLEAR -> calc!!.handleClear() CLEAR -> calc!!.handleClear()
RESET -> calc!!.handleReset() RESET -> calc!!.handleReset()
PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> calc!!.handleOperation(action) PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT, FACTORIAL -> calc!!.handleOperation(action)
} }
} }

View File

@ -14,6 +14,7 @@ object OperationFactory {
PERCENT -> PercentOperation(baseValue, secondValue) PERCENT -> PercentOperation(baseValue, secondValue)
POWER -> PowerOperation(baseValue, secondValue) POWER -> PowerOperation(baseValue, secondValue)
ROOT -> RootOperation(baseValue) ROOT -> RootOperation(baseValue)
FACTORIAL -> ParOperation(baseValue)
else -> null else -> null
} }
} }