final changes
This commit is contained in:
parent
88565fc748
commit
c489247bc4
|
@ -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) }
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 -> ""
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ object OperationFactory {
|
|||
PERCENT -> PercentOperation(baseValue, secondValue)
|
||||
POWER -> PowerOperation(baseValue, secondValue)
|
||||
ROOT -> RootOperation(baseValue)
|
||||
FACTORIAL -> ParOperation(baseValue)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue