Merge pull request #257 from Naveen3Singh/comma_as_decimal

Allow toggling comma/dot as decimal/thousands
This commit is contained in:
Tibor Kaputa 2022-06-05 09:15:31 +02:00 committed by GitHub
commit 47d6110a0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 235 additions and 90 deletions

View file

@ -13,6 +13,7 @@ import com.simplemobiletools.calculator.databases.CalculatorDatabase
import com.simplemobiletools.calculator.dialogs.HistoryDialog
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.updateViewColors
import com.simplemobiletools.calculator.extensions.updateWidgets
import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW
@ -24,8 +25,11 @@ import me.grantland.widget.AutofitHelper
class MainActivity : SimpleActivity(), Calculator {
private var storedTextColor = 0
private var vibrateOnButtonPress = true
private var storedUseCommaAsDecimalMark = false
private var decimalSeparator = DOT
private var groupingSeparator = COMMA
lateinit var calc: CalculatorImpl
private lateinit var calc: CalculatorImpl
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -34,26 +38,28 @@ class MainActivity : SimpleActivity(), Calculator {
calc = CalculatorImpl(this, applicationContext)
btn_plus.setOnClickListener { calc.handleOperation(PLUS); checkHaptic(it) }
btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) }
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) }
btn_divide.setOnClickListener { calc.handleOperation(DIVIDE); checkHaptic(it) }
btn_percent.setOnClickListener { calc.handleOperation(PERCENT); checkHaptic(it) }
btn_power.setOnClickListener { calc.handleOperation(POWER); checkHaptic(it) }
btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) }
btn_plus.setOnClickOperation(PLUS)
btn_minus.setOnClickOperation(MINUS)
btn_multiply.setOnClickOperation(MULTIPLY)
btn_divide.setOnClickOperation(DIVIDE)
btn_percent.setOnClickOperation(PERCENT)
btn_power.setOnClickOperation(POWER)
btn_root.setOnClickOperation(ROOT)
btn_minus.setOnLongClickListener { calc.turnToNegative() }
btn_minus.setOnLongClickListener {
calc.turnToNegative()
btn_clear.setVibratingOnClickListener { calc.handleClear() }
btn_clear.setOnLongClickListener {
calc.handleReset()
true
}
btn_clear.setOnClickListener { calc.handleClear(); checkHaptic(it) }
btn_clear.setOnLongClickListener { calc.handleReset(); true }
getButtonIds().forEach {
it.setOnClickListener { calc.numpadClicked(it.id); checkHaptic(it) }
it.setVibratingOnClickListener { view ->
calc.numpadClicked(view.id)
}
}
btn_equals.setOnClickListener { calc.handleEquals(); checkHaptic(it) }
btn_equals.setVibratingOnClickListener { calc.handleEquals() }
formula.setOnLongClickListener { copyToClipboard(false) }
result.setOnLongClickListener { copyToClipboard(true) }
@ -61,6 +67,7 @@ class MainActivity : SimpleActivity(), Calculator {
AutofitHelper.create(formula)
storeStateVariables()
updateViewColors(calculator_holder, getProperTextColor())
setupDecimalSeparator()
checkWhatsNewDialog()
checkAppOnSDCard()
}
@ -75,6 +82,11 @@ class MainActivity : SimpleActivity(), Calculator {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
if (storedUseCommaAsDecimalMark != config.useCommaAsDecimalMark) {
setupDecimalSeparator()
updateWidgets()
}
vibrateOnButtonPress = config.vibrateOnButtonPress
val properPrimaryColor = getProperPrimaryColor()
@ -117,6 +129,7 @@ class MainActivity : SimpleActivity(), Calculator {
private fun storeStateVariables() {
config.apply {
storedTextColor = textColor
storedUseCommaAsDecimalMark = useCommaAsDecimalMark
}
}
@ -186,4 +199,30 @@ class MainActivity : SimpleActivity(), Calculator {
override fun showNewFormula(value: String, context: Context) {
formula.text = value
}
private fun setupDecimalSeparator() {
storedUseCommaAsDecimalMark = config.useCommaAsDecimalMark
if (storedUseCommaAsDecimalMark) {
decimalSeparator = COMMA
groupingSeparator = DOT
} else {
decimalSeparator = DOT
groupingSeparator = COMMA
}
calc.updateSeparators(decimalSeparator, groupingSeparator)
btn_decimal.text = decimalSeparator
}
private fun View.setVibratingOnClickListener(callback: (view: View) -> Unit) {
setOnClickListener {
callback(it)
checkHaptic(it)
}
}
private fun View.setOnClickOperation(operation: String) {
setVibratingOnClickListener {
calc.handleOperation(operation)
}
}
}

View file

@ -4,13 +4,17 @@ import android.content.Intent
import android.os.Bundle
import android.view.Menu
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.extensions.calculatorDB
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.updateWidgets
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import kotlinx.android.synthetic.main.activity_settings.*
import java.util.*
import kotlin.system.exitProcess
class SettingsActivity : SimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -26,6 +30,7 @@ class SettingsActivity : SimpleActivity() {
setupUseEnglish()
setupVibrate()
setupPreventPhoneFromSleeping()
setupUseCommaAsDecimalMark()
setupCustomizeWidgetColors()
updateTextColors(settings_scrollview)
@ -94,6 +99,18 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupUseCommaAsDecimalMark() {
settings_use_comma_as_decimal_mark.isChecked = config.useCommaAsDecimalMark
settings_use_comma_as_decimal_mark_holder.setOnClickListener {
settings_use_comma_as_decimal_mark.toggle()
config.useCommaAsDecimalMark = settings_use_comma_as_decimal_mark.isChecked
updateWidgets()
ensureBackgroundThread {
applicationContext.calculatorDB.deleteHistory()
}
}
}
private fun setupCustomizeWidgetColors() {
settings_customize_widget_colors_holder.setOnClickListener {
Intent(this, WidgetConfigureActivity::class.java).apply {

View file

@ -1,12 +1,16 @@
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.helpers.Config
import com.simplemobiletools.calculator.interfaces.CalculatorDao
import com.simplemobiletools.calculator.databases.CalculatorDatabase
import com.simplemobiletools.calculator.helpers.Config
import com.simplemobiletools.calculator.helpers.MyWidgetProvider
import com.simplemobiletools.calculator.interfaces.CalculatorDao
val Context.config: Config get() = Config.newInstance(applicationContext)
@ -16,11 +20,23 @@ 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)
}
}
}
fun Context.updateWidgets() {
val widgetIDs = AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetProvider::class.java))
?: return
if (widgetIDs.isNotEmpty()) {
Intent(applicationContext, MyWidgetProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs)
sendBroadcast(this)
}
}
}

View file

@ -8,7 +8,12 @@ import com.simplemobiletools.commons.extensions.toast
import net.objecthunter.exp4j.ExpressionBuilder
import java.math.BigDecimal
class CalculatorImpl(calculator: Calculator, private val context: Context) {
class CalculatorImpl(
calculator: Calculator,
private val context: Context,
private var decimalSeparator: String = DOT,
private var groupingSeparator: String = COMMA
) {
private var callback: Calculator? = calculator
private var baseValue = 0.0
@ -20,6 +25,10 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
private val operationsRegex = "[-+×÷^%√]".toPattern()
private val numbersRegex = "[^0-9,.]".toRegex()
private val formatter = NumberFormatHelper(
decimalSeparator = decimalSeparator, groupingSeparator = groupingSeparator
)
init {
showNewResult("0")
}
@ -35,21 +44,21 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
private fun zeroClicked() {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value != "0" || value.contains(".")) {
if (value != "0" || value.contains(decimalSeparator)) {
addDigit(0)
}
}
private fun decimalClicked() {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(groupingSeparator, "")
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (!value.contains(".")) {
if (!value.contains(decimalSeparator)) {
when {
value == "0" && !valueToCheck.contains(operationsRegex.toRegex()) -> inputDisplayedFormula = "0."
value == "" -> inputDisplayedFormula += "0."
else -> inputDisplayedFormula += "."
value == "0" && !valueToCheck.contains(operationsRegex.toRegex()) -> inputDisplayedFormula = "0$decimalSeparator"
value == "" -> inputDisplayedFormula += "0$decimalSeparator"
else -> inputDisplayedFormula += decimalSeparator
}
}
@ -60,10 +69,13 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
private fun addThousandsDelimiter() {
val valuesToCheck = numbersRegex.split(inputDisplayedFormula).filter { it.trim().isNotEmpty() }
valuesToCheck.forEach {
var newString = Formatter.addGroupingSeparators(it)
var newString = formatter.addGroupingSeparators(it)
// allow writing numbers like 0.003
if (it.contains(".")) {
newString = newString.substringBefore(".") + ".${it.substringAfter(".")}"
if (it.contains(decimalSeparator)) {
val firstPart = newString.substringBefore(decimalSeparator)
val lastPart = it.substringAfter(decimalSeparator)
newString = "$firstPart$decimalSeparator$lastPart"
}
inputDisplayedFormula = inputDisplayedFormula.replace(it, newString)
@ -86,7 +98,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
val lastChar = inputDisplayedFormula.last().toString()
if (lastChar == ".") {
if (lastChar == decimalSeparator) {
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
} else if (operations.contains(lastChar)) {
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
@ -137,7 +149,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
return false
}
if (!inputDisplayedFormula.trimStart('-').any { it.toString() in operations } && inputDisplayedFormula.replace(",", "").toDouble() != 0.0) {
if (!inputDisplayedFormula.trimStart('-').any { it.toString() in operations } && inputDisplayedFormula.removeGroupSeparator().toDouble() != 0.0) {
inputDisplayedFormula = if (inputDisplayedFormula.first() == '-') {
inputDisplayedFormula.substring(1)
} else {
@ -185,7 +197,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
private fun getSecondValue(): Double {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value == "") {
value = "0"
@ -205,23 +217,28 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
if (lastKey != EQUALS) {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
val parts = valueToCheck.split(operationsRegex).filter { it != "" }
if (parts.isEmpty()) {
return
}
baseValue = Formatter.stringToDouble(parts.first())
baseValue = parts.first().toDouble()
if (inputDisplayedFormula.startsWith("-")) {
baseValue *= -1
}
secondValue = parts.getOrNull(1)?.replace(",", "")?.toDouble() ?: secondValue
secondValue = parts.getOrNull(1)?.toDouble() ?: secondValue
}
if (lastOperation != "") {
val sign = getSign(lastOperation)
val expression = "${baseValue.format()}$sign${secondValue.format()}".replace("", "sqrt").replace("×", "*").replace("÷", "/")
val formattedBaseValue = baseValue.format().removeGroupSeparator()
val formatterSecondValue = secondValue.format().removeGroupSeparator()
val expression = "$formattedBaseValue$sign$formatterSecondValue"
.replace("", "sqrt")
.replace("×", "*")
.replace("÷", "/")
try {
if (sign == "÷" && secondValue == 0.0) {
@ -229,16 +246,11 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
return
}
if (sign == "%") {
val second = secondValue / 100f
"${baseValue.format()}*${second.format()}".replace("", "sqrt").replace("×", "*").replace("÷", "/")
}
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
// handle cases like 10%200 here
val result = if (sign == "%") {
val second = secondValue / 100f
ExpressionBuilder("${baseValue.format().replace(",", "")}*${second.format()}").build().evaluate()
val second = (secondValue / 100f).format().removeGroupSeparator()
ExpressionBuilder("$formattedBaseValue*$second").build().evaluate()
} else {
// avoid Double rounding errors at expressions like 5250,74 + 14,98
if (sign == "+" || sign == "-") {
@ -250,7 +262,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
bigDecimalResult.toDouble()
} else {
ExpressionBuilder(expression.replace(",", "")).build().evaluate()
ExpressionBuilder(expression).build().evaluate()
}
}
@ -260,11 +272,14 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
showNewResult(result.format())
baseValue = result
val newFormula = expression.replace("sqrt", "").replace("*", "×").replace("/", "÷")
HistoryHelper(context).insertOrUpdateHistoryEntry(History(null, newFormula, result.format(), System.currentTimeMillis()))
inputDisplayedFormula = result.format()
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
HistoryHelper(context).insertOrUpdateHistoryEntry(
History(id = null, formula = newFormula, result = result.format(), timestamp = System.currentTimeMillis())
)
showNewFormula(newFormula)
inputDisplayedFormula = result.format()
baseValue = result
} catch (e: Exception) {
context.toast(R.string.unknown_error_occurred)
}
@ -317,19 +332,13 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
}
val lastValue = newValue.last().toString()
lastKey = when {
operations.contains(lastValue) -> {
CLEAR
}
lastValue == "." -> {
DECIMAL
}
else -> {
DIGIT
}
operations.contains(lastValue) -> CLEAR
lastValue == decimalSeparator -> DECIMAL
else -> DIGIT
}
}
newValue = newValue.trimEnd(',')
newValue = newValue.trimEnd(groupingSeparator.single())
inputDisplayedFormula = newValue
addThousandsDelimiter()
showNewResult(inputDisplayedFormula)
@ -391,4 +400,19 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
addThousandsDelimiter()
showNewResult(inputDisplayedFormula)
}
fun updateSeparators(decimalSeparator: String, groupingSeparator: String) {
if (this.decimalSeparator != decimalSeparator || this.groupingSeparator != groupingSeparator) {
this.decimalSeparator = decimalSeparator
this.groupingSeparator = groupingSeparator
formatter.decimalSeparator = decimalSeparator
formatter.groupingSeparator = groupingSeparator
// future: maybe update the formulas with new separators instead of resetting the whole thing
handleReset()
}
}
private fun Double.format() = formatter.doubleToString(this)
private fun String.removeGroupSeparator() = formatter.removeGroupingSeparator(this)
}

View file

@ -7,4 +7,8 @@ class Config(context: Context) : BaseConfig(context) {
companion object {
fun newInstance(context: Context) = Config(context)
}
var useCommaAsDecimalMark: Boolean
get() = prefs.getBoolean(USE_COMMA_AS_DECIMAL_MARK, false)
set(useCommaAsDecimalMark) = prefs.edit().putBoolean(USE_COMMA_AS_DECIMAL_MARK, useCommaAsDecimalMark).apply()
}

View file

@ -24,3 +24,9 @@ const val SIX = "six"
const val SEVEN = "seven"
const val EIGHT = "eight"
const val NINE = "nine"
const val DOT = "."
const val COMMA = ","
// shared prefs
const val USE_COMMA_AS_DECIMAL_MARK = "use_comma_as_decimal_mark"

View file

@ -1,25 +0,0 @@
package com.simplemobiletools.calculator.helpers
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*
object Formatter {
fun doubleToString(d: Double): String {
val symbols = DecimalFormatSymbols(Locale.US)
symbols.decimalSeparator = '.'
symbols.groupingSeparator = ','
val formatter = DecimalFormat()
formatter.maximumFractionDigits = 12
formatter.decimalFormatSymbols = symbols
formatter.isGroupingUsed = true
return formatter.format(d)
}
fun stringToDouble(str: String) = str.replace(",", "").toDouble()
fun addGroupingSeparators(str: String) = doubleToString(stringToDouble(str))
}
fun Double.format(): String = Formatter.doubleToString(this)

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)
}
}

View file

@ -0,0 +1,31 @@
package com.simplemobiletools.calculator.helpers
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*
class NumberFormatHelper(
var decimalSeparator: String = DOT,
var groupingSeparator: String = COMMA
) {
fun doubleToString(d: Double): String {
val symbols = DecimalFormatSymbols(Locale.US)
symbols.decimalSeparator = decimalSeparator.single()
symbols.groupingSeparator = groupingSeparator.single()
val formatter = DecimalFormat()
formatter.maximumFractionDigits = 12
formatter.decimalFormatSymbols = symbols
formatter.isGroupingUsed = true
return formatter.format(d)
}
fun addGroupingSeparators(str: String): String {
return doubleToString(removeGroupingSeparator(str).toDouble())
}
fun removeGroupingSeparator(str: String): String {
return str.replace(groupingSeparator, "").replace(decimalSeparator, DOT)
}
}

View file

@ -126,7 +126,7 @@
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_bottom_corners">
android:background="@drawable/ripple_background">
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_prevent_phone_from_sleeping"
@ -136,6 +136,22 @@
android:text="@string/prevent_phone_from_sleeping" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_use_comma_as_decimal_mark_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_bottom_corners">
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_use_comma_as_decimal_mark"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/use_comma_as_decimal_mark" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>