updating a couple more widget related things
This commit is contained in:
parent
2f9f65ae5b
commit
b75d44b147
8 changed files with 199 additions and 216 deletions
|
@ -10,9 +10,9 @@ import android.view.MenuItem
|
|||
import com.simplemobiletools.calculator.BuildConfig
|
||||
import com.simplemobiletools.calculator.R
|
||||
import com.simplemobiletools.calculator.extensions.config
|
||||
import com.simplemobiletools.calculator.extensions.updateViewColors
|
||||
import com.simplemobiletools.calculator.helpers.*
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.commons.extensions.value
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_ESPRESSO
|
||||
|
@ -22,53 +22,48 @@ import kotlinx.android.synthetic.main.activity_main.*
|
|||
import me.grantland.widget.AutofitHelper
|
||||
|
||||
class MainActivity : SimpleActivity(), Calculator {
|
||||
private var mStoredTextColor = 0
|
||||
|
||||
companion object {
|
||||
private lateinit var mCalc: CalculatorImpl
|
||||
}
|
||||
|
||||
val calc: CalculatorImpl?
|
||||
get() = mCalc
|
||||
private var storedTextColor = 0
|
||||
lateinit var calc: CalculatorImpl
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
btn_plus.setOnClickListener { mCalc.handleOperation(PLUS) }
|
||||
btn_minus.setOnClickListener { mCalc.handleOperation(MINUS) }
|
||||
btn_multiply.setOnClickListener { mCalc.handleOperation(MULTIPLY) }
|
||||
btn_divide.setOnClickListener { mCalc.handleOperation(DIVIDE) }
|
||||
btn_modulo.setOnClickListener { mCalc.handleOperation(MODULO) }
|
||||
btn_power.setOnClickListener { mCalc.handleOperation(POWER) }
|
||||
btn_root.setOnClickListener { mCalc.handleOperation(ROOT) }
|
||||
calc = CalculatorImpl(this, applicationContext)
|
||||
|
||||
btn_clear.setOnClickListener { mCalc.handleClear() }
|
||||
btn_clear.setOnLongClickListener { mCalc.handleReset(); true }
|
||||
btn_plus.setOnClickListener { calc.handleOperation(PLUS) }
|
||||
btn_minus.setOnClickListener { calc.handleOperation(MINUS) }
|
||||
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY) }
|
||||
btn_divide.setOnClickListener { calc.handleOperation(DIVIDE) }
|
||||
btn_modulo.setOnClickListener { calc.handleOperation(MODULO) }
|
||||
btn_power.setOnClickListener { calc.handleOperation(POWER) }
|
||||
btn_root.setOnClickListener { calc.handleOperation(ROOT) }
|
||||
|
||||
btn_clear.setOnClickListener { calc.handleClear() }
|
||||
btn_clear.setOnLongClickListener { calc.handleReset(); true }
|
||||
|
||||
getButtonIds().forEach {
|
||||
it.setOnClickListener { mCalc.numpadClicked(it.id) }
|
||||
it.setOnClickListener { calc.numpadClicked(it.id) }
|
||||
}
|
||||
|
||||
btn_equals.setOnClickListener { mCalc.handleEquals() }
|
||||
btn_equals.setOnClickListener { calc.handleEquals() }
|
||||
formula.setOnLongClickListener { copyToClipboard(false) }
|
||||
result.setOnLongClickListener { copyToClipboard(true) }
|
||||
|
||||
mCalc = CalculatorImpl(this)
|
||||
AutofitHelper.create(result)
|
||||
AutofitHelper.create(formula)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (mStoredTextColor != config.textColor) {
|
||||
updateTextColors(calculator_holder)
|
||||
if (storedTextColor != config.textColor) {
|
||||
updateViewColors(calculator_holder, config.textColor)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
mStoredTextColor = config.textColor
|
||||
storedTextColor = config.textColor
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
|
@ -111,17 +106,17 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||
return true
|
||||
}
|
||||
|
||||
override fun setValue(value: String) {
|
||||
override fun setValue(value: String, context: Context) {
|
||||
result.text = value
|
||||
}
|
||||
|
||||
// used only by Robolectric
|
||||
override fun setValueDouble(d: Double) {
|
||||
mCalc.setValue(Formatter.doubleToString(d))
|
||||
mCalc.setLastKey(DIGIT)
|
||||
calc.setValue(Formatter.doubleToString(d))
|
||||
calc.lastKey = DIGIT
|
||||
}
|
||||
|
||||
override fun setFormula(value: String) {
|
||||
override fun setFormula(value: String, context: Context) {
|
||||
formula.text = value
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import android.content.Intent
|
|||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.SeekBar
|
||||
|
@ -15,6 +14,7 @@ import com.simplemobiletools.calculator.extensions.config
|
|||
import com.simplemobiletools.calculator.helpers.MyWidgetProvider
|
||||
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
|
||||
import com.simplemobiletools.commons.extensions.adjustAlpha
|
||||
import com.simplemobiletools.commons.extensions.beVisible
|
||||
import com.simplemobiletools.commons.extensions.setBackgroundColor
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.widget_config.*
|
||||
|
@ -53,7 +53,7 @@ class WidgetConfigureActivity : AppCompatActivity() {
|
|||
mBgAlpha = Color.alpha(mBgColor) / 255.toFloat()
|
||||
}
|
||||
|
||||
btn_reset.visibility = View.VISIBLE
|
||||
btn_reset.beVisible()
|
||||
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
|
||||
config_bg_seekbar.setOnSeekBarChangeListener(seekbarChangeListener)
|
||||
config_bg_seekbar.progress = (mBgAlpha * 100).toInt()
|
||||
|
|
|
@ -1,6 +1,22 @@
|
|||
package com.simplemobiletools.calculator.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
import com.simplemobiletools.calculator.helpers.Config
|
||||
|
||||
val Context.config: Config get() = Config.newInstance(this)
|
||||
|
||||
// we are reusing the same layout in the app and widget, but cannot use MyTextView etc in a widget, so color regular view types like this
|
||||
fun Context.updateViewColors(viewGroup: ViewGroup, textColor: Int) {
|
||||
val cnt = viewGroup.childCount
|
||||
(0 until cnt).map { viewGroup.getChildAt(it) }
|
||||
.forEach {
|
||||
when (it) {
|
||||
is TextView -> it.setTextColor(textColor)
|
||||
is Button -> it.setTextColor(textColor)
|
||||
is ViewGroup -> updateViewColors(it, textColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.simplemobiletools.calculator.helpers
|
||||
|
||||
import android.content.Context
|
||||
|
||||
interface Calculator {
|
||||
fun setValue(value: String)
|
||||
fun setValue(value: String, context: Context)
|
||||
|
||||
fun setValueDouble(d: Double)
|
||||
|
||||
fun setFormula(value: String)
|
||||
fun setFormula(value: String, context: Context)
|
||||
}
|
||||
|
|
|
@ -1,34 +1,27 @@
|
|||
package com.simplemobiletools.calculator.helpers
|
||||
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.calculator.R
|
||||
import com.simplemobiletools.calculator.operation.OperationFactory
|
||||
|
||||
class CalculatorImpl {
|
||||
class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
var displayedNumber: String? = null
|
||||
var displayedFormula: String? = null
|
||||
private var mLastKey: String? = null
|
||||
var lastKey: String? = null
|
||||
private var mLastOperation: String? = null
|
||||
private var mCallback: Calculator? = null
|
||||
private var mCallback: Calculator? = calculator
|
||||
|
||||
private var mIsFirstOperation = false
|
||||
private var mResetValue = false
|
||||
private var mBaseValue = 0.0
|
||||
private var mSecondValue = 0.0
|
||||
|
||||
constructor(calculator: Calculator) {
|
||||
mCallback = calculator
|
||||
init {
|
||||
resetValues()
|
||||
setValue("0")
|
||||
setFormula("")
|
||||
}
|
||||
|
||||
constructor(calculatorInterface: Calculator, value: String) {
|
||||
mCallback = calculatorInterface
|
||||
resetValues()
|
||||
displayedNumber = value
|
||||
setFormula("")
|
||||
}
|
||||
|
||||
private fun resetValueIfNeeded() {
|
||||
if (mResetValue)
|
||||
displayedNumber = "0"
|
||||
|
@ -40,20 +33,20 @@ class CalculatorImpl {
|
|||
mBaseValue = 0.0
|
||||
mSecondValue = 0.0
|
||||
mResetValue = false
|
||||
mLastKey = ""
|
||||
mLastOperation = ""
|
||||
displayedNumber = ""
|
||||
displayedFormula = ""
|
||||
mIsFirstOperation = true
|
||||
lastKey = ""
|
||||
}
|
||||
|
||||
fun setValue(value: String) {
|
||||
mCallback!!.setValue(value)
|
||||
mCallback!!.setValue(value, context)
|
||||
displayedNumber = value
|
||||
}
|
||||
|
||||
private fun setFormula(value: String) {
|
||||
mCallback!!.setFormula(value)
|
||||
mCallback!!.setFormula(value, context)
|
||||
displayedFormula = value
|
||||
}
|
||||
|
||||
|
@ -69,10 +62,6 @@ class CalculatorImpl {
|
|||
}
|
||||
}
|
||||
|
||||
fun setLastKey(mLastKey: String) {
|
||||
this.mLastKey = mLastKey
|
||||
}
|
||||
|
||||
fun addDigit(number: Int) {
|
||||
val currentValue = displayedNumber
|
||||
val newValue = formatString(currentValue!! + number)
|
||||
|
@ -82,8 +71,9 @@ class CalculatorImpl {
|
|||
private fun formatString(str: String): String {
|
||||
// if the number contains a decimal, do not try removing the leading zero anymore, nor add group separator
|
||||
// it would prevent writing values like 1.02
|
||||
if (str.contains("."))
|
||||
if (str.contains(".")) {
|
||||
return str
|
||||
}
|
||||
|
||||
val doubleValue = Formatter.stringToDouble(str)
|
||||
return Formatter.doubleToString(doubleValue)
|
||||
|
@ -102,7 +92,7 @@ class CalculatorImpl {
|
|||
mBaseValue = getDisplayedNumberAsDouble()
|
||||
}
|
||||
|
||||
fun calculateResult() {
|
||||
private fun calculateResult() {
|
||||
if (!mIsFirstOperation) {
|
||||
updateFormula()
|
||||
}
|
||||
|
@ -117,11 +107,11 @@ class CalculatorImpl {
|
|||
}
|
||||
|
||||
fun handleOperation(operation: String) {
|
||||
if (mLastKey == DIGIT)
|
||||
if (lastKey == DIGIT)
|
||||
handleResult()
|
||||
|
||||
mResetValue = true
|
||||
mLastKey = operation
|
||||
lastKey = operation
|
||||
mLastOperation = operation
|
||||
|
||||
if (operation == ROOT)
|
||||
|
@ -136,8 +126,9 @@ class CalculatorImpl {
|
|||
if (oldValue.contains("-"))
|
||||
minLen++
|
||||
|
||||
if (len > minLen)
|
||||
if (len > minLen) {
|
||||
newValue = oldValue.substring(0, len - 1)
|
||||
}
|
||||
|
||||
newValue = newValue.replace("\\.$".toRegex(), "")
|
||||
newValue = formatString(newValue)
|
||||
|
@ -152,21 +143,22 @@ class CalculatorImpl {
|
|||
}
|
||||
|
||||
fun handleEquals() {
|
||||
if (mLastKey == EQUALS)
|
||||
if (lastKey == EQUALS)
|
||||
calculateResult()
|
||||
|
||||
if (mLastKey != DIGIT)
|
||||
if (lastKey != DIGIT)
|
||||
return
|
||||
|
||||
mSecondValue = getDisplayedNumberAsDouble()
|
||||
calculateResult()
|
||||
mLastKey = EQUALS
|
||||
lastKey = EQUALS
|
||||
}
|
||||
|
||||
private fun decimalClicked() {
|
||||
var value = displayedNumber
|
||||
if (!value!!.contains("."))
|
||||
if (!value!!.contains(".")) {
|
||||
value += "."
|
||||
}
|
||||
setValue(value)
|
||||
}
|
||||
|
||||
|
@ -188,11 +180,11 @@ class CalculatorImpl {
|
|||
}
|
||||
|
||||
fun numpadClicked(id: Int) {
|
||||
if (mLastKey == EQUALS) {
|
||||
if (lastKey == EQUALS) {
|
||||
mLastOperation = EQUALS
|
||||
}
|
||||
|
||||
mLastKey = DIGIT
|
||||
lastKey = DIGIT
|
||||
resetValueIfNeeded()
|
||||
|
||||
when (id) {
|
||||
|
|
|
@ -6,114 +6,87 @@ import android.appwidget.AppWidgetProvider
|
|||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.graphics.Color
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import com.simplemobiletools.calculator.R
|
||||
|
||||
import com.simplemobiletools.calculator.activities.MainActivity
|
||||
import com.simplemobiletools.commons.helpers.PREFS_KEY
|
||||
import com.simplemobiletools.commons.helpers.WIDGET_BG_COLOR
|
||||
import com.simplemobiletools.commons.helpers.WIDGET_TEXT_COLOR
|
||||
import com.simplemobiletools.calculator.extensions.config
|
||||
import com.simplemobiletools.commons.extensions.setBackgroundColor
|
||||
import com.simplemobiletools.commons.extensions.setText
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
||||
|
||||
companion object {
|
||||
private var mRemoteViews: RemoteViews? = null
|
||||
private var mCalc: CalculatorImpl? = null
|
||||
private var mWidgetManager: AppWidgetManager? = null
|
||||
private var mIntent: Intent? = null
|
||||
private var mContext: Context? = null
|
||||
private var mPrefs: SharedPreferences? = null
|
||||
|
||||
private var mWidgetIds: IntArray? = null
|
||||
private var calc: CalculatorImpl? = null
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
initVariables(context)
|
||||
|
||||
mIntent = Intent(context, MyWidgetProvider::class.java)
|
||||
setupIntent(DECIMAL, R.id.btn_decimal)
|
||||
setupIntent(ZERO, R.id.btn_0)
|
||||
setupIntent(ONE, R.id.btn_1)
|
||||
setupIntent(TWO, R.id.btn_2)
|
||||
setupIntent(THREE, R.id.btn_3)
|
||||
setupIntent(FOUR, R.id.btn_4)
|
||||
setupIntent(FIVE, R.id.btn_5)
|
||||
setupIntent(SIX, R.id.btn_6)
|
||||
setupIntent(SEVEN, R.id.btn_7)
|
||||
setupIntent(EIGHT, R.id.btn_8)
|
||||
setupIntent(NINE, R.id.btn_9)
|
||||
|
||||
setupIntent(EQUALS, R.id.btn_equals)
|
||||
setupIntent(PLUS, R.id.btn_plus)
|
||||
setupIntent(MINUS, R.id.btn_minus)
|
||||
setupIntent(MULTIPLY, R.id.btn_multiply)
|
||||
setupIntent(DIVIDE, R.id.btn_divide)
|
||||
setupIntent(MODULO, R.id.btn_modulo)
|
||||
setupIntent(POWER, R.id.btn_power)
|
||||
setupIntent(ROOT, R.id.btn_root)
|
||||
setupIntent(CLEAR, R.id.btn_clear)
|
||||
setupIntent(RESET, R.id.btn_reset)
|
||||
|
||||
setupAppOpenIntent(R.id.formula)
|
||||
setupAppOpenIntent(R.id.result)
|
||||
|
||||
updateWidget()
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
private fun setupIntent(action: String, id: Int) {
|
||||
mIntent!!.action = action
|
||||
val pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0)
|
||||
mRemoteViews!!.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
private fun performUpdate(context: Context) {
|
||||
Log.e("DEBUG", "perform update")
|
||||
val config = context.config
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
val views = RemoteViews(context.packageName, R.layout.activity_main)
|
||||
setupIntent(context, views, DECIMAL, R.id.btn_decimal)
|
||||
setupIntent(context, views, ZERO, R.id.btn_0)
|
||||
setupIntent(context, views, ONE, R.id.btn_1)
|
||||
setupIntent(context, views, TWO, R.id.btn_2)
|
||||
setupIntent(context, views, THREE, R.id.btn_3)
|
||||
setupIntent(context, views, FOUR, R.id.btn_4)
|
||||
setupIntent(context, views, FIVE, R.id.btn_5)
|
||||
setupIntent(context, views, SIX, R.id.btn_6)
|
||||
setupIntent(context, views, SEVEN, R.id.btn_7)
|
||||
setupIntent(context, views, EIGHT, R.id.btn_8)
|
||||
setupIntent(context, views, NINE, R.id.btn_9)
|
||||
|
||||
private fun setupAppOpenIntent(id: Int) {
|
||||
val intent = Intent(mContext, MainActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0)
|
||||
mRemoteViews!!.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
setupIntent(context, views, EQUALS, R.id.btn_equals)
|
||||
setupIntent(context, views, PLUS, R.id.btn_plus)
|
||||
setupIntent(context, views, MINUS, R.id.btn_minus)
|
||||
setupIntent(context, views, MULTIPLY, R.id.btn_multiply)
|
||||
setupIntent(context, views, DIVIDE, R.id.btn_divide)
|
||||
setupIntent(context, views, MODULO, R.id.btn_modulo)
|
||||
setupIntent(context, views, POWER, R.id.btn_power)
|
||||
setupIntent(context, views, ROOT, R.id.btn_root)
|
||||
setupIntent(context, views, CLEAR, R.id.btn_clear)
|
||||
setupIntent(context, views, RESET, R.id.btn_reset)
|
||||
|
||||
private fun initVariables(context: Context) {
|
||||
mContext = context
|
||||
updateWidgetIds()
|
||||
mPrefs = initPrefs(mContext)
|
||||
val defaultColor = mContext!!.resources.getColor(R.color.text_grey)
|
||||
val newBgColor = mPrefs!!.getInt(WIDGET_BG_COLOR, defaultColor)
|
||||
val newTextColor = mPrefs!!.getInt(WIDGET_TEXT_COLOR, Color.WHITE)
|
||||
setupAppOpenIntent(context, views, R.id.formula)
|
||||
setupAppOpenIntent(context, views, R.id.result)
|
||||
|
||||
mRemoteViews = RemoteViews(mContext!!.packageName, R.layout.activity_main)
|
||||
mRemoteViews!!.setViewVisibility(R.id.btn_reset, View.VISIBLE)
|
||||
mRemoteViews!!.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor)
|
||||
views.setViewVisibility(R.id.btn_reset, View.VISIBLE)
|
||||
views.setBackgroundColor(R.id.calculator_holder, config.widgetBgColor)
|
||||
|
||||
updateTextColors(newTextColor)
|
||||
mWidgetManager = AppWidgetManager.getInstance(mContext)
|
||||
|
||||
val displayValue = "0"
|
||||
mCalc = CalculatorImpl(this, displayValue)
|
||||
}
|
||||
|
||||
private fun updateWidgetIds() {
|
||||
val component = ComponentName(mContext!!, MyWidgetProvider::class.java)
|
||||
mWidgetManager = AppWidgetManager.getInstance(mContext)
|
||||
mWidgetIds = mWidgetManager!!.getAppWidgetIds(component)
|
||||
}
|
||||
|
||||
private fun updateWidget() {
|
||||
for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
|
||||
updateTextColors(views, config.widgetTextColor)
|
||||
appWidgetManager.updateAppWidget(it, views)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initPrefs(context: Context?) = context!!.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
|
||||
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetProvider::class.java)
|
||||
|
||||
private fun updateTextColors(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_modulo, 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_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
|
||||
private fun setupIntent(context: Context, views: RemoteViews, action: String, id: Int) {
|
||||
Intent(context, MyWidgetProvider::class.java).apply {
|
||||
this.action = action
|
||||
val pendingIntent = PendingIntent.getBroadcast(context, 0, this, 0)
|
||||
views.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) {
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
|
||||
views.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
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_modulo, 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_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
|
||||
|
||||
for (i in viewIds) {
|
||||
mRemoteViews!!.setInt(i, "setTextColor", color)
|
||||
views.setTextColor(i, color)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,48 +99,53 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
|||
}
|
||||
|
||||
private fun myAction(action: String, context: Context) {
|
||||
if (mCalc == null || mRemoteViews == null || mWidgetManager == null || mPrefs == null || mContext == null) {
|
||||
initVariables(context)
|
||||
Log.e("DEBUG", "myaction $calc")
|
||||
if (calc == null) {
|
||||
calc = CalculatorImpl(this, context)
|
||||
}
|
||||
|
||||
when (action) {
|
||||
DECIMAL -> mCalc!!.numpadClicked(R.id.btn_decimal)
|
||||
ZERO -> mCalc!!.numpadClicked(R.id.btn_0)
|
||||
ONE -> mCalc!!.numpadClicked(R.id.btn_1)
|
||||
TWO -> mCalc!!.numpadClicked(R.id.btn_2)
|
||||
THREE -> mCalc!!.numpadClicked(R.id.btn_3)
|
||||
FOUR -> mCalc!!.numpadClicked(R.id.btn_4)
|
||||
FIVE -> mCalc!!.numpadClicked(R.id.btn_5)
|
||||
SIX -> mCalc!!.numpadClicked(R.id.btn_6)
|
||||
SEVEN -> mCalc!!.numpadClicked(R.id.btn_7)
|
||||
EIGHT -> mCalc!!.numpadClicked(R.id.btn_8)
|
||||
NINE -> mCalc!!.numpadClicked(R.id.btn_9)
|
||||
EQUALS -> mCalc!!.handleEquals()
|
||||
CLEAR -> mCalc!!.handleClear()
|
||||
RESET -> mCalc!!.handleReset()
|
||||
PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> mCalc!!.handleOperation(action)
|
||||
else -> {
|
||||
}
|
||||
DECIMAL -> calc!!.numpadClicked(R.id.btn_decimal)
|
||||
ZERO -> calc!!.numpadClicked(R.id.btn_0)
|
||||
ONE -> calc!!.numpadClicked(R.id.btn_1)
|
||||
TWO -> calc!!.numpadClicked(R.id.btn_2)
|
||||
THREE -> calc!!.numpadClicked(R.id.btn_3)
|
||||
FOUR -> calc!!.numpadClicked(R.id.btn_4)
|
||||
FIVE -> calc!!.numpadClicked(R.id.btn_5)
|
||||
SIX -> calc!!.numpadClicked(R.id.btn_6)
|
||||
SEVEN -> calc!!.numpadClicked(R.id.btn_7)
|
||||
EIGHT -> calc!!.numpadClicked(R.id.btn_8)
|
||||
NINE -> calc!!.numpadClicked(R.id.btn_9)
|
||||
EQUALS -> calc!!.handleEquals()
|
||||
CLEAR -> calc!!.handleClear()
|
||||
RESET -> calc!!.handleReset()
|
||||
PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> calc!!.handleOperation(action)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setValue(value: String) {
|
||||
mRemoteViews!!.setTextViewText(R.id.result, value)
|
||||
updateWidget()
|
||||
override fun setValue(value: String, context: Context) {
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
val views = RemoteViews(context.packageName, R.layout.activity_main)
|
||||
views.setText(R.id.result, value)
|
||||
appWidgetManager.partiallyUpdateAppWidget(it, views)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setValueDouble(d: Double) {
|
||||
|
||||
}
|
||||
|
||||
override fun setFormula(value: String) {
|
||||
mRemoteViews!!.setTextViewText(R.id.formula, value)
|
||||
updateWidget()
|
||||
override fun setFormula(value: String, context: Context) {
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
val views = RemoteViews(context.packageName, R.layout.activity_main)
|
||||
views.setText(R.id.formula, value)
|
||||
appWidgetManager.partiallyUpdateAppWidget(it, views)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||
override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
|
||||
super.onDeleted(context, appWidgetIds)
|
||||
if (mContext != null)
|
||||
updateWidgetIds()
|
||||
calc = null
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
tools:context=".activities.MainActivity"
|
||||
tools:ignore="HardcodedText">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
<TextView
|
||||
android:id="@+id/formula"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
@ -21,7 +21,7 @@
|
|||
android:paddingRight="@dimen/activity_margin"
|
||||
android:textSize="@dimen/actionbar_text_size"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
<TextView
|
||||
android:id="@+id/result"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
@ -40,7 +40,7 @@
|
|||
android:layout_weight="2"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_modulo"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -50,7 +50,7 @@
|
|||
android:textAllCaps="false"
|
||||
android:textSize="@dimen/extra_big_text_size"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_power"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -58,7 +58,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="^"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_root"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -66,7 +66,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="√"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_clear"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -74,7 +74,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="C"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_reset"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -90,7 +90,7 @@
|
|||
android:layout_weight="2"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_7"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -98,7 +98,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="7"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_8"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -106,7 +106,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="8"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_9"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -114,7 +114,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="9"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_divide"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -129,7 +129,7 @@
|
|||
android:layout_weight="2"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_4"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -137,7 +137,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="4"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_5"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -145,7 +145,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="5"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_6"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -153,7 +153,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="6"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_multiply"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -168,7 +168,7 @@
|
|||
android:layout_weight="2"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_1"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -176,7 +176,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="1"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_2"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -184,7 +184,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="2"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_3"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -192,7 +192,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="3"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_minus"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -207,7 +207,7 @@
|
|||
android:layout_weight="2"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_0"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -215,7 +215,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="0"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_decimal"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -223,7 +223,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="."/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_equals"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -231,7 +231,7 @@
|
|||
android:layout_weight="1"
|
||||
android:text="="/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyButton
|
||||
<Button
|
||||
android:id="@+id/btn_plus"
|
||||
style="@style/MyButton"
|
||||
android:layout_width="match_parent"
|
||||
|
|
|
@ -13,13 +13,13 @@ import org.robolectric.annotation.Config
|
|||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(constants = BuildConfig::class, sdk = intArrayOf(21))
|
||||
class MainActivityTest {
|
||||
internal lateinit var activity: MainActivity
|
||||
private lateinit var activity: MainActivity
|
||||
|
||||
private val displayedNumber: String?
|
||||
get() = activity.calc!!.displayedNumber
|
||||
get() = activity.calc.displayedNumber
|
||||
|
||||
private val displayedFormula: String?
|
||||
get() = activity.calc!!.displayedFormula
|
||||
get() = activity.calc.displayedFormula
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
|
@ -28,14 +28,14 @@ class MainActivityTest {
|
|||
|
||||
@Test
|
||||
fun addSimpleDigit() {
|
||||
activity.calc!!.addDigit(2)
|
||||
activity.calc.addDigit(2)
|
||||
assertEquals("2", displayedNumber)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeLeadingZero() {
|
||||
activity.calc!!.addDigit(0)
|
||||
activity.calc!!.addDigit(5)
|
||||
activity.calc.addDigit(0)
|
||||
activity.calc.addDigit(5)
|
||||
assertEquals("5", displayedNumber)
|
||||
}
|
||||
|
||||
|
@ -99,26 +99,26 @@ class MainActivityTest {
|
|||
@Test
|
||||
fun clearBtnSimpleTest() {
|
||||
setDouble(156.0)
|
||||
activity.calc!!.handleClear()
|
||||
activity.calc.handleClear()
|
||||
assertEquals("15", displayedNumber)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clearBtnComplexTest() {
|
||||
setDouble(-26.0)
|
||||
activity.calc!!.handleClear()
|
||||
activity.calc.handleClear()
|
||||
assertEquals("-2", displayedNumber)
|
||||
activity.calc!!.handleClear()
|
||||
activity.calc.handleClear()
|
||||
assertEquals("0", displayedNumber)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clearBtnLongClick_resetsEverything() {
|
||||
calcResult(-1.2, PLUS, 3.4)
|
||||
activity.calc!!.handleReset()
|
||||
activity.calc.handleReset()
|
||||
handleOperation(PLUS)
|
||||
setDouble(3.0)
|
||||
activity.calc!!.handleResult()
|
||||
activity.calc.handleResult()
|
||||
assertEquals("3", displayedNumber)
|
||||
checkFormula("")
|
||||
}
|
||||
|
@ -133,10 +133,10 @@ class MainActivityTest {
|
|||
checkFormula("-12.2+21")
|
||||
|
||||
setDouble(1.6)
|
||||
activity.calc!!.handleEquals()
|
||||
activity.calc.handleEquals()
|
||||
assertEquals("7.2", displayedNumber)
|
||||
checkFormula("8.8-1.6")
|
||||
activity.calc!!.handleEquals()
|
||||
activity.calc.handleEquals()
|
||||
assertEquals("5.6", displayedNumber)
|
||||
checkFormula("7.2-1.6")
|
||||
|
||||
|
@ -161,7 +161,7 @@ class MainActivityTest {
|
|||
assertEquals("16", displayedNumber)
|
||||
checkFormula("√256")
|
||||
|
||||
activity.calc!!.handleClear()
|
||||
activity.calc.handleClear()
|
||||
assertEquals("1", displayedNumber)
|
||||
}
|
||||
|
||||
|
@ -170,7 +170,7 @@ class MainActivityTest {
|
|||
}
|
||||
|
||||
private fun handleOperation(operation: String) {
|
||||
activity.calc!!.handleOperation(operation)
|
||||
activity.calc.handleOperation(operation)
|
||||
}
|
||||
|
||||
private fun checkFormula(desired: String) {
|
||||
|
@ -181,7 +181,7 @@ class MainActivityTest {
|
|||
setDouble(baseValue)
|
||||
handleOperation(operation)
|
||||
setDouble(secondValue)
|
||||
activity.calc!!.handleResult()
|
||||
activity.calc.handleResult()
|
||||
return displayedNumber
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue