|
| 1 | +package com.enaboapps.switchify.screens.replydrafter |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Box |
| 5 | +import androidx.compose.foundation.layout.Column |
| 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.height |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.systemBarsPadding |
| 12 | +import androidx.compose.foundation.rememberScrollState |
| 13 | +import androidx.compose.foundation.verticalScroll |
| 14 | +import androidx.compose.material3.Card |
| 15 | +import androidx.compose.material3.CircularProgressIndicator |
| 16 | +import androidx.compose.material3.MaterialTheme |
| 17 | +import androidx.compose.material3.Surface |
| 18 | +import androidx.compose.material3.Text |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.ui.Alignment |
| 21 | +import androidx.compose.ui.Modifier |
| 22 | +import androidx.compose.ui.res.stringResource |
| 23 | +import androidx.compose.ui.text.font.FontWeight |
| 24 | +import androidx.compose.ui.text.style.TextAlign |
| 25 | +import com.enaboapps.switchify.R |
| 26 | +import com.enaboapps.switchify.components.ActionButton |
| 27 | +import com.enaboapps.switchify.components.ActionButtonType |
| 28 | +import com.enaboapps.switchify.theme.Dimens |
| 29 | + |
| 30 | +@Composable |
| 31 | +fun ReplyDrafterScreen( |
| 32 | + state: ReplyDrafterUiState, |
| 33 | + onSelect: (String) -> Unit, |
| 34 | + onRetry: () -> Unit, |
| 35 | + onClose: () -> Unit |
| 36 | +) { |
| 37 | + Surface( |
| 38 | + modifier = Modifier.fillMaxSize(), |
| 39 | + color = MaterialTheme.colorScheme.background |
| 40 | + ) { |
| 41 | + Column( |
| 42 | + modifier = Modifier |
| 43 | + .fillMaxSize() |
| 44 | + .systemBarsPadding() |
| 45 | + .padding(Dimens.spaceL) |
| 46 | + ) { |
| 47 | + Text( |
| 48 | + text = stringResource(R.string.reply_drafter_suggestions_title), |
| 49 | + style = MaterialTheme.typography.headlineSmall, |
| 50 | + fontWeight = FontWeight.SemiBold |
| 51 | + ) |
| 52 | + Spacer(modifier = Modifier.height(Dimens.spaceL)) |
| 53 | + |
| 54 | + Box( |
| 55 | + modifier = Modifier |
| 56 | + .weight(1f) |
| 57 | + .fillMaxWidth() |
| 58 | + ) { |
| 59 | + when (state) { |
| 60 | + is ReplyDrafterUiState.Loading -> LoadingContent() |
| 61 | + |
| 62 | + is ReplyDrafterUiState.Suggestions -> |
| 63 | + SuggestionsContent(state.replies, onSelect) |
| 64 | + |
| 65 | + is ReplyDrafterUiState.Failed -> |
| 66 | + FailedContent(state, onRetry) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 71 | + ActionButton( |
| 72 | + textResId = R.string.reply_drafter_close, |
| 73 | + onClick = onClose, |
| 74 | + modifier = Modifier.fillMaxWidth(), |
| 75 | + type = ActionButtonType.SECONDARY, |
| 76 | + applyPadding = false |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@Composable |
| 83 | +private fun LoadingContent() { |
| 84 | + Column( |
| 85 | + modifier = Modifier.fillMaxSize(), |
| 86 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 87 | + verticalArrangement = Arrangement.Center |
| 88 | + ) { |
| 89 | + CircularProgressIndicator() |
| 90 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 91 | + Text( |
| 92 | + text = stringResource(R.string.reply_drafter_processing), |
| 93 | + style = MaterialTheme.typography.bodyLarge |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +@Composable |
| 99 | +private fun SuggestionsContent(replies: List<String>, onSelect: (String) -> Unit) { |
| 100 | + Column( |
| 101 | + modifier = Modifier |
| 102 | + .fillMaxSize() |
| 103 | + .verticalScroll(rememberScrollState()), |
| 104 | + verticalArrangement = Arrangement.spacedBy(Dimens.spaceS) |
| 105 | + ) { |
| 106 | + Text( |
| 107 | + text = stringResource(R.string.reply_drafter_pick_hint), |
| 108 | + style = MaterialTheme.typography.bodyMedium, |
| 109 | + color = MaterialTheme.colorScheme.onSurfaceVariant |
| 110 | + ) |
| 111 | + replies.forEach { reply -> |
| 112 | + ReplyCard(reply = reply, onClick = { onSelect(reply) }) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +@Composable |
| 118 | +private fun ReplyCard(reply: String, onClick: () -> Unit) { |
| 119 | + Card( |
| 120 | + onClick = onClick, |
| 121 | + modifier = Modifier.fillMaxWidth() |
| 122 | + ) { |
| 123 | + Text( |
| 124 | + text = reply, |
| 125 | + modifier = Modifier.padding(Dimens.spaceM), |
| 126 | + style = MaterialTheme.typography.bodyLarge |
| 127 | + ) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +@Composable |
| 132 | +private fun FailedContent(state: ReplyDrafterUiState.Failed, onRetry: () -> Unit) { |
| 133 | + Column( |
| 134 | + modifier = Modifier.fillMaxSize(), |
| 135 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 136 | + verticalArrangement = Arrangement.Center |
| 137 | + ) { |
| 138 | + Text( |
| 139 | + text = stringResource(state.messageRes), |
| 140 | + style = MaterialTheme.typography.bodyLarge, |
| 141 | + textAlign = TextAlign.Center |
| 142 | + ) |
| 143 | + if (state.canRetry) { |
| 144 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 145 | + ActionButton( |
| 146 | + textResId = R.string.reply_drafter_retry, |
| 147 | + onClick = onRetry |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments