@@ -583,62 +583,66 @@ class MainActivity : AppCompatActivity() {
583
583
val result = roundResult((Calculator ().evaluate(calculationTmp, isDegreeModeActivated)))
584
584
var resultString = result.toString()
585
585
var formattedResult = NumberFormatter .format(resultString.replace(" ." , NumberFormatter .decimalSeparatorSymbol))
586
- val currentTime = System .currentTimeMillis().toString()
587
586
588
- // If there is an unused 0 at the end, remove it : 2.0 -> 2
587
+ // If result is a number and it is finite
589
588
if (! result.isNaN() && result.isFinite()) {
589
+ // If there is an unused 0 at the end, remove it : 2.0 -> 2
590
590
if ((result * 10 ) % 10 == 0.0 ) {
591
591
resultString = String .format(" %.0f" , result)
592
592
formattedResult = NumberFormatter .format(resultString)
593
593
}
594
- }
595
594
596
- // If result is a number && it is finite && calculation != result
597
- if (! result.isNaN() && result.isFinite() && calculation != formattedResult) {
598
- val history = MyPreferences (this @MainActivity).getHistory()
599
-
600
- // If the current calculation is different from the former calculation in the history
601
- if (history[history.size - 1 ].calculation != calculation) {
602
- // Save to history
603
- history.add(
604
- History (
605
- calculation = calculation,
606
- result = formattedResult,
607
- time = currentTime,
608
- )
609
- )
610
- MyPreferences (this @MainActivity).saveHistory(this @MainActivity, history)
611
- // Update history variables
612
- withContext(Dispatchers .Main ) {
613
- historyAdapter.appendOneHistoryElement(History (
614
- calculation = calculation,
615
- result = formattedResult,
616
- time = currentTime,
617
- ))
618
- // Scroll to the bottom of the recycle view
619
- binding.historyRecylcleView.scrollToPosition(historyAdapter.itemCount - 1 )
620
- }
595
+ // Hide the cursor before updating binding.input to avoid weird cursor movement
596
+ withContext(Dispatchers .Main ) {
597
+ binding.input.isCursorVisible = false
598
+ }
621
599
622
- // Hide the cursor before updating binding.input to avoid weird cursor movement
623
- withContext(Dispatchers .Main ) {
624
- binding.input.isCursorVisible = false
625
- }
600
+ // Display result
601
+ withContext(Dispatchers .Main ) { binding.input.setText(formattedResult) }
626
602
627
- if ((result * 10 ) % 10 == 0.0 ) {
628
- resultString = String .format(" %.0f" , result)
629
- formattedResult = NumberFormatter .format(resultString)
630
- withContext(Dispatchers .Main ) { binding.input.setText(formattedResult) }
631
- } else {
632
- withContext(Dispatchers .Main ) { binding.input.setText(formattedResult) }
633
- }
634
- // Set cursor
635
- withContext(Dispatchers .Main ) {
636
- // Hide the cursor (do not remove this, it's not a duplicate)
637
- binding.input.isCursorVisible = false
638
- // Scroll to the beginning
639
- binding.input.setSelection(0 )
640
- // Clear resultDisplay
641
- binding.resultDisplay.setText(" " )
603
+ // Set cursor
604
+ withContext(Dispatchers .Main ) {
605
+ // Scroll to the end
606
+ binding.input.setSelection(binding.input.length())
607
+
608
+ // Hide the cursor (do not remove this, it's not a duplicate)
609
+ binding.input.isCursorVisible = false
610
+
611
+ // Clear resultDisplay
612
+ binding.resultDisplay.setText(" " )
613
+ }
614
+
615
+ if (calculation != formattedResult) {
616
+ val history = MyPreferences (this @MainActivity).getHistory()
617
+
618
+ // Do not save to history if the previous entry is the same as the current one
619
+ if (history.isEmpty() || history[history.size - 1 ].calculation != calculation) {
620
+ // Store time
621
+ val currentTime = System .currentTimeMillis().toString()
622
+
623
+ // Save to history
624
+ history.add(
625
+ History (
626
+ calculation = calculation,
627
+ result = formattedResult,
628
+ time = currentTime,
629
+ )
630
+ )
631
+
632
+ MyPreferences (this @MainActivity).saveHistory(this @MainActivity, history)
633
+
634
+ // Update history variables
635
+ withContext(Dispatchers .Main ) {
636
+ historyAdapter.appendOneHistoryElement(
637
+ History (
638
+ calculation = calculation,
639
+ result = formattedResult,
640
+ time = currentTime,
641
+ )
642
+ )
643
+ // Scroll to the bottom of the recycle view
644
+ binding.historyRecylcleView.scrollToPosition(historyAdapter.itemCount - 1 )
645
+ }
642
646
}
643
647
}
644
648
} else {
0 commit comments