Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with putting decimal point at the middle value #478

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -406,29 +406,25 @@ class MainActivity : AppCompatActivity() {
withContext(Dispatchers.Main) {
// Avoid two decimalSeparator in the same number
// when you click on the decimalSeparator button
if (value == decimalSeparatorSymbol && decimalSeparatorSymbol in binding.input.text.toString()) {
if (binding.input.text.toString().isNotEmpty()) {
var lastNumberBefore = ""
if (cursorPosition > 0 && binding.input.text.toString()
.substring(0, cursorPosition)
.last() in "0123456789\\$decimalSeparatorSymbol"
) {
lastNumberBefore = NumberFormatter.extractNumbers(
binding.input.text.toString().substring(0, cursorPosition),
decimalSeparatorSymbol
).last()
}
var firstNumberAfter = ""
if (cursorPosition < binding.input.text.length - 1) {
firstNumberAfter = NumberFormatter.extractNumbers(
binding.input.text.toString()
.substring(cursorPosition, binding.input.text.length),
decimalSeparatorSymbol
).first()
}
if (decimalSeparatorSymbol in lastNumberBefore || decimalSeparatorSymbol in firstNumberAfter) {
return@withContext
}
val textLength = binding.input.text.length

if (textLength > 0) {

// Get next / previous characters relative to the cursor
val nextChar =
if (textLength - cursorPosition > 0) binding.input.text[cursorPosition].toString() else "0" // use "0" as default like it's not a symbol

val previousChar =
if (cursorPosition > 0) binding.input.text[cursorPosition - 1].toString() else "0"

val currentValue = getValueAroundCursor(cursorPosition)

if(value == decimalSeparatorSymbol && currentValue.contains(decimalSeparatorSymbol) ){
return@withContext
}

if(value == decimalSeparatorSymbol && previousChar == decimalSeparatorSymbol || nextChar == decimalSeparatorSymbol){
return@withContext
}
}

Expand All @@ -446,6 +442,30 @@ class MainActivity : AppCompatActivity() {
}
}

private fun getValueAroundCursor(cursorPosition: Int): String {

val text = binding.input.text.toString()

if (cursorPosition < 0 || cursorPosition > text.length) {
return ""
}

val delimiters = charArrayOf('+', '-', '×', '÷', '(', ')')

// Find the start of the text around the cursor by looking for the previous delimiter
val start = text.substring(0, cursorPosition).lastIndexOfAny(delimiters).let {
if (it == -1) 0 else it + 1
}

// Find the end of the text around the cursor by looking for the next delimiter
val end = text.indexOfAny(delimiters, cursorPosition).let {
if (it == -1) text.length else it
}

// Return the text around the cursor position
return text.substring(start, end)
}

private fun roundResult(result: BigDecimal): BigDecimal {
val numberPrecision = MyPreferences(this).numberPrecision!!.toInt()
var newResult = result.setScale(numberPrecision, RoundingMode.HALF_EVEN)
Expand Down