Skip to content

Commit 5d1b279

Browse files
authored
Merge pull request #181 from Yet-Zio/v2.0.6
v2.0.6
2 parents a33735a + 64443f3 commit 5d1b279

File tree

9 files changed

+231
-19
lines changed

9 files changed

+231
-19
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ android {
2525
applicationId = "yetzio.yetcalc"
2626
minSdk = 28
2727
targetSdk = 35
28-
versionCode = 21
29-
versionName = "2.0.5"
28+
versionCode = 22
29+
versionName = "2.0.6"
3030

3131
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
3232
}

app/src/main/java/yetzio/yetcalc/views/CalculatorActivity.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import kotlinx.coroutines.CoroutineScope
2525
import kotlinx.coroutines.Dispatchers
2626
import kotlinx.coroutines.async
2727
import kotlinx.coroutines.launch
28+
import kotlinx.coroutines.runBlocking
29+
import kotlinx.coroutines.withContext
2830
import yetzio.yetcalc.R
2931
import yetzio.yetcalc.component.SharedPrefs
3032
import yetzio.yetcalc.config.CalcBaseActivity
@@ -153,6 +155,15 @@ class CalculatorActivity : CalcBaseActivity(), View.OnClickListener {
153155
result = textres.text.toString()
154156
}
155157
}
158+
159+
override fun onSolve(selectedText: String): String {
160+
return runBlocking {
161+
val result = withContext(Dispatchers.Default) {
162+
Calc.calculate(selectedText)
163+
}
164+
result
165+
}
166+
}
156167
})
157168

158169
val fontPref = preferences.getBoolean(SharedPrefs.SYSFONTCALCKEY, false)

app/src/main/java/yetzio/yetcalc/views/ProgramCalcActivity.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import kotlinx.coroutines.CoroutineScope
2222
import kotlinx.coroutines.Dispatchers
2323
import kotlinx.coroutines.async
2424
import kotlinx.coroutines.launch
25+
import kotlinx.coroutines.runBlocking
26+
import kotlinx.coroutines.withContext
2527
import yetzio.yetcalc.R
2628
import yetzio.yetcalc.component.SharedPrefs
2729
import yetzio.yetcalc.config.CalcBaseActivity
@@ -173,6 +175,15 @@ class ProgramCalcActivity : CalcBaseActivity(), View.OnClickListener{
173175
textres.setText("")
174176
}
175177
}
178+
179+
override fun onSolve(selectedText: String): String {
180+
return runBlocking {
181+
val result = withContext(Dispatchers.Default) {
182+
mviewModel.numberSys.value?.let { PGCalc.calculate(selectedText, it) }
183+
}
184+
result.toString()
185+
}
186+
}
176187
})
177188

178189
val fontPref = preferences.getBoolean(SharedPrefs.SYSFONTPGCALCKEY, false)

app/src/main/java/yetzio/yetcalc/widget/CalcText.kt

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
11
package yetzio.yetcalc.widget
22

3+
import android.app.AlertDialog
4+
import android.content.ClipData
5+
import android.content.ClipboardManager
36
import android.content.Context
7+
import android.content.Intent
48
import android.util.AttributeSet
9+
import android.util.TypedValue
10+
import android.view.ActionMode
11+
import android.view.LayoutInflater
12+
import android.view.Menu
13+
import android.view.MenuItem
14+
import android.widget.TextView
15+
import androidx.core.content.ContextCompat
16+
import com.google.android.material.button.MaterialButton
17+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
518
import com.google.android.material.textfield.TextInputEditText
19+
import com.google.android.material.textview.MaterialTextView
20+
import yetzio.yetcalc.R
621

722
interface CalcTextListener {
823
fun onUpdate()
924
fun onCutText()
25+
fun onSolve(selectedText: String): String
1026
}
1127

1228
class CalcText : TextInputEditText {
13-
var listeners: ArrayList<CalcTextListener>
29+
var listeners: ArrayList<CalcTextListener> = ArrayList()
30+
private val SOLVE_ACTION_ID = 1000
1431

15-
constructor(context: Context?) : super(context!!) {
16-
listeners = ArrayList()
17-
}
32+
constructor(context: Context?) : super(context!!)
33+
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)
34+
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context!!, attrs, defStyle)
1835

19-
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
20-
listeners = ArrayList()
21-
}
36+
init {
37+
customSelectionActionModeCallback = object : ActionMode.Callback {
38+
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
39+
menu.add(Menu.NONE, SOLVE_ACTION_ID, Menu.NONE, "Solve")
40+
return true
41+
}
42+
43+
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
44+
return false
45+
}
46+
47+
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
48+
return when (item.itemId) {
49+
SOLVE_ACTION_ID -> {
50+
handleSolveAction()
51+
mode.finish()
52+
true
53+
}
54+
else -> false
55+
}
56+
}
2257

23-
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
24-
context!!,
25-
attrs,
26-
defStyle
27-
) {
28-
listeners = ArrayList()
58+
override fun onDestroyActionMode(mode: ActionMode) {}
59+
}
2960
}
3061

3162
fun addListener(listener: CalcTextListener) {
@@ -36,6 +67,62 @@ class CalcText : TextInputEditText {
3667
}
3768
}
3869

70+
private fun handleSolveAction() {
71+
val selectedText = text.toString().substring(selectionStart, selectionEnd)
72+
73+
val solveResult = listeners.firstNotNullOfOrNull { listener ->
74+
runCatching { listener.onSolve(selectedText) }.getOrNull()
75+
}
76+
77+
solveResult?.let { result ->
78+
showSolveResultDialog(selectedText, result)
79+
}
80+
}
81+
82+
private fun showSolveResultDialog(expression: String, result: String) {
83+
val dialogView = LayoutInflater.from(context).inflate(R.layout.expression_solve_dialog, null)
84+
val expressionTextView = dialogView.findViewById<MaterialTextView>(R.id.tv_expression)
85+
val resultTextView = dialogView.findViewById<MaterialTextView>(R.id.tv_result)
86+
val btnCopy = dialogView.findViewById<MaterialButton>(R.id.btn_copy)
87+
val btnShare = dialogView.findViewById<MaterialButton>(R.id.btn_share)
88+
val btnClose = dialogView.findViewById<MaterialButton>(R.id.btn_close)
89+
90+
val dialog = MaterialAlertDialogBuilder(context, com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog)
91+
.setView(dialogView)
92+
.create()
93+
94+
expressionTextView.apply {
95+
setText(expression)
96+
}
97+
98+
resultTextView.apply {
99+
setText(result)
100+
}
101+
102+
// Copy button
103+
btnCopy.setOnClickListener {
104+
(context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
105+
.setPrimaryClip(ClipData.newPlainText("Solved Result", result))
106+
dialog.dismiss()
107+
}
108+
109+
// Share button
110+
btnShare.setOnClickListener {
111+
val shareIntent = Intent(Intent.ACTION_SEND).apply {
112+
type = "text/plain"
113+
putExtra(Intent.EXTRA_TEXT, "Expression: $expression\nResult: $result")
114+
}
115+
context.startActivity(Intent.createChooser(shareIntent, "Share Result"))
116+
dialog.dismiss()
117+
}
118+
119+
btnClose.setOnClickListener {
120+
dialog.dismiss()
121+
}
122+
123+
dialog.show()
124+
}
125+
39126
override fun onTextContextMenuItem(id: Int): Boolean {
40127
val consumed = super.onTextContextMenuItem(id)
41128
when (id) {
@@ -50,6 +137,7 @@ class CalcText : TextInputEditText {
50137
for (listener in listeners)
51138
listener.onCutText()
52139
}
140+
53141
fun onTextCopy() {}
54142

55143
fun onTextPaste() {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="vertical"
6+
android:padding="16dp">
7+
8+
<TextView
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:text="Solution"
12+
android:textColor="@color/textexp"
13+
android:textSize="24sp"
14+
android:layout_marginBottom="8dp"/>
15+
16+
<LinearLayout
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:orientation="horizontal">
20+
<com.google.android.material.textview.MaterialTextView
21+
android:id="@+id/tv_expressionlabel"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:textColor="@color/textexp"
25+
android:textSize="18sp"
26+
android:text="Expression:"
27+
android:layout_marginEnd="5dp"
28+
android:layout_marginBottom="4dp"/>
29+
30+
<com.google.android.material.textview.MaterialTextView
31+
android:id="@+id/tv_expression"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:textColor="@color/textexp"
35+
android:textSize="20sp"
36+
android:layout_marginBottom="4dp"/>
37+
</LinearLayout>
38+
39+
<LinearLayout
40+
android:layout_width="match_parent"
41+
android:layout_height="wrap_content"
42+
android:orientation="horizontal">
43+
44+
<com.google.android.material.textview.MaterialTextView
45+
android:id="@+id/tv_reslabel"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:textColor="@color/textexp"
49+
android:textSize="18sp"
50+
android:text="Result:"
51+
android:layout_marginEnd="5dp"
52+
android:layout_marginBottom="4dp"/>
53+
54+
<com.google.android.material.textview.MaterialTextView
55+
android:id="@+id/tv_result"
56+
android:layout_width="wrap_content"
57+
android:layout_height="wrap_content"
58+
android:textColor="@color/textexp"
59+
android:textSize="20sp"
60+
android:layout_marginBottom="16dp"/>
61+
</LinearLayout>
62+
63+
64+
65+
<LinearLayout
66+
android:layout_width="match_parent"
67+
android:layout_height="wrap_content"
68+
android:orientation="horizontal"
69+
android:gravity="end">
70+
71+
<com.google.android.material.button.MaterialButton
72+
android:id="@+id/btn_close"
73+
style="@style/Widget.Material3.Button.TextButton"
74+
android:textColor="@color/textexp"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:text="Close"/>
78+
79+
<com.google.android.material.button.MaterialButton
80+
android:id="@+id/btn_copy"
81+
style="@style/Widget.Material3.Button.TextButton"
82+
android:layout_width="wrap_content"
83+
android:layout_height="wrap_content"
84+
android:textColor="@color/textexp"
85+
android:text="Copy"/>
86+
87+
<com.google.android.material.button.MaterialButton
88+
android:id="@+id/btn_share"
89+
style="@style/Widget.Material3.Button.TextButton"
90+
android:layout_width="wrap_content"
91+
android:layout_height="wrap_content"
92+
android:textColor="@color/textexp"
93+
android:text="Share"/>
94+
</LinearLayout>
95+
</LinearLayout>

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string name="VERSION_NAMEANDNUM">重回正轨 (2.0.5)</string>
3+
<string name="VERSION_NAMEANDNUM">重回正轨 (2.0.6)</string>
44
<string name="eastereggtoast">你现在拥有了能带领人类撕碎高维度数学难题的完美躯体!(Gigachad.mp4)</string>
55
<string name="app_descrp">Yet another calculator by Yet Zio</string>
66
<string name="ghbtext">在Github查看</string>

app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<dimen name="last_chip_margin_end">10dp</dimen>
4-
<dimen name="textExpFontSize">33sp</dimen>
4+
<dimen name="textExpFontSize">20sp</dimen>
55
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<resources>
22
<string name="author_name" translatable="false">Yet Zio</string>
33
<string name="app_name" translatable="false">yetCalc</string>
4-
<string name="VERSION_NUM" translatable="false">2.0.5</string>
5-
<string name="VERSION_NAMEANDNUM">Back in Action (2.0.5)</string>
4+
<string name="VERSION_NUM" translatable="false">2.0.6</string>
5+
<string name="VERSION_NAMEANDNUM">Back in Action (2.0.6)</string>
66
<string name="eastereggtoast">Congratulations, Now you are a Gigachad!</string>
77
<string name="app_descrp">Yet another calculator by Yet Zio</string>
88
<string name="ghbtext">View on GitHub</string>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Fixes
2+
3+
- Font size fixes for smaller layouts
4+
5+
Enhancements
6+
7+
- Added a solve and share option in text selection context menu.

0 commit comments

Comments
 (0)