11package yetzio.yetcalc.widget
22
3+ import android.app.AlertDialog
4+ import android.content.ClipData
5+ import android.content.ClipboardManager
36import android.content.Context
7+ import android.content.Intent
48import 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
518import com.google.android.material.textfield.TextInputEditText
19+ import com.google.android.material.textview.MaterialTextView
20+ import yetzio.yetcalc.R
621
722interface CalcTextListener {
823 fun onUpdate ()
924 fun onCutText ()
25+ fun onSolve (selectedText : String ): String
1026}
1127
1228class 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 \n Result: $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 () {
0 commit comments