Update calculator widget on comma/dot toggle

This commit is contained in:
Naveen 2022-05-31 10:57:18 +05:30
parent b9b5cfa249
commit 8668a5b680
4 changed files with 42 additions and 8 deletions

View file

@ -12,6 +12,7 @@ import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.databases.CalculatorDatabase
import com.simplemobiletools.calculator.dialogs.HistoryDialog
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.refreshAppWidget
import com.simplemobiletools.calculator.extensions.updateViewColors
import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.commons.extensions.*
@ -83,6 +84,7 @@ class MainActivity : SimpleActivity(), Calculator {
if (storedUseCommaAsDecimalMark != config.useCommaAsDecimalMark) {
setupDecimalSeparator()
refreshAppWidget<MyWidgetProvider>()
}
vibrateOnButtonPress = config.vibrateOnButtonPress

View file

@ -5,12 +5,15 @@ import android.os.Bundle
import android.view.Menu
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.refreshAppWidget
import com.simplemobiletools.calculator.helpers.MyWidgetProvider
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
import kotlinx.android.synthetic.main.activity_settings.*
import java.util.*
import kotlin.system.exitProcess
class SettingsActivity : SimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -100,6 +103,7 @@ class SettingsActivity : SimpleActivity() {
settings_use_comma_as_decimal_mark_holder.setOnClickListener {
settings_use_comma_as_decimal_mark.toggle()
config.useCommaAsDecimalMark = settings_use_comma_as_decimal_mark.isChecked
refreshAppWidget<MyWidgetProvider>()
}
}

View file

@ -1,12 +1,15 @@
package com.simplemobiletools.calculator.extensions
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.simplemobiletools.calculator.databases.CalculatorDatabase
import com.simplemobiletools.calculator.helpers.Config
import com.simplemobiletools.calculator.interfaces.CalculatorDao
import com.simplemobiletools.calculator.databases.CalculatorDatabase
val Context.config: Config get() = Config.newInstance(applicationContext)
@ -16,11 +19,19 @@ val Context.calculatorDB: CalculatorDao get() = CalculatorDatabase.getInstance(a
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)
}
.forEach {
when (it) {
is TextView -> it.setTextColor(textColor)
is Button -> it.setTextColor(textColor)
is ViewGroup -> updateViewColors(it, textColor)
}
}
}
inline fun <reified T> Context.refreshAppWidget() {
val intent = Intent(this, T::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, T::class.java))
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(intent)
}

View file

@ -17,6 +17,9 @@ import com.simplemobiletools.commons.extensions.setText
class MyWidgetProvider : AppWidgetProvider(), Calculator {
companion object {
private var calc: CalculatorImpl? = null
private var storedUseCommaAsDecimalMark = false
private var decimalSeparator = DOT
private var groupingSeparator = COMMA
}
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
@ -53,6 +56,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
views.applyColorFilter(R.id.widget_background, config.widgetBgColor)
updateTextColors(views, config.widgetTextColor)
setupDecimalSeparator(views, config.useCommaAsDecimalMark)
appWidgetManager.updateAppWidget(it, views)
}
}
@ -97,7 +101,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
private fun myAction(action: String, context: Context) {
if (calc == null) {
calc = CalculatorImpl(this, context)
calc = CalculatorImpl(this, context, decimalSeparator, groupingSeparator)
}
when (action) {
@ -141,4 +145,17 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
super.onDeleted(context, appWidgetIds)
calc = null
}
private fun setupDecimalSeparator(views: RemoteViews, useCommaAsDecimalMark: Boolean) {
storedUseCommaAsDecimalMark = useCommaAsDecimalMark
if (storedUseCommaAsDecimalMark) {
decimalSeparator = COMMA
groupingSeparator = DOT
} else {
decimalSeparator = DOT
groupingSeparator = COMMA
}
calc?.updateSeparators(decimalSeparator, groupingSeparator)
views.setTextViewText(R.id.btn_decimal, decimalSeparator)
}
}