Guest User

composable

a guest
Mar 22nd, 2022
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 10.52 KB | None | 0 0
  1. private data class CreditCard(val type: CreditCardTypes, val lastDigits: String)
  2.  
  3. @Composable
  4. fun CardSelectionScreen(onCardSelected: () -> Unit) {
  5.     Column(
  6.         horizontalAlignment = Alignment.CenterHorizontally,
  7.         modifier = Modifier
  8.             .padding(vertical = 24.dp, horizontal = 16.dp)
  9.             .verticalScroll(rememberScrollState())
  10.     ) {
  11.         var selectedCardIndex by remember { mutableStateOf(0) }
  12.         val cardList = listOf(
  13.             CreditCard(CreditCardTypes.MASTERCARD, "3429"),
  14.             CreditCard(CreditCardTypes.VISA, "3429"),
  15.             CreditCard(CreditCardTypes.MASTERCARD, "3429")
  16.         )
  17.         TopBarPadding(true)
  18.         Spacer(modifier = Modifier.height(10.dp))
  19.         RaisedCard() {
  20.             Column(
  21.                 modifier = Modifier.padding(vertical = 20.dp),
  22.                 verticalArrangement = Arrangement.spacedBy(8.dp),
  23.             ) {
  24.                 Text(
  25.                     text = stringResource(id = R.string.please_select_a_card),
  26.                     fontSize = 16.sp,
  27.                     fontWeight = FontWeight.Bold,
  28.                     textAlign = TextAlign.Start
  29.                 )
  30.                 Spacer(modifier = Modifier.height(9.dp))
  31.                 for (i in cardList.indices) {
  32.                     CreditCardItem(cardList[i],
  33.                         isSelected = selectedCardIndex == i, onItemSelected = { ->
  34.                             selectedCardIndex = i
  35.                         })
  36.                 }
  37.                 ShadowWrapper( // This is the item's layout
  38.                     cardElevation = 1.dp,
  39.                     shadowElevation = 3.dp
  40.                 ) {
  41.                     Column(
  42.                         modifier = Modifier
  43.                             .animateContentSize()
  44.                     ) {
  45.                         Row(
  46.                             modifier = Modifier
  47.                                 .fillMaxWidth()
  48.                                 .clip(RoundedCornerShape(6.dp))
  49.                                 .background(
  50.                                     if (selectedCardIndex == cardList.size) colorResource(
  51.                                         id = R.color.bottom_modal_drawer_background
  52.                                     ) else Color.White
  53.                                 )
  54.                                 .padding(horizontal = 16.dp, vertical = 16.dp)
  55.                                 .clickable(
  56.                                     indication = null,
  57.                                     interactionSource = MutableInteractionSource()
  58.                                 ) { // this does not register at all, tried with Log.d
  59.                                     selectedCardIndex = cardList.size
  60.                                 },
  61.                             verticalAlignment = Alignment.CenterVertically
  62.                         ) {
  63.                             Image(
  64.                                 painter = painterResource(id = R.drawable.ic_add_credit_card),
  65.                                 contentDescription = "Add credit card icon"
  66.                             )
  67.                             Spacer(modifier = Modifier.width(13.dp))
  68.                             Text(
  69.                                 stringResource(id = R.string.new_card_addition),
  70.                                 textAlign = TextAlign.Start,
  71.                                 fontSize = 16.sp,
  72.                                 fontWeight = FontWeight.Normal,
  73.                                 color = colorResource(id = R.color.Orange_100)
  74.                             )
  75.                         }
  76.                         if (selectedCardIndex == cardList.size) {
  77.                             Column(
  78.                                 modifier = Modifier.padding(
  79.                                     horizontal = 16.dp
  80.                                 )
  81.                             ) {
  82.                                 Spacer(modifier = Modifier.padding(22.fixedDp()))
  83.                                 Text(
  84.                                     text = LocalContext.current.getString(R.string.add_credit_card_top_msg),
  85.                                     fontSize = 16.sp,
  86.                                     fontWeight = FontWeight.Bold,
  87.                                     color = colorResource(id = R.color.Black_100)
  88.                                 )
  89.                                 Spacer(modifier = Modifier.padding(10.dp))
  90.                                 InputField(label = LocalContext.current.getString(R.string.owner_name))
  91.                                 Spacer(modifier = Modifier.padding(18.fixedDp()))
  92.                                 InputField(label = LocalContext.current.getString(R.string.credit_card_number))
  93.                                 Spacer(modifier = Modifier.padding(18.fixedDp()))
  94.                                 Row() {
  95.                                     Box(
  96.                                         modifier = Modifier
  97.                                             .weight(1.5f)
  98.                                     ) {
  99.                                         InputField(label = LocalContext.current.getString(R.string.expiration_date))
  100.                                     }
  101.                                     Spacer(modifier = Modifier.padding(6.fixedDp()))
  102.                                     Box(
  103.                                         modifier = Modifier
  104.                                             .weight(1f)
  105.                                     ) {
  106.                                         InputField(
  107.                                             label = LocalContext.current.getString(R.string.cvv),
  108.                                             isPassword = true,
  109.                                             placeholder = ""
  110.                                         )
  111.                                     }
  112.                                 }
  113.                                 Spacer(modifier = Modifier.height(34.fixedDp()))
  114.                                 Row() {
  115.                                     MyCheckbox(
  116.                                         modifier = Modifier.padding(top = 3.dp),
  117.                                         isCheckedInitially = true
  118.                                     )
  119.                                     Spacer(modifier = Modifier.width(13.dp))
  120.                                     Text(
  121.                                         stringResource(id = R.string.save_card_for_future_transactions),
  122.                                         fontSize = 14.sp,
  123.                                         fontWeight = FontWeight.Normal,
  124.                                         color = colorResource(id = R.color.Black_100)
  125.                                     )
  126.                                 }
  127.                                 Spacer(modifier = Modifier.padding(22.fixedDp()))
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.                 Spacer(modifier = Modifier.height(2.dp))
  133.             }
  134.         }
  135.         Spacer(modifier = Modifier.height(32.dp))
  136.         MyButton(
  137.             text = stringResource(id = R.string.continue_text),
  138.             MyButtonType.PRIMARY,
  139.             onClick = { onCardSelected() }
  140.         )
  141.         Spacer(modifier = Modifier.height(20.dp))
  142.         AcceptedCardsFooter()
  143.         BottomBarPadding(true)
  144.     }
  145. }
  146.  
  147. @Composable
  148. private fun CreditCardItem(
  149.     cardDetails: CreditCard,
  150.     isSelected: Boolean,
  151.     onItemSelected: () -> Unit
  152. ) {
  153.     ShadowWrapper(cardElevation = 1.dp, shadowElevation = 3.dp) {
  154.         Row(
  155.             modifier = Modifier
  156.                 .fillMaxWidth()
  157.                 .clip(RoundedCornerShape(6.dp))
  158.                 .background(if (isSelected) colorResource(id = R.color.bottom_modal_drawer_background) else Color.White)
  159.                 .padding(horizontal = 16.dp, vertical = 15.dp)
  160.                 .clickable(indication = null, interactionSource = MutableInteractionSource()) {
  161.                     onItemSelected()
  162.                 },
  163.             horizontalArrangement = Arrangement.SpaceBetween,
  164.             verticalAlignment = Alignment.CenterVertically
  165.         ) {
  166.             Row(verticalAlignment = Alignment.CenterVertically) {
  167.                 MyRadioButton(label = "", selected = isSelected)
  168.                 Spacer(modifier = Modifier.width(16.dp))
  169.                 Box(
  170.                     modifier = Modifier
  171.                         .width(43.dp)
  172.                         .height(33.dp)
  173.                         .clip(RoundedCornerShape(4.dp))
  174.                         .background(colorResource(id = R.color.Grey_10))
  175.                         .padding(horizontal = 6.dp, vertical = 7.dp)
  176.                 ) {
  177.                     Image(
  178.                         painter = painterResource(id = cardDetails.type.icon),
  179.                         contentDescription = "",
  180.                         modifier = Modifier.align(Alignment.Center)
  181.                     )
  182.                 }
  183.                 Spacer(modifier = Modifier.padding(8.fixedDp()))
  184.                 Text(
  185.                     text = "${cardDetails.type.prefix}****${cardDetails.lastDigits}",
  186.                     fontSize = 16.sp,
  187.                     color = colorResource(id = R.color.Black_100)
  188.                 )
  189.             }
  190.         }
  191.     }
  192. }
  193.  
  194. @Composable
  195. fun RaisedCard(
  196.     modifier: Modifier = Modifier,
  197.     mainBody: @Composable () -> Unit
  198. ) {
  199.     Card(
  200.         shape = RoundedCornerShape(13.dp),
  201.         elevation = 10.dp,
  202.         modifier = modifier
  203.             .fillMaxWidth()
  204.             .wrapContentHeight()
  205.     ) {
  206.         Column(
  207.             modifier = Modifier
  208.                 .background(Color.White)
  209.                 .padding(horizontal = 16.dp)
  210.         ) {
  211.             mainBody()
  212.         }
  213.     }
  214. }
  215.  
  216. @Preview(widthDp = 300, heightDp = 500)
  217. @Composable
  218. private fun TopUpCardViewPreview() {
  219.     Column(
  220.         modifier = Modifier.padding(horizontal = 16.dp, vertical = 25.dp)
  221.     ) {
  222.         RaisedCard() {
  223.             Text("Your main layout goes here")
  224.         }
  225.     }
  226. }
  227.  
  228. @Composable
  229. fun ShadowWrapper(
  230.     modifier: Modifier = Modifier,
  231.     border: BorderStroke = BorderStroke(0.dp, Color.Transparent),
  232.     cardElevation: Dp = 2.dp,
  233.     shadowElevation: Dp = 1.dp,
  234.     shadowShapeRadius: Dp = 6.dp,
  235.     content: @Composable () -> Unit,
  236. ) {
  237.     Card(
  238.         elevation = cardElevation,
  239.         border = border,
  240.         shape = RoundedCornerShape(shadowShapeRadius),
  241.         modifier = modifier.shadow(shadowElevation, RoundedCornerShape(shadowShapeRadius)).wrapContentHeight()
  242.     ) {
  243.         content()
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment