@@ -19,7 +19,12 @@ import androidx.annotation.RequiresApi
1919import androidx.core.content.res.ResourcesCompat
2020import Skyflow.core.elements.state.StateforText
2121import Skyflow.utils.EventName
22+ import android.content.ClipData
23+ import android.content.ClipboardManager
2224import android.graphics.Typeface
25+ import android.graphics.drawable.Drawable
26+ import android.os.Handler
27+ import android.os.Looper
2328import android.text.Spanned
2429import androidx.core.text.isDigitsOnly
2530import com.skyflow_android.R
@@ -28,6 +33,8 @@ import kotlin.String
2833import android.text.InputFilter
2934import android.text.InputFilter.LengthFilter
3035import android.util.Log
36+ import android.view.MotionEvent
37+ import androidx.core.content.ContextCompat
3138import com.Skyflow.collect.elements.validations.*
3239import com.Skyflow.collect.elements.validations.SkyflowValidator
3340import com.Skyflow.collect.elements.validations.SkyflowValidateExpireDate
@@ -62,6 +69,8 @@ class TextField @JvmOverloads constructor(
6269
6370 private var isFormatting = false
6471
72+ private var drawableRight: Drawable ? = null
73+
6574 override var uuid = " "
6675 override fun getValue (): String {
6776 return actualValue
@@ -100,6 +109,66 @@ class TextField @JvmOverloads constructor(
100109 }
101110 }
102111
112+ private fun appendIcon (iconName : String ) {
113+ lateinit var drawable: Drawable
114+ val copyDrawable = ContextCompat .getDrawable(context, R .drawable.ic_copy)
115+ val copiedDrawable = ContextCompat .getDrawable(context, R .drawable.ic_copied)
116+
117+ when (iconName) {
118+ " COPY" -> drawable = copyDrawable!!
119+ " COPIED" -> {
120+ drawable = copiedDrawable!!
121+ Handler (Looper .getMainLooper()).postDelayed({
122+ inputField.setCompoundDrawablesWithIntrinsicBounds(
123+ null ,
124+ null ,
125+ copyDrawable,
126+ null
127+ )
128+ drawableRight = copyDrawable
129+ }, 2000 ) // 2000 milliseconds = 2 seconds
130+ }
131+ }
132+ inputField.setCompoundDrawablesRelativeWithIntrinsicBounds(null , null , drawable, null )
133+ inputField.compoundDrawablePadding = 8
134+ drawableRight = drawable
135+ }
136+
137+ private fun removeIcon () {
138+ inputField.setCompoundDrawablesRelativeWithIntrinsicBounds(null , null , null , null )
139+ }
140+
141+ override fun onTouchEvent (event : MotionEvent ? ): Boolean {
142+ when (event?.action) {
143+ MotionEvent .ACTION_DOWN -> {
144+ return performCopyAction(event)
145+ }
146+ }
147+ return super .onTouchEvent(event)
148+ }
149+
150+ private fun performCopyAction (event : MotionEvent ): Boolean {
151+ val extraTapArea = 10
152+ val actionX = event.rawX
153+ if (drawableRight != null ) {
154+ val wBound = inputField.right - inputField.compoundDrawables[2 ].bounds.width()
155+ if (actionX >= wBound - extraTapArea && actionX <= inputField.right) {
156+ handleTap()
157+ return true
158+ }
159+ }
160+ return true
161+ }
162+
163+ private fun handleTap () {
164+ val textToCopy = this .actualValue
165+ val clipboard = context.getSystemService(Context .CLIPBOARD_SERVICE ) as ClipboardManager
166+ val clip = ClipData .newPlainText(" CustomCopyTextView" , textToCopy)
167+ clipboard.setPrimaryClip(clip)
168+ VibrationHelper .vibrate(context, 10 )
169+ appendIcon(" COPIED" )
170+ }
171+
103172 override fun setupField (
104173 collectInput : CollectElementInput ,
105174 options : CollectElementOptions
@@ -219,7 +288,6 @@ class TextField @JvmOverloads constructor(
219288 }
220289 }
221290
222-
223291 @RequiresApi(Build .VERSION_CODES .JELLY_BEAN_MR1 )
224292 public override fun onAttachedToWindow () {
225293 super .onAttachedToWindow()
@@ -240,11 +308,9 @@ class TextField @JvmOverloads constructor(
240308 // when text changes
241309 inputField.addTextChangedListener(object : TextWatcher {
242310 override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {
243-
244311 }
245312
246313 override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {
247-
248314 }
249315
250316 override fun afterTextChanged (s : Editable ? ) {
@@ -254,6 +320,13 @@ class TextField @JvmOverloads constructor(
254320 actualValue = inputField.text.toString()
255321 formatPatternForField(s)
256322 state = StateforText (this @TextField)
323+ if (state.getInternalState().getBoolean(" isValid" )){
324+ if (options.enableCopy) {
325+ appendIcon(" COPY" )
326+ }
327+ }else if (options.enableCopy){
328+ removeIcon()
329+ }
257330 if (userOnchangeListener != = null )
258331 userOnchangeListener?.let {
259332 it(
0 commit comments