From b75d44b1473ea9854bd7ba4cf49c35522f765743 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 5 Nov 2017 20:25:04 +0100 Subject: [PATCH] updating a couple more widget related things --- .../calculator/activities/MainActivity.kt | 51 ++--- .../activities/WidgetConfigureActivity.kt | 4 +- .../calculator/extensions/Context.kt | 16 ++ .../calculator/helpers/Calculator.kt | 6 +- .../calculator/helpers/CalculatorImpl.kt | 52 ++--- .../calculator/helpers/MyWidgetProvider.kt | 208 ++++++++---------- app/src/main/res/layout/activity_main.xml | 46 ++-- .../calculator/MainActivityTest.kt | 32 +-- 8 files changed, 199 insertions(+), 216 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 2d3b260c..e132dee9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/MainActivity.kt @@ -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 } } 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 e81b3b05..a5f67585 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/WidgetConfigureActivity.kt @@ -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() diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/extensions/Context.kt index 4ef56d53..e07d6bb7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/extensions/Context.kt @@ -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) + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Calculator.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Calculator.kt index 74284819..1ad20b9f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Calculator.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Calculator.kt @@ -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) } 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 1cf2715b..7123e1d8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -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) { 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 7ab4a3b3..97502818 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/MyWidgetProvider.kt @@ -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 } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5638f0f0..d11de69b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -9,7 +9,7 @@ tools:context=".activities.MainActivity" tools:ignore="HardcodedText"> - - - - - - - - - - - - - - - - - - - - - - -