@@ -9,9 +9,13 @@ import android.os.Bundle
9
9
import android.view.HapticFeedbackConstants
10
10
import android.view.MenuItem
11
11
import android.view.View
12
- import android.widget.*
12
+ import android.widget.PopupMenu
13
13
import androidx.appcompat.app.AppCompatActivity
14
+ import androidx.lifecycle.lifecycleScope
14
15
import kotlinx.android.synthetic.main.activity_main.*
16
+ import kotlinx.coroutines.Dispatchers
17
+ import kotlinx.coroutines.launch
18
+ import kotlinx.coroutines.withContext
15
19
import org.mariuszgromada.math.mxparser.Expression
16
20
import org.mariuszgromada.math.mxparser.mXparser
17
21
@@ -21,6 +25,7 @@ class MainActivity : AppCompatActivity() {
21
25
// https://stackoverflow.com/questions/34197026/android-content-pm-applicationinfo-android-content-context-getapplicationinfo
22
26
private var isInvButtonClicked = false
23
27
28
+
24
29
override fun onCreate (savedInstanceState : Bundle ? ) {
25
30
super .onCreate(savedInstanceState)
26
31
@@ -49,8 +54,7 @@ class MainActivity : AppCompatActivity() {
49
54
setTheme(R .style.amoledTheme)
50
55
}
51
56
// Material You
52
- 3 ->
53
- {
57
+ 3 -> {
54
58
if (checkIfDarkModeIsEnabledByDefault()) {
55
59
setTheme(R .style.materialYouDark)
56
60
} else {
@@ -74,7 +78,7 @@ class MainActivity : AppCompatActivity() {
74
78
75
79
// Disable the keyboard on display EditText
76
80
input.showSoftInputOnFocus = false
77
-
81
+
78
82
// https://www.geeksforgeeks.org/how-to-detect-long-press-in-android/
79
83
backspaceButton.setOnLongClickListener {
80
84
input.setText(" " )
@@ -92,7 +96,7 @@ class MainActivity : AppCompatActivity() {
92
96
tableLayout.layoutTransition = lt
93
97
}
94
98
95
- private fun checkIfDarkModeIsEnabledByDefault (): Boolean =
99
+ private fun checkIfDarkModeIsEnabledByDefault (): Boolean =
96
100
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
97
101
resources.configuration.isNightModeActive
98
102
} else
@@ -111,7 +115,8 @@ class MainActivity : AppCompatActivity() {
111
115
val popup = PopupMenu (this , view)
112
116
val inflater = popup.menuInflater
113
117
inflater.inflate(R .menu.app_menu, popup.menu)
114
- popup.menu.findItem(R .id.app_menu_vibration_button).isChecked = MyPreferences (this ).vibrationMode;
118
+ popup.menu.findItem(R .id.app_menu_vibration_button).isChecked =
119
+ MyPreferences (this ).vibrationMode;
115
120
popup.show()
116
121
}
117
122
@@ -127,7 +132,7 @@ class MainActivity : AppCompatActivity() {
127
132
startActivity(browserIntent)
128
133
}
129
134
130
- fun keyVibration (view : View ) {
135
+ private fun keyVibration (view : View ) {
131
136
if (MyPreferences (this ).vibrationMode) {
132
137
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O_MR1 ) {
133
138
view.performHapticFeedback(HapticFeedbackConstants .KEYBOARD_PRESS )
@@ -136,78 +141,96 @@ class MainActivity : AppCompatActivity() {
136
141
}
137
142
138
143
private fun updateDisplay (view : View , value : String ) {
139
- // Vibrate when key pressed
140
- keyVibration(view)
141
-
142
- val formerValue = input.text.toString()
143
- val cursorPosition = input.selectionStart
144
- val leftValue = formerValue.subSequence(0 , cursorPosition).toString()
145
- val rightValue = formerValue.subSequence(cursorPosition, formerValue.length).toString()
144
+ lifecycleScope.launch(Dispatchers .Default ) {
145
+ withContext(Dispatchers .Main ) {
146
+ // Vibrate when key pressed
147
+ keyVibration(view)
148
+ }
146
149
147
- val newValue = leftValue + value + rightValue
150
+ val formerValue = input.text.toString()
151
+ val cursorPosition = input.selectionStart
152
+ val leftValue = formerValue.subSequence(0 , cursorPosition).toString()
153
+ val rightValue = formerValue.subSequence(cursorPosition, formerValue.length).toString()
148
154
149
- // Update Display
150
- input.setText(newValue)
155
+ val newValue = leftValue + value + rightValue
151
156
152
- // Increase cursor position
153
- input.setSelection(cursorPosition + value.length)
157
+ withContext(Dispatchers .Main ) {
158
+ // Update Display
159
+ input.setText(newValue)
154
160
155
- // Update resultDisplay
156
- updateResultDisplay()
161
+ // Increase cursor position
162
+ input.setSelection(cursorPosition + value.length)
163
+
164
+ // Update resultDisplay
165
+ updateResultDisplay()
166
+ }
167
+ }
157
168
}
158
169
159
170
private fun updateResultDisplay () {
160
- var calculation = input.text.toString()
171
+ lifecycleScope.launch(Dispatchers .Default ) {
172
+ var calculation = input.text.toString()
161
173
162
- if (calculation != " " ) {
163
- calculation = calculation.replace(' ×' , ' *' )
164
- calculation = calculation.replace(' ÷' , ' /' )
165
- calculation = calculation.replace(" log" , " log10" )
174
+ if (calculation != " " ) {
175
+ calculation = calculation.replace(' ×' , ' *' )
176
+ calculation = calculation.replace(' ÷' , ' /' )
177
+ calculation = calculation.replace(" log" , " log10" )
166
178
167
- // Add ")" which lack
168
- var openParentheses = 0
169
- var closeParentheses = 0
179
+ // Add ")" which lack
180
+ var openParentheses = 0
181
+ var closeParentheses = 0
170
182
171
- for (i in 0 .. calculation.length- 1 ) {
172
- if (calculation[i] == ' (' ) {
173
- openParentheses + = 1
174
- }
175
- if (calculation[i] == ' )' ) {
176
- closeParentheses + = 1
183
+ for (i in 0 .. calculation.length - 1 ) {
184
+ if (calculation[i] == ' (' ) {
185
+ openParentheses + = 1
186
+ }
187
+ if (calculation[i] == ' )' ) {
188
+ closeParentheses + = 1
189
+ }
177
190
}
178
- }
179
- if (closeParentheses < openParentheses) {
180
- for (i in 0 .. openParentheses - closeParentheses - 1 ) {
181
- calculation + = ' ) '
191
+ if (closeParentheses < openParentheses) {
192
+ for (i in 0 .. openParentheses - closeParentheses - 1 ) {
193
+ calculation + = ' ) '
194
+ }
182
195
}
183
- }
184
196
185
- val exp = Expression (calculation)
186
- var result = exp.calculate().toString()
187
-
188
- if (result != " NaN" && result != " Infinity" ) {
189
- // If the double ends with .0 we remove the .0
190
- if ((exp.calculate() * 10 ) % 10 == 0.0 ) {
191
- result = String .format(" %.0f" , exp.calculate())
192
- if (result != calculation) {
193
- resultDisplay.setText(result)
197
+ val exp = Expression (calculation)
198
+ var result = exp.calculate().toString()
199
+
200
+ if (result != " NaN" && result != " Infinity" ) {
201
+ // If the double ends with .0 we remove the .0
202
+ if ((exp.calculate() * 10 ) % 10 == 0.0 ) {
203
+ result = String .format(" %.0f" , exp.calculate())
204
+ withContext(Dispatchers .Main ) {
205
+ if (result != calculation) resultDisplay.setText(result)
206
+ else resultDisplay.setText(" " )
207
+ }
194
208
} else {
195
- resultDisplay.setText(" " )
209
+ withContext(Dispatchers .Main ) {
210
+
211
+ if (result != calculation) {
212
+ resultDisplay.setText(result)
213
+ } else {
214
+ resultDisplay.setText(" " )
215
+ }
216
+ }
196
217
}
197
- } else {
198
- if (result != calculation) {
199
- resultDisplay.setText(result)
218
+ } else withContext(Dispatchers .Main ) {
219
+ if (result == " Infinity" ) {
220
+
221
+ resultDisplay.setText(" Infinity" )
222
+
200
223
} else {
201
- resultDisplay.setText(" " )
224
+ withContext(Dispatchers .Main ) {
225
+ resultDisplay.setText(" " )
226
+ }
202
227
}
203
228
}
204
- } else if (result == " Infinity" ) {
205
- resultDisplay.setText(" Infinity" )
206
229
} else {
207
- resultDisplay.setText(" " )
230
+ withContext(Dispatchers .Main ) {
231
+ resultDisplay.setText(" " )
232
+ }
208
233
}
209
- } else {
210
- resultDisplay.setText(" " )
211
234
}
212
235
}
213
236
@@ -393,54 +416,59 @@ class MainActivity : AppCompatActivity() {
393
416
}
394
417
395
418
fun equalsButton (view : View ) {
396
- keyVibration(view)
397
419
398
- var calculation = input.text.toString()
399
- calculation = calculation.replace(' ×' , ' *' )
400
- calculation = calculation.replace(' ÷' , ' /' )
401
- calculation = calculation.replace(" log" , " log10" )
420
+ lifecycleScope.launch(Dispatchers .Default ) {
421
+ keyVibration(view)
422
+
423
+ var calculation = input.text.toString()
424
+ calculation = calculation.replace(' ×' , ' *' )
425
+ calculation = calculation.replace(' ÷' , ' /' )
426
+ calculation = calculation.replace(" log" , " log10" )
402
427
403
- if (calculation != " " ) {
404
- // Add ")" which lack
405
- var openParentheses = 0
406
- var closeParentheses = 0
428
+ if (calculation != " " ) {
429
+ // Add ")" which lack
430
+ var openParentheses = 0
431
+ var closeParentheses = 0
407
432
408
- for (i in 0 .. calculation.length- 1 ) {
409
- if (calculation[i] == ' (' ) {
410
- openParentheses + = 1
411
- }
412
- if (calculation[i] == ' )' ) {
413
- closeParentheses + = 1
433
+ for (i in 0 .. calculation.length - 1 ) {
434
+ if (calculation[i] == ' (' ) {
435
+ openParentheses + = 1
436
+ }
437
+ if (calculation[i] == ' )' ) {
438
+ closeParentheses + = 1
439
+ }
414
440
}
415
- }
416
- if (closeParentheses < openParentheses) {
417
- for (i in 0 .. openParentheses - closeParentheses - 1 ) {
418
- calculation + = ' ) '
441
+ if (closeParentheses < openParentheses) {
442
+ for (i in 0 .. openParentheses - closeParentheses - 1 ) {
443
+ calculation + = ' ) '
444
+ }
419
445
}
420
- }
421
446
422
- val exp = Expression (calculation)
423
- var result = exp.calculate().toString()
447
+ val exp = Expression (calculation)
448
+ var result = exp.calculate().toString()
449
+
450
+ mXparser.consolePrintln(" Res: " + exp.expressionString.toString() + " = " + exp.calculate())
424
451
425
- mXparser.consolePrintln(" Res: " + exp.expressionString.toString() + " = " + exp.calculate())
452
+ if (result != " NaN" && result != " Infinity" ) {
453
+ if ((exp.calculate() * 10 ) % 10 == 0.0 ) {
454
+ result = String .format(" %.0f" , exp.calculate())
455
+ withContext(Dispatchers .Main ) { input.setText(result) }
456
+ } else {
457
+ withContext(Dispatchers .Main ) { input.setText(result) }
458
+ }
459
+ // Set cursor
460
+ withContext(Dispatchers .Main ) {
461
+ input.setSelection(input.text.length)
426
462
427
- if (result != " NaN" && result != " Infinity" ) {
428
- if ((exp.calculate() * 10 ) % 10 == 0.0 ) {
429
- result = String .format(" %.0f" , exp.calculate())
430
- input.setText(result)
463
+ // Clear resultDisplay
464
+ resultDisplay.setText(" " )
465
+ }
431
466
} else {
432
- input. setText(result)
467
+ withContext( Dispatchers . Main ) { resultDisplay. setText(result) }
433
468
}
434
- // Set cursor
435
- input.setSelection(input.text.length)
436
-
437
- // Clear resultDisplay
438
- resultDisplay.setText(" " )
439
469
} else {
440
- resultDisplay.setText(result)
470
+ withContext( Dispatchers . Main ) { resultDisplay.setText(" " ) }
441
471
}
442
- } else {
443
- resultDisplay.setText(" " )
444
472
}
445
473
}
446
474
@@ -455,7 +483,7 @@ class MainActivity : AppCompatActivity() {
455
483
456
484
// https://kotlinlang.org/docs/ranges.html
457
485
// https://www.reddit.com/r/Kotlin/comments/couh07/getting_error_operator_cannot_be_applied_to_char/
458
- for (i in 0 .. cursorPosition- 1 ) {
486
+ for (i in 0 .. cursorPosition - 1 ) {
459
487
if (text[i] == ' (' ) {
460
488
openParentheses + = 1
461
489
}
@@ -467,12 +495,14 @@ class MainActivity : AppCompatActivity() {
467
495
if (openParentheses == closeParentheses || input.text.toString().subSequence(
468
496
textLength - 1 ,
469
497
textLength
470
- ) == " (" ) {
498
+ ) == " ("
499
+ ) {
471
500
updateDisplay(view, " (" )
472
501
} else if (closeParentheses < openParentheses && input.text.toString().subSequence(
473
502
textLength - 1 ,
474
503
textLength
475
- ) != " (" ) {
504
+ ) != " ("
505
+ ) {
476
506
updateDisplay(view, " )" )
477
507
}
478
508
@@ -486,10 +516,12 @@ class MainActivity : AppCompatActivity() {
486
516
val textLength = input.text.length
487
517
488
518
if (cursorPosition != 0 && textLength != 0 ) {
489
- val newValue = input.text.subSequence(0 , cursorPosition - 1 ).toString() + input.text.subSequence(
490
- cursorPosition,
491
- textLength
492
- ).toString()
519
+ val newValue =
520
+ input.text.subSequence(0 , cursorPosition - 1 )
521
+ .toString() + input.text.subSequence(
522
+ cursorPosition,
523
+ textLength
524
+ ).toString()
493
525
input.setText(newValue)
494
526
495
527
input.setSelection(cursorPosition - 1 )
@@ -499,8 +531,7 @@ class MainActivity : AppCompatActivity() {
499
531
}
500
532
501
533
fun scientistModeSwitchButton (view : View ) {
502
- if (scientistModeRow2.visibility != View .VISIBLE )
503
- {
534
+ if (scientistModeRow2.visibility != View .VISIBLE ) {
504
535
scientistModeRow2.visibility = View .VISIBLE
505
536
scientistModeRow3.visibility = View .VISIBLE
506
537
scientistModeSwitchButton.setImageResource(R .drawable.ic_baseline_keyboard_arrow_up_24)
0 commit comments