@@ -45,6 +45,7 @@ class CardInputWidget @JvmOverloads constructor(
45
45
) : LinearLayout(context, attrs, defStyleAttr), CardWidget {
46
46
private val cardIconImageView: ImageView
47
47
private val frameLayout: FrameLayout
48
+
48
49
private val cardNumberEditText: CardNumberEditText
49
50
private val cvcNumberEditText: StripeEditText
50
51
private val expiryDateEditText: ExpiryDateEditText
@@ -267,12 +268,10 @@ class CardInputWidget @JvmOverloads constructor(
267
268
return super .onInterceptTouchEvent(ev)
268
269
}
269
270
270
- val focusEditText = getFocusRequestOnTouch(ev.x.toInt())
271
- if (focusEditText != null ) {
272
- focusEditText.requestFocus()
273
- return true
274
- }
275
- return super .onInterceptTouchEvent(ev)
271
+ return getFocusRequestOnTouch(ev.x.toInt())?.let {
272
+ it.requestFocus()
273
+ true
274
+ } ? : super .onInterceptTouchEvent(ev)
276
275
}
277
276
278
277
override fun onSaveInstanceState (): Parcelable {
@@ -460,11 +459,10 @@ class CardInputWidget @JvmOverloads constructor(
460
459
info : AccessibilityNodeInfoCompat
461
460
) {
462
461
super .onInitializeAccessibilityNodeInfo(host, info)
463
- val accLabel = resources.getString(
462
+ info.text = resources.getString(
464
463
R .string.acc_label_cvc_node,
465
464
cvcNumberEditText.text
466
465
)
467
- info.text = accLabel
468
466
}
469
467
})
470
468
@@ -509,11 +507,8 @@ class CardInputWidget @JvmOverloads constructor(
509
507
}
510
508
}
511
509
512
- expiryDateEditText.setDeleteEmptyListener(
513
- BackUpFieldDeleteListener (cardNumberEditText))
514
-
515
- cvcNumberEditText.setDeleteEmptyListener(
516
- BackUpFieldDeleteListener (expiryDateEditText))
510
+ expiryDateEditText.setDeleteEmptyListener(BackUpFieldDeleteListener (cardNumberEditText))
511
+ cvcNumberEditText.setDeleteEmptyListener(BackUpFieldDeleteListener (expiryDateEditText))
517
512
518
513
cvcNumberEditText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
519
514
if (hasFocus) {
@@ -540,33 +535,21 @@ class CardInputWidget @JvmOverloads constructor(
540
535
}
541
536
)
542
537
543
- cardNumberEditText.setCardNumberCompleteListener(
544
- object : CardNumberEditText .CardNumberCompleteListener {
545
- override fun onCardNumberComplete () {
546
- scrollRight()
547
- cardInputListener?.onCardComplete()
548
- }
549
- }
550
- )
538
+ cardNumberEditText.completionCallback = {
539
+ scrollRight()
540
+ cardInputListener?.onCardComplete()
541
+ }
551
542
552
- cardNumberEditText.setCardBrandChangeListener(
553
- object : CardNumberEditText .CardBrandChangeListener {
554
- override fun onCardBrandChanged (brand : String ) {
555
- isAmEx = CardBrand .AMERICAN_EXPRESS == brand
556
- updateIcon(brand)
557
- updateCvc(brand)
558
- }
559
- }
560
- )
543
+ cardNumberEditText.brandChangeCallback = { brand ->
544
+ isAmEx = CardBrand .AMERICAN_EXPRESS == brand
545
+ updateIcon(brand)
546
+ updateCvc(brand)
547
+ }
561
548
562
- expiryDateEditText.setExpiryDateEditListener(
563
- object : ExpiryDateEditText .ExpiryDateEditListener {
564
- override fun onExpiryDateComplete () {
565
- cvcNumberEditText.requestFocus()
566
- cardInputListener?.onExpirationComplete()
567
- }
568
- }
569
- )
549
+ expiryDateEditText.completionCallback = {
550
+ cvcNumberEditText.requestFocus()
551
+ cardInputListener?.onExpirationComplete()
552
+ }
570
553
571
554
cardNumberEditText.requestFocus()
572
555
}
@@ -627,10 +610,11 @@ class CardInputWidget @JvmOverloads constructor(
627
610
slideDateLeftAnimation.duration = ANIMATION_LENGTH
628
611
slideCvcLeftAnimation.duration = ANIMATION_LENGTH
629
612
630
- val animationSet = AnimationSet (true )
631
- animationSet.addAnimation(slideCardLeftAnimation)
632
- animationSet.addAnimation(slideDateLeftAnimation)
633
- animationSet.addAnimation(slideCvcLeftAnimation)
613
+ val animationSet = AnimationSet (true ).apply {
614
+ addAnimation(slideCardLeftAnimation)
615
+ addAnimation(slideDateLeftAnimation)
616
+ addAnimation(slideCvcLeftAnimation)
617
+ }
634
618
frameLayout.startAnimation(animationSet)
635
619
cardNumberIsViewed = true
636
620
}
@@ -642,7 +626,7 @@ class CardInputWidget @JvmOverloads constructor(
642
626
643
627
val dateStartMargin = placementParameters.cardWidth + placementParameters.cardDateSeparation
644
628
645
- updateSpaceSizes(false )
629
+ updateSpaceSizes(isCardViewed = false )
646
630
647
631
val slideCardRightAnimation = object : Animation () {
648
632
override fun applyTransformation (interpolatedTime : Float , t : Transformation ) {
@@ -693,10 +677,11 @@ class CardInputWidget @JvmOverloads constructor(
693
677
}
694
678
})
695
679
696
- val animationSet = AnimationSet (true )
697
- animationSet.addAnimation(slideCardRightAnimation)
698
- animationSet.addAnimation(slideDateRightAnimation)
699
- animationSet.addAnimation(slideCvcRightAnimation)
680
+ val animationSet = AnimationSet (true ).apply {
681
+ addAnimation(slideCardRightAnimation)
682
+ addAnimation(slideDateRightAnimation)
683
+ addAnimation(slideCvcRightAnimation)
684
+ }
700
685
701
686
frameLayout.startAnimation(animationSet)
702
687
cardNumberIsViewed = false
@@ -724,10 +709,11 @@ class CardInputWidget @JvmOverloads constructor(
724
709
}
725
710
setLayoutValues(placementParameters.cardWidth, cardLeftMargin, cardNumberEditText)
726
711
727
- val dateMargin = if (cardNumberIsViewed)
712
+ val dateMargin = if (cardNumberIsViewed) {
728
713
placementParameters.cardWidth + placementParameters.cardDateSeparation
729
- else
714
+ } else {
730
715
placementParameters.peekCardWidth + placementParameters.cardDateSeparation
716
+ }
731
717
setLayoutValues(placementParameters.dateWidth, dateMargin, expiryDateEditText)
732
718
733
719
val cvcMargin = if (cardNumberIsViewed) {
@@ -781,10 +767,10 @@ class CardInputWidget @JvmOverloads constructor(
781
767
782
768
private fun updateCvc (@Card.CardBrand brand : String ) {
783
769
if (CardBrand .AMERICAN_EXPRESS == brand) {
784
- cvcNumberEditText.filters = arrayOf< InputFilter >( InputFilter . LengthFilter ( Card . CVC_LENGTH_AMERICAN_EXPRESS ))
770
+ cvcNumberEditText.filters = INPUT_FILTER_AMEX
785
771
cvcNumberEditText.setHint(R .string.cvc_amex_hint)
786
772
} else {
787
- cvcNumberEditText.filters = arrayOf< InputFilter >( InputFilter . LengthFilter ( Card . CVC_LENGTH_COMMON ))
773
+ cvcNumberEditText.filters = INPUT_FILTER_COMMON
788
774
cvcNumberEditText.setHint(R .string.cvc_number_hint)
789
775
}
790
776
}
@@ -812,54 +798,59 @@ class CardInputWidget @JvmOverloads constructor(
812
798
}
813
799
814
800
private fun updateIconForCvcEntry (isAmEx : Boolean ) {
815
- if (isAmEx) {
816
- cardIconImageView.setImageResource( R .drawable.ic_cvc_amex)
801
+ cardIconImageView.setImageResource( if (isAmEx) {
802
+ R .drawable.ic_cvc_amex
817
803
} else {
818
- cardIconImageView.setImageResource( R .drawable.ic_cvc)
819
- }
804
+ R .drawable.ic_cvc
805
+ })
820
806
applyTint(true )
821
807
}
822
808
823
809
/* *
824
810
* Interface useful for testing calculations without generating real views.
825
811
*/
826
- @VisibleForTesting
827
812
internal interface DimensionOverrideSettings {
828
-
829
813
val frameWidth: Int
814
+
830
815
fun getPixelWidth (text : String , editText : EditText ): Int
831
816
}
832
817
833
818
/* *
834
819
* A data-dump class.
835
820
*/
836
821
internal class PlacementParameters {
837
- var cardWidth: Int = 0
838
- var hiddenCardWidth: Int = 0
839
- var peekCardWidth: Int = 0
840
- var cardDateSeparation: Int = 0
841
- var dateWidth: Int = 0
842
- var dateCvcSeparation: Int = 0
843
- var cvcWidth: Int = 0
844
-
845
- var cardTouchBufferLimit: Int = 0
846
- var dateStartPosition: Int = 0
847
- var dateRightTouchBufferLimit: Int = 0
848
- var cvcStartPosition: Int = 0
822
+ internal var cardWidth: Int = 0
823
+ internal var hiddenCardWidth: Int = 0
824
+ internal var peekCardWidth: Int = 0
825
+ internal var cardDateSeparation: Int = 0
826
+ internal var dateWidth: Int = 0
827
+ internal var dateCvcSeparation: Int = 0
828
+ internal var cvcWidth: Int = 0
829
+
830
+ internal var cardTouchBufferLimit: Int = 0
831
+ internal var dateStartPosition: Int = 0
832
+ internal var dateRightTouchBufferLimit: Int = 0
833
+ internal var cvcStartPosition: Int = 0
849
834
850
835
override fun toString (): String {
851
- val touchBufferData = " Touch Buffer Data:\n " +
852
- " CardTouchBufferLimit = $cardTouchBufferLimit \n " +
853
- " DateStartPosition = $dateStartPosition \n " +
854
- " DateRightTouchBufferLimit = $dateRightTouchBufferLimit \n " +
836
+ val touchBufferData = """
837
+ Touch Buffer Data:
838
+ "CardTouchBufferLimit = $cardTouchBufferLimit
839
+ "DateStartPosition = $dateStartPosition
840
+ "DateRightTouchBufferLimit = $dateRightTouchBufferLimit
855
841
"CvcStartPosition = $cvcStartPosition "
856
- val elementSizeData = " CardWidth = $cardWidth \n " +
857
- " HiddenCardWidth = $hiddenCardWidth \n " +
858
- " PeekCardWidth = $peekCardWidth \n " +
859
- " CardDateSeparation = $cardDateSeparation \n " +
860
- " DateWidth = $dateWidth \n " +
861
- " DateCvcSeparation = $dateCvcSeparation \n " +
862
- " CvcWidth = $cvcWidth \n "
842
+ """
843
+
844
+ val elementSizeData = """
845
+ CardWidth = $cardWidth
846
+ HiddenCardWidth = $hiddenCardWidth
847
+ PeekCardWidth = $peekCardWidth
848
+ CardDateSeparation = $cardDateSeparation
849
+ DateWidth = $dateWidth
850
+ DateCvcSeparation = $dateCvcSeparation
851
+ CvcWidth = $cvcWidth
852
+ """
853
+
863
854
return elementSizeData + touchBufferData
864
855
}
865
856
}
@@ -904,6 +895,12 @@ class CardInputWidget @JvmOverloads constructor(
904
895
905
896
private const val ANIMATION_LENGTH = 150L
906
897
898
+ private val INPUT_FILTER_AMEX : Array <InputFilter > =
899
+ arrayOf(InputFilter .LengthFilter (Card .CVC_LENGTH_AMERICAN_EXPRESS ))
900
+
901
+ private val INPUT_FILTER_COMMON : Array <InputFilter > =
902
+ arrayOf(InputFilter .LengthFilter (Card .CVC_LENGTH_COMMON ))
903
+
907
904
/* *
908
905
* Determines whether or not the icon should show the card brand instead of the
909
906
* CVC helper icon.
@@ -920,9 +917,7 @@ class CardInputWidget @JvmOverloads constructor(
920
917
cvcHasFocus : Boolean ,
921
918
cvcText : String?
922
919
): Boolean {
923
- return if (! cvcHasFocus) {
924
- true
925
- } else ViewUtils .isCvcMaximalLength(brand, cvcText)
920
+ return ! cvcHasFocus || ViewUtils .isCvcMaximalLength(brand, cvcText)
926
921
}
927
922
}
928
923
}
0 commit comments