|
| 1 | +package com.getcode.view.main.getKin |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.net.Uri |
| 5 | +import androidx.compose.foundation.Image |
| 6 | +import androidx.compose.foundation.clickable |
| 7 | +import androidx.compose.foundation.layout.Box |
| 8 | +import androidx.compose.foundation.layout.aspectRatio |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.size |
| 12 | +import androidx.compose.foundation.lazy.LazyColumn |
| 13 | +import androidx.compose.foundation.lazy.items |
| 14 | +import androidx.compose.material.MaterialTheme |
| 15 | +import androidx.compose.material.Text |
| 16 | +import androidx.compose.runtime.Composable |
| 17 | +import androidx.compose.runtime.getValue |
| 18 | +import androidx.compose.runtime.remember |
| 19 | +import androidx.compose.ui.Alignment |
| 20 | +import androidx.compose.ui.Modifier |
| 21 | +import androidx.compose.ui.draw.clip |
| 22 | +import androidx.compose.ui.layout.ContentScale |
| 23 | +import androidx.compose.ui.platform.LocalContext |
| 24 | +import androidx.compose.ui.res.painterResource |
| 25 | +import androidx.compose.ui.res.stringResource |
| 26 | +import androidx.compose.ui.unit.dp |
| 27 | +import androidx.constraintlayout.compose.ConstraintLayout |
| 28 | +import androidx.hilt.navigation.compose.hiltViewModel |
| 29 | +import com.getcode.R |
| 30 | +import com.getcode.view.components.ButtonState |
| 31 | +import com.getcode.view.components.CodeButton |
| 32 | +import kotlinx.collections.immutable.toImmutableList |
| 33 | + |
| 34 | +@Composable |
| 35 | +fun BuyAndSellKin() { |
| 36 | + val context = LocalContext.current |
| 37 | + val viewModel = hiltViewModel<BuyAndSellKinViewModel>() |
| 38 | + |
| 39 | + val state by remember { viewModel.state } |
| 40 | + |
| 41 | + ConstraintLayout( |
| 42 | + modifier = Modifier |
| 43 | + .fillMaxWidth() |
| 44 | + .padding(horizontal = 20.dp), |
| 45 | + ) { |
| 46 | + val (topSection) = createRefs() |
| 47 | + |
| 48 | + LazyColumn( |
| 49 | + modifier = Modifier |
| 50 | + .fillMaxWidth() |
| 51 | + .constrainAs(topSection) { |
| 52 | + top.linkTo(parent.top) |
| 53 | + start.linkTo(parent.start) |
| 54 | + end.linkTo(parent.end) |
| 55 | + }, |
| 56 | + ) { |
| 57 | + item { |
| 58 | + Text( |
| 59 | + text = stringResource(R.string.title_buyAndSellKin), |
| 60 | + style = MaterialTheme.typography.h1, |
| 61 | + modifier = Modifier.padding(vertical = 15.dp), |
| 62 | + ) |
| 63 | + } |
| 64 | + item { |
| 65 | + Text( |
| 66 | + text = stringResource(R.string.subtitle_buySellDescription), |
| 67 | + style = MaterialTheme.typography.body1, |
| 68 | + modifier = Modifier.padding(vertical = 30.dp), |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + items(state.items.toImmutableList(), key = { it.link }) { item -> |
| 73 | + |
| 74 | + VideoThumbnail( |
| 75 | + context = context, |
| 76 | + imageResId = item.imageResId, |
| 77 | + link = item.link, |
| 78 | + onVideoClick = { context, link -> |
| 79 | + viewModel.openVideo( |
| 80 | + context = context, |
| 81 | + link = link, |
| 82 | + ) |
| 83 | + }, |
| 84 | + ) |
| 85 | + |
| 86 | + CodeButton( |
| 87 | + onClick = { |
| 88 | + viewModel.openVideo( |
| 89 | + context = context, |
| 90 | + link = item.link, |
| 91 | + ) |
| 92 | + }, |
| 93 | + text = stringResource(id = item.buttonTextResId), |
| 94 | + buttonState = ButtonState.Filled, |
| 95 | + ) |
| 96 | + |
| 97 | + CodeButton( |
| 98 | + modifier = Modifier.padding(bottom = 30.dp), |
| 99 | + onClick = { |
| 100 | + viewModel.shareVideo( |
| 101 | + context = context, |
| 102 | + link = item.link, |
| 103 | + ) |
| 104 | + }, |
| 105 | + text = stringResource(id = R.string.action_shareVideo), |
| 106 | + buttonState = ButtonState.Subtle, |
| 107 | + isPaddedVertical = false, |
| 108 | + ) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +@Composable |
| 115 | +private fun VideoThumbnail( |
| 116 | + context: Context, |
| 117 | + imageResId: Int, |
| 118 | + link: Uri, |
| 119 | + onVideoClick: (Context, Uri) -> Unit, |
| 120 | +) { |
| 121 | + Box( |
| 122 | + modifier = Modifier |
| 123 | + .fillMaxWidth() |
| 124 | + .padding(bottom = 10.dp) |
| 125 | + .clip(MaterialTheme.shapes.small) |
| 126 | + .clickable { onVideoClick(context, link) }, |
| 127 | + |
| 128 | + ) { |
| 129 | + Image( |
| 130 | + modifier = Modifier |
| 131 | + .aspectRatio(16f / 9f) |
| 132 | + .fillMaxWidth() |
| 133 | + .align(Alignment.CenterStart), |
| 134 | + contentScale = ContentScale.Fit, |
| 135 | + painter = painterResource(id = imageResId), |
| 136 | + contentDescription = "Video Thumbnail", |
| 137 | + ) |
| 138 | + |
| 139 | + Image( |
| 140 | + modifier = Modifier |
| 141 | + .size(70.dp) |
| 142 | + .align(Alignment.Center), |
| 143 | + painter = painterResource(id = R.drawable.youtube), |
| 144 | + contentDescription = "Youtube Logo", |
| 145 | + ) |
| 146 | + } |
| 147 | +} |
0 commit comments