|
1 | 1 | package co.yml.ychat.android.presentation.images |
2 | 2 |
|
3 | 3 | import androidx.compose.foundation.background |
4 | | -import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Box |
5 | 5 | import androidx.compose.foundation.layout.Column |
6 | | -import androidx.compose.foundation.layout.fillMaxHeight |
| 6 | +import androidx.compose.foundation.layout.Spacer |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.padding |
| 10 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 11 | +import androidx.compose.material.CircularProgressIndicator |
7 | 12 | import androidx.compose.runtime.Composable |
| 13 | +import androidx.compose.runtime.collectAsState |
| 14 | +import androidx.compose.ui.Alignment |
8 | 15 | import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.draw.clip |
| 17 | +import androidx.compose.ui.layout.ContentScale |
| 18 | +import androidx.compose.ui.res.stringResource |
9 | 19 | import androidx.compose.ui.tooling.preview.Preview |
| 20 | +import co.yml.ychat.YChat |
| 21 | +import co.yml.ychat.android.BuildConfig |
| 22 | +import co.yml.ychat.android.R |
| 23 | +import co.yml.ychat.android.presentation.images.ImagesViewModel.State |
| 24 | +import co.yml.ychat.android.ui.components.button.ButtonContained |
10 | 25 | import co.yml.ychat.android.ui.components.feedback.Feedback |
11 | 26 | import co.yml.ychat.android.ui.components.feedback.model.FeedbackState |
| 27 | +import co.yml.ychat.android.ui.components.placeholder.ImagePlaceHolder |
| 28 | +import co.yml.ychat.android.ui.components.textfield.StandardTextField |
| 29 | +import co.yml.ychat.android.ui.theme.Dimens |
| 30 | +import co.yml.ychat.android.ui.theme.TypographyStyle |
12 | 31 | import co.yml.ychat.android.ui.theme.YChatTheme |
| 32 | +import coil.compose.SubcomposeAsyncImage |
| 33 | +import org.koin.androidx.compose.getViewModel |
13 | 34 |
|
14 | 35 | @Composable |
15 | | -internal fun ImagesScreen() { |
| 36 | +internal fun ImagesScreen(viewModel: ImagesViewModel = getViewModel()) { |
| 37 | + val state = viewModel.state.collectAsState().value |
16 | 38 | Column( |
17 | 39 | modifier = Modifier |
18 | 40 | .background(YChatTheme.colors.background) |
19 | | - .fillMaxHeight(), |
20 | | - verticalArrangement = Arrangement.Center, |
| 41 | + .padding(Dimens.MD) |
| 42 | + .fillMaxSize(), |
21 | 43 | ) { |
22 | | - Feedback(feedbackState = FeedbackState.CONSTRUCTION) |
| 44 | + TypographyStyle.MediumBody.Text(text = stringResource(id = R.string.images_input_title)) |
| 45 | + StandardTextField( |
| 46 | + value = viewModel.inputMessage.value, |
| 47 | + hint = stringResource(id = R.string.images_input_hint), |
| 48 | + modifier = Modifier |
| 49 | + .fillMaxWidth() |
| 50 | + .padding(top = Dimens.MD) |
| 51 | + , |
| 52 | + onTextChanged = { viewModel.onInputMessage(it) }, |
| 53 | + enabled = state !is State.Loading, |
| 54 | + ) |
| 55 | + ButtonContained( |
| 56 | + text = stringResource(id = R.string.images_action), |
| 57 | + modifier = Modifier |
| 58 | + .fillMaxWidth() |
| 59 | + .padding(top = Dimens.XM), |
| 60 | + enabled = viewModel.onEnableButton.value, |
| 61 | + onClick = { viewModel.requestEdits() } |
| 62 | + ) |
| 63 | + Box(modifier = Modifier.fillMaxSize()) { |
| 64 | + when (state) { |
| 65 | + is State.Loading -> |
| 66 | + CircularProgressIndicator( |
| 67 | + modifier = Modifier.align(Alignment.Center) |
| 68 | + ) |
| 69 | + is State.Error -> |
| 70 | + Feedback( |
| 71 | + modifier = Modifier.align(Alignment.Center), |
| 72 | + feedbackState = FeedbackState.ERROR |
| 73 | + ) |
| 74 | + is State.Success -> |
| 75 | + SubcomposeAsyncImage( |
| 76 | + model = state.generatedImage, |
| 77 | + loading = { ImagePlaceHolder() }, |
| 78 | + contentDescription = null, |
| 79 | + contentScale = ContentScale.FillBounds, |
| 80 | + modifier = Modifier |
| 81 | + .fillMaxSize() |
| 82 | + .padding(top = Dimens.XM) |
| 83 | + .clip(RoundedCornerShape(Dimens.XS)) |
| 84 | + ) |
| 85 | + else -> Spacer(modifier = Modifier) |
| 86 | + } |
| 87 | + } |
23 | 88 | } |
24 | 89 | } |
25 | 90 |
|
26 | 91 | @Preview |
27 | 92 | @Composable |
28 | 93 | private fun ImagesScreenPreview() { |
29 | 94 | YChatTheme { |
30 | | - ImagesScreen() |
| 95 | + val yChat = YChat.create(BuildConfig.API_KEY) |
| 96 | + val viewModel = ImagesViewModel(yChat) |
| 97 | + ImagesScreen(viewModel) |
31 | 98 | } |
32 | 99 | } |
0 commit comments