updating a couple more widget related things

This commit is contained in:
tibbi
2017-11-05 20:25:04 +01:00
parent 2f9f65ae5b
commit b75d44b147
8 changed files with 199 additions and 216 deletions

View File

@ -10,9 +10,9 @@ import android.view.MenuItem
import com.simplemobiletools.calculator.BuildConfig import com.simplemobiletools.calculator.BuildConfig
import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.extensions.config import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.updateViewColors
import com.simplemobiletools.calculator.helpers.* import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.extensions.value import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW
import com.simplemobiletools.commons.helpers.LICENSE_ESPRESSO import com.simplemobiletools.commons.helpers.LICENSE_ESPRESSO
@ -22,53 +22,48 @@ import kotlinx.android.synthetic.main.activity_main.*
import me.grantland.widget.AutofitHelper import me.grantland.widget.AutofitHelper
class MainActivity : SimpleActivity(), Calculator { class MainActivity : SimpleActivity(), Calculator {
private var mStoredTextColor = 0 private var storedTextColor = 0
lateinit var calc: CalculatorImpl
companion object {
private lateinit var mCalc: CalculatorImpl
}
val calc: CalculatorImpl?
get() = mCalc
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
btn_plus.setOnClickListener { mCalc.handleOperation(PLUS) } calc = CalculatorImpl(this, applicationContext)
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) }
btn_clear.setOnClickListener { mCalc.handleClear() } btn_plus.setOnClickListener { calc.handleOperation(PLUS) }
btn_clear.setOnLongClickListener { mCalc.handleReset(); true } 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 { 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) } formula.setOnLongClickListener { copyToClipboard(false) }
result.setOnLongClickListener { copyToClipboard(true) } result.setOnLongClickListener { copyToClipboard(true) }
mCalc = CalculatorImpl(this)
AutofitHelper.create(result) AutofitHelper.create(result)
AutofitHelper.create(formula) AutofitHelper.create(formula)
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (mStoredTextColor != config.textColor) { if (storedTextColor != config.textColor) {
updateTextColors(calculator_holder) updateViewColors(calculator_holder, config.textColor)
} }
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
mStoredTextColor = config.textColor storedTextColor = config.textColor
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -111,17 +106,17 @@ class MainActivity : SimpleActivity(), Calculator {
return true return true
} }
override fun setValue(value: String) { override fun setValue(value: String, context: Context) {
result.text = value result.text = value
} }
// used only by Robolectric // used only by Robolectric
override fun setValueDouble(d: Double) { override fun setValueDouble(d: Double) {
mCalc.setValue(Formatter.doubleToString(d)) calc.setValue(Formatter.doubleToString(d))
mCalc.setLastKey(DIGIT) calc.lastKey = DIGIT
} }
override fun setFormula(value: String) { override fun setFormula(value: String, context: Context) {
formula.text = value formula.text = value
} }
} }

View File

@ -6,7 +6,6 @@ import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.RemoteViews import android.widget.RemoteViews
import android.widget.SeekBar import android.widget.SeekBar
@ -15,6 +14,7 @@ import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.helpers.MyWidgetProvider import com.simplemobiletools.calculator.helpers.MyWidgetProvider
import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.adjustAlpha import com.simplemobiletools.commons.extensions.adjustAlpha
import com.simplemobiletools.commons.extensions.beVisible
import com.simplemobiletools.commons.extensions.setBackgroundColor import com.simplemobiletools.commons.extensions.setBackgroundColor
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.widget_config.* import kotlinx.android.synthetic.main.widget_config.*
@ -53,7 +53,7 @@ class WidgetConfigureActivity : AppCompatActivity() {
mBgAlpha = Color.alpha(mBgColor) / 255.toFloat() 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)) mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
config_bg_seekbar.setOnSeekBarChangeListener(seekbarChangeListener) config_bg_seekbar.setOnSeekBarChangeListener(seekbarChangeListener)
config_bg_seekbar.progress = (mBgAlpha * 100).toInt() config_bg_seekbar.progress = (mBgAlpha * 100).toInt()

View File

@ -1,6 +1,22 @@
package com.simplemobiletools.calculator.extensions package com.simplemobiletools.calculator.extensions
import android.content.Context import android.content.Context
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.simplemobiletools.calculator.helpers.Config import com.simplemobiletools.calculator.helpers.Config
val Context.config: Config get() = Config.newInstance(this) 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)
}
}
}

View File

@ -1,9 +1,11 @@
package com.simplemobiletools.calculator.helpers package com.simplemobiletools.calculator.helpers
import android.content.Context
interface Calculator { interface Calculator {
fun setValue(value: String) fun setValue(value: String, context: Context)
fun setValueDouble(d: Double) fun setValueDouble(d: Double)
fun setFormula(value: String) fun setFormula(value: String, context: Context)
} }

View File

@ -1,34 +1,27 @@
package com.simplemobiletools.calculator.helpers package com.simplemobiletools.calculator.helpers
import android.content.Context
import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.operation.OperationFactory import com.simplemobiletools.calculator.operation.OperationFactory
class CalculatorImpl { class CalculatorImpl(calculator: Calculator, val context: Context) {
var displayedNumber: String? = null var displayedNumber: String? = null
var displayedFormula: String? = null var displayedFormula: String? = null
private var mLastKey: String? = null var lastKey: String? = null
private var mLastOperation: String? = null private var mLastOperation: String? = null
private var mCallback: Calculator? = null private var mCallback: Calculator? = calculator
private var mIsFirstOperation = false private var mIsFirstOperation = false
private var mResetValue = false private var mResetValue = false
private var mBaseValue = 0.0 private var mBaseValue = 0.0
private var mSecondValue = 0.0 private var mSecondValue = 0.0
constructor(calculator: Calculator) { init {
mCallback = calculator
resetValues() resetValues()
setValue("0") setValue("0")
setFormula("") setFormula("")
} }
constructor(calculatorInterface: Calculator, value: String) {
mCallback = calculatorInterface
resetValues()
displayedNumber = value
setFormula("")
}
private fun resetValueIfNeeded() { private fun resetValueIfNeeded() {
if (mResetValue) if (mResetValue)
displayedNumber = "0" displayedNumber = "0"
@ -40,20 +33,20 @@ class CalculatorImpl {
mBaseValue = 0.0 mBaseValue = 0.0
mSecondValue = 0.0 mSecondValue = 0.0
mResetValue = false mResetValue = false
mLastKey = ""
mLastOperation = "" mLastOperation = ""
displayedNumber = "" displayedNumber = ""
displayedFormula = "" displayedFormula = ""
mIsFirstOperation = true mIsFirstOperation = true
lastKey = ""
} }
fun setValue(value: String) { fun setValue(value: String) {
mCallback!!.setValue(value) mCallback!!.setValue(value, context)
displayedNumber = value displayedNumber = value
} }
private fun setFormula(value: String) { private fun setFormula(value: String) {
mCallback!!.setFormula(value) mCallback!!.setFormula(value, context)
displayedFormula = value displayedFormula = value
} }
@ -69,10 +62,6 @@ class CalculatorImpl {
} }
} }
fun setLastKey(mLastKey: String) {
this.mLastKey = mLastKey
}
fun addDigit(number: Int) { fun addDigit(number: Int) {
val currentValue = displayedNumber val currentValue = displayedNumber
val newValue = formatString(currentValue!! + number) val newValue = formatString(currentValue!! + number)
@ -82,8 +71,9 @@ class CalculatorImpl {
private fun formatString(str: String): String { private fun formatString(str: String): String {
// if the number contains a decimal, do not try removing the leading zero anymore, nor add group separator // 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 // it would prevent writing values like 1.02
if (str.contains(".")) if (str.contains(".")) {
return str return str
}
val doubleValue = Formatter.stringToDouble(str) val doubleValue = Formatter.stringToDouble(str)
return Formatter.doubleToString(doubleValue) return Formatter.doubleToString(doubleValue)
@ -102,7 +92,7 @@ class CalculatorImpl {
mBaseValue = getDisplayedNumberAsDouble() mBaseValue = getDisplayedNumberAsDouble()
} }
fun calculateResult() { private fun calculateResult() {
if (!mIsFirstOperation) { if (!mIsFirstOperation) {
updateFormula() updateFormula()
} }
@ -117,11 +107,11 @@ class CalculatorImpl {
} }
fun handleOperation(operation: String) { fun handleOperation(operation: String) {
if (mLastKey == DIGIT) if (lastKey == DIGIT)
handleResult() handleResult()
mResetValue = true mResetValue = true
mLastKey = operation lastKey = operation
mLastOperation = operation mLastOperation = operation
if (operation == ROOT) if (operation == ROOT)
@ -136,8 +126,9 @@ class CalculatorImpl {
if (oldValue.contains("-")) if (oldValue.contains("-"))
minLen++ minLen++
if (len > minLen) if (len > minLen) {
newValue = oldValue.substring(0, len - 1) newValue = oldValue.substring(0, len - 1)
}
newValue = newValue.replace("\\.$".toRegex(), "") newValue = newValue.replace("\\.$".toRegex(), "")
newValue = formatString(newValue) newValue = formatString(newValue)
@ -152,21 +143,22 @@ class CalculatorImpl {
} }
fun handleEquals() { fun handleEquals() {
if (mLastKey == EQUALS) if (lastKey == EQUALS)
calculateResult() calculateResult()
if (mLastKey != DIGIT) if (lastKey != DIGIT)
return return
mSecondValue = getDisplayedNumberAsDouble() mSecondValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
mLastKey = EQUALS lastKey = EQUALS
} }
private fun decimalClicked() { private fun decimalClicked() {
var value = displayedNumber var value = displayedNumber
if (!value!!.contains(".")) if (!value!!.contains(".")) {
value += "." value += "."
}
setValue(value) setValue(value)
} }
@ -188,11 +180,11 @@ class CalculatorImpl {
} }
fun numpadClicked(id: Int) { fun numpadClicked(id: Int) {
if (mLastKey == EQUALS) { if (lastKey == EQUALS) {
mLastOperation = EQUALS mLastOperation = EQUALS
} }
mLastKey = DIGIT lastKey = DIGIT
resetValueIfNeeded() resetValueIfNeeded()
when (id) { when (id) {

View File

@ -6,114 +6,87 @@ import android.appwidget.AppWidgetProvider
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.util.Log
import android.graphics.Color
import android.view.View import android.view.View
import android.widget.RemoteViews import android.widget.RemoteViews
import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.activities.MainActivity import com.simplemobiletools.calculator.activities.MainActivity
import com.simplemobiletools.commons.helpers.PREFS_KEY import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.commons.helpers.WIDGET_BG_COLOR import com.simplemobiletools.commons.extensions.setBackgroundColor
import com.simplemobiletools.commons.helpers.WIDGET_TEXT_COLOR import com.simplemobiletools.commons.extensions.setText
class MyWidgetProvider : AppWidgetProvider(), Calculator { class MyWidgetProvider : AppWidgetProvider(), Calculator {
companion object { companion object {
private var mRemoteViews: RemoteViews? = null private var calc: CalculatorImpl? = 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
} }
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
initVariables(context) performUpdate(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)
} }
private fun setupIntent(action: String, id: Int) { private fun performUpdate(context: Context) {
mIntent!!.action = action Log.e("DEBUG", "perform update")
val pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0) val config = context.config
mRemoteViews!!.setOnClickPendingIntent(id, pendingIntent) 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) { setupIntent(context, views, EQUALS, R.id.btn_equals)
val intent = Intent(mContext, MainActivity::class.java) setupIntent(context, views, PLUS, R.id.btn_plus)
val pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0) setupIntent(context, views, MINUS, R.id.btn_minus)
mRemoteViews!!.setOnClickPendingIntent(id, pendingIntent) 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) { setupAppOpenIntent(context, views, R.id.formula)
mContext = context setupAppOpenIntent(context, views, R.id.result)
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)
mRemoteViews = RemoteViews(mContext!!.packageName, R.layout.activity_main) views.setViewVisibility(R.id.btn_reset, View.VISIBLE)
mRemoteViews!!.setViewVisibility(R.id.btn_reset, View.VISIBLE) views.setBackgroundColor(R.id.calculator_holder, config.widgetBgColor)
mRemoteViews!!.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor)
updateTextColors(newTextColor) updateTextColors(views, config.widgetTextColor)
mWidgetManager = AppWidgetManager.getInstance(mContext) appWidgetManager.updateAppWidget(it, views)
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)
} }
} }
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) { private fun setupIntent(context: Context, views: RemoteViews, action: String, id: 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) 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) { 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) { private fun myAction(action: String, context: Context) {
if (mCalc == null || mRemoteViews == null || mWidgetManager == null || mPrefs == null || mContext == null) { Log.e("DEBUG", "myaction $calc")
initVariables(context) if (calc == null) {
calc = CalculatorImpl(this, context)
} }
when (action) { when (action) {
DECIMAL -> mCalc!!.numpadClicked(R.id.btn_decimal) DECIMAL -> calc!!.numpadClicked(R.id.btn_decimal)
ZERO -> mCalc!!.numpadClicked(R.id.btn_0) ZERO -> calc!!.numpadClicked(R.id.btn_0)
ONE -> mCalc!!.numpadClicked(R.id.btn_1) ONE -> calc!!.numpadClicked(R.id.btn_1)
TWO -> mCalc!!.numpadClicked(R.id.btn_2) TWO -> calc!!.numpadClicked(R.id.btn_2)
THREE -> mCalc!!.numpadClicked(R.id.btn_3) THREE -> calc!!.numpadClicked(R.id.btn_3)
FOUR -> mCalc!!.numpadClicked(R.id.btn_4) FOUR -> calc!!.numpadClicked(R.id.btn_4)
FIVE -> mCalc!!.numpadClicked(R.id.btn_5) FIVE -> calc!!.numpadClicked(R.id.btn_5)
SIX -> mCalc!!.numpadClicked(R.id.btn_6) SIX -> calc!!.numpadClicked(R.id.btn_6)
SEVEN -> mCalc!!.numpadClicked(R.id.btn_7) SEVEN -> calc!!.numpadClicked(R.id.btn_7)
EIGHT -> mCalc!!.numpadClicked(R.id.btn_8) EIGHT -> calc!!.numpadClicked(R.id.btn_8)
NINE -> mCalc!!.numpadClicked(R.id.btn_9) NINE -> calc!!.numpadClicked(R.id.btn_9)
EQUALS -> mCalc!!.handleEquals() EQUALS -> calc!!.handleEquals()
CLEAR -> mCalc!!.handleClear() CLEAR -> calc!!.handleClear()
RESET -> mCalc!!.handleReset() RESET -> calc!!.handleReset()
PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> mCalc!!.handleOperation(action) PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> calc!!.handleOperation(action)
else -> {
}
} }
} }
override fun setValue(value: String) { override fun setValue(value: String, context: Context) {
mRemoteViews!!.setTextViewText(R.id.result, value) val appWidgetManager = AppWidgetManager.getInstance(context)
updateWidget() 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 setValueDouble(d: Double) {
} }
override fun setFormula(value: String) { override fun setFormula(value: String, context: Context) {
mRemoteViews!!.setTextViewText(R.id.formula, value) val appWidgetManager = AppWidgetManager.getInstance(context)
updateWidget() 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) super.onDeleted(context, appWidgetIds)
if (mContext != null) calc = null
updateWidgetIds()
} }
} }

View File

@ -9,7 +9,7 @@
tools:context=".activities.MainActivity" tools:context=".activities.MainActivity"
tools:ignore="HardcodedText"> tools:ignore="HardcodedText">
<com.simplemobiletools.commons.views.MyTextView <TextView
android:id="@+id/formula" android:id="@+id/formula"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -21,7 +21,7 @@
android:paddingRight="@dimen/activity_margin" android:paddingRight="@dimen/activity_margin"
android:textSize="@dimen/actionbar_text_size"/> android:textSize="@dimen/actionbar_text_size"/>
<com.simplemobiletools.commons.views.MyTextView <TextView
android:id="@+id/result" android:id="@+id/result"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -40,7 +40,7 @@
android:layout_weight="2" android:layout_weight="2"
android:orientation="horizontal"> android:orientation="horizontal">
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_modulo" android:id="@+id/btn_modulo"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -50,7 +50,7 @@
android:textAllCaps="false" android:textAllCaps="false"
android:textSize="@dimen/extra_big_text_size"/> android:textSize="@dimen/extra_big_text_size"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_power" android:id="@+id/btn_power"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -58,7 +58,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="^"/> android:text="^"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_root" android:id="@+id/btn_root"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -66,7 +66,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="√"/> android:text="√"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_clear" android:id="@+id/btn_clear"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -74,7 +74,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="C"/> android:text="C"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_reset" android:id="@+id/btn_reset"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -90,7 +90,7 @@
android:layout_weight="2" android:layout_weight="2"
android:orientation="horizontal"> android:orientation="horizontal">
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_7" android:id="@+id/btn_7"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -98,7 +98,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="7"/> android:text="7"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_8" android:id="@+id/btn_8"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -106,7 +106,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="8"/> android:text="8"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_9" android:id="@+id/btn_9"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -114,7 +114,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="9"/> android:text="9"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_divide" android:id="@+id/btn_divide"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -129,7 +129,7 @@
android:layout_weight="2" android:layout_weight="2"
android:orientation="horizontal"> android:orientation="horizontal">
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_4" android:id="@+id/btn_4"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -137,7 +137,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="4"/> android:text="4"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_5" android:id="@+id/btn_5"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -145,7 +145,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="5"/> android:text="5"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_6" android:id="@+id/btn_6"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -153,7 +153,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="6"/> android:text="6"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_multiply" android:id="@+id/btn_multiply"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -168,7 +168,7 @@
android:layout_weight="2" android:layout_weight="2"
android:orientation="horizontal"> android:orientation="horizontal">
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_1" android:id="@+id/btn_1"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -176,7 +176,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="1"/> android:text="1"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_2" android:id="@+id/btn_2"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -184,7 +184,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="2"/> android:text="2"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_3" android:id="@+id/btn_3"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -192,7 +192,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="3"/> android:text="3"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_minus" android:id="@+id/btn_minus"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -207,7 +207,7 @@
android:layout_weight="2" android:layout_weight="2"
android:orientation="horizontal"> android:orientation="horizontal">
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_0" android:id="@+id/btn_0"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -215,7 +215,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="0"/> android:text="0"/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_decimal" android:id="@+id/btn_decimal"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -223,7 +223,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="."/> android:text="."/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_equals" android:id="@+id/btn_equals"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -231,7 +231,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="="/> android:text="="/>
<com.simplemobiletools.commons.views.MyButton <Button
android:id="@+id/btn_plus" android:id="@+id/btn_plus"
style="@style/MyButton" style="@style/MyButton"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -13,13 +13,13 @@ import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class) @RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(21)) @Config(constants = BuildConfig::class, sdk = intArrayOf(21))
class MainActivityTest { class MainActivityTest {
internal lateinit var activity: MainActivity private lateinit var activity: MainActivity
private val displayedNumber: String? private val displayedNumber: String?
get() = activity.calc!!.displayedNumber get() = activity.calc.displayedNumber
private val displayedFormula: String? private val displayedFormula: String?
get() = activity.calc!!.displayedFormula get() = activity.calc.displayedFormula
@Before @Before
fun setUp() { fun setUp() {
@ -28,14 +28,14 @@ class MainActivityTest {
@Test @Test
fun addSimpleDigit() { fun addSimpleDigit() {
activity.calc!!.addDigit(2) activity.calc.addDigit(2)
assertEquals("2", displayedNumber) assertEquals("2", displayedNumber)
} }
@Test @Test
fun removeLeadingZero() { fun removeLeadingZero() {
activity.calc!!.addDigit(0) activity.calc.addDigit(0)
activity.calc!!.addDigit(5) activity.calc.addDigit(5)
assertEquals("5", displayedNumber) assertEquals("5", displayedNumber)
} }
@ -99,26 +99,26 @@ class MainActivityTest {
@Test @Test
fun clearBtnSimpleTest() { fun clearBtnSimpleTest() {
setDouble(156.0) setDouble(156.0)
activity.calc!!.handleClear() activity.calc.handleClear()
assertEquals("15", displayedNumber) assertEquals("15", displayedNumber)
} }
@Test @Test
fun clearBtnComplexTest() { fun clearBtnComplexTest() {
setDouble(-26.0) setDouble(-26.0)
activity.calc!!.handleClear() activity.calc.handleClear()
assertEquals("-2", displayedNumber) assertEquals("-2", displayedNumber)
activity.calc!!.handleClear() activity.calc.handleClear()
assertEquals("0", displayedNumber) assertEquals("0", displayedNumber)
} }
@Test @Test
fun clearBtnLongClick_resetsEverything() { fun clearBtnLongClick_resetsEverything() {
calcResult(-1.2, PLUS, 3.4) calcResult(-1.2, PLUS, 3.4)
activity.calc!!.handleReset() activity.calc.handleReset()
handleOperation(PLUS) handleOperation(PLUS)
setDouble(3.0) setDouble(3.0)
activity.calc!!.handleResult() activity.calc.handleResult()
assertEquals("3", displayedNumber) assertEquals("3", displayedNumber)
checkFormula("") checkFormula("")
} }
@ -133,10 +133,10 @@ class MainActivityTest {
checkFormula("-12.2+21") checkFormula("-12.2+21")
setDouble(1.6) setDouble(1.6)
activity.calc!!.handleEquals() activity.calc.handleEquals()
assertEquals("7.2", displayedNumber) assertEquals("7.2", displayedNumber)
checkFormula("8.8-1.6") checkFormula("8.8-1.6")
activity.calc!!.handleEquals() activity.calc.handleEquals()
assertEquals("5.6", displayedNumber) assertEquals("5.6", displayedNumber)
checkFormula("7.2-1.6") checkFormula("7.2-1.6")
@ -161,7 +161,7 @@ class MainActivityTest {
assertEquals("16", displayedNumber) assertEquals("16", displayedNumber)
checkFormula("√256") checkFormula("√256")
activity.calc!!.handleClear() activity.calc.handleClear()
assertEquals("1", displayedNumber) assertEquals("1", displayedNumber)
} }
@ -170,7 +170,7 @@ class MainActivityTest {
} }
private fun handleOperation(operation: String) { private fun handleOperation(operation: String) {
activity.calc!!.handleOperation(operation) activity.calc.handleOperation(operation)
} }
private fun checkFormula(desired: String) { private fun checkFormula(desired: String) {
@ -181,7 +181,7 @@ class MainActivityTest {
setDouble(baseValue) setDouble(baseValue)
handleOperation(operation) handleOperation(operation)
setDouble(secondValue) setDouble(secondValue)
activity.calc!!.handleResult() activity.calc.handleResult()
return displayedNumber return displayedNumber
} }
} }