|
| 1 | +package com.enaboapps.switchify.screens.screenhighlights |
| 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.shape.RoundedCornerShape |
| 14 | +import androidx.compose.foundation.verticalScroll |
| 15 | +import androidx.compose.material3.Card |
| 16 | +import androidx.compose.material3.CircularProgressIndicator |
| 17 | +import androidx.compose.material3.MaterialTheme |
| 18 | +import androidx.compose.material3.Surface |
| 19 | +import androidx.compose.material3.Text |
| 20 | +import androidx.compose.runtime.Composable |
| 21 | +import androidx.compose.ui.Alignment |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.res.stringResource |
| 24 | +import androidx.compose.ui.text.font.FontWeight |
| 25 | +import androidx.compose.ui.text.style.TextAlign |
| 26 | +import androidx.compose.ui.unit.dp |
| 27 | +import com.enaboapps.switchify.R |
| 28 | +import com.enaboapps.switchify.components.ActionButton |
| 29 | +import com.enaboapps.switchify.components.ActionButtonType |
| 30 | +import com.enaboapps.switchify.service.llm.ExtractedItem |
| 31 | +import com.enaboapps.switchify.service.llm.HighlightType |
| 32 | +import com.enaboapps.switchify.theme.Dimens |
| 33 | + |
| 34 | +@Composable |
| 35 | +fun ScreenHighlightsScreen( |
| 36 | + state: ScreenHighlightsUiState, |
| 37 | + onSelect: (ExtractedItem) -> Unit, |
| 38 | + onRetry: () -> Unit, |
| 39 | + onClose: () -> Unit |
| 40 | +) { |
| 41 | + Surface( |
| 42 | + modifier = Modifier.fillMaxSize(), |
| 43 | + color = MaterialTheme.colorScheme.background |
| 44 | + ) { |
| 45 | + Column( |
| 46 | + modifier = Modifier |
| 47 | + .fillMaxSize() |
| 48 | + .systemBarsPadding() |
| 49 | + .padding(Dimens.spaceL) |
| 50 | + ) { |
| 51 | + Text( |
| 52 | + text = stringResource(R.string.screen_highlights_title), |
| 53 | + style = MaterialTheme.typography.headlineSmall, |
| 54 | + fontWeight = FontWeight.SemiBold |
| 55 | + ) |
| 56 | + Spacer(modifier = Modifier.height(Dimens.spaceL)) |
| 57 | + |
| 58 | + Box( |
| 59 | + modifier = Modifier |
| 60 | + .weight(1f) |
| 61 | + .fillMaxWidth() |
| 62 | + ) { |
| 63 | + when (state) { |
| 64 | + is ScreenHighlightsUiState.Loading -> LoadingContent() |
| 65 | + |
| 66 | + is ScreenHighlightsUiState.Items -> |
| 67 | + ItemsContent(state.items, onSelect) |
| 68 | + |
| 69 | + is ScreenHighlightsUiState.Empty -> |
| 70 | + EmptyContent(onRetry) |
| 71 | + |
| 72 | + is ScreenHighlightsUiState.Failed -> |
| 73 | + FailedContent(state, onRetry) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 78 | + ActionButton( |
| 79 | + textResId = R.string.screen_highlights_close, |
| 80 | + onClick = onClose, |
| 81 | + modifier = Modifier.fillMaxWidth(), |
| 82 | + type = ActionButtonType.SECONDARY, |
| 83 | + applyPadding = false |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +@Composable |
| 90 | +private fun LoadingContent() { |
| 91 | + Column( |
| 92 | + modifier = Modifier.fillMaxSize(), |
| 93 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 94 | + verticalArrangement = Arrangement.Center |
| 95 | + ) { |
| 96 | + CircularProgressIndicator() |
| 97 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 98 | + Text( |
| 99 | + text = stringResource(R.string.screen_highlights_processing), |
| 100 | + style = MaterialTheme.typography.bodyLarge |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +@Composable |
| 106 | +private fun ItemsContent(items: List<ExtractedItem>, onSelect: (ExtractedItem) -> Unit) { |
| 107 | + Column( |
| 108 | + modifier = Modifier |
| 109 | + .fillMaxSize() |
| 110 | + .verticalScroll(rememberScrollState()), |
| 111 | + verticalArrangement = Arrangement.spacedBy(Dimens.spaceS) |
| 112 | + ) { |
| 113 | + Text( |
| 114 | + text = stringResource(R.string.screen_highlights_pick_hint), |
| 115 | + style = MaterialTheme.typography.bodyMedium, |
| 116 | + color = MaterialTheme.colorScheme.onSurfaceVariant |
| 117 | + ) |
| 118 | + items.forEach { item -> |
| 119 | + HighlightCard(item = item, onClick = { onSelect(item) }) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +@Composable |
| 125 | +private fun HighlightCard(item: ExtractedItem, onClick: () -> Unit) { |
| 126 | + Card( |
| 127 | + onClick = onClick, |
| 128 | + modifier = Modifier.fillMaxWidth() |
| 129 | + ) { |
| 130 | + Column( |
| 131 | + modifier = Modifier.padding(Dimens.spaceM), |
| 132 | + verticalArrangement = Arrangement.spacedBy(Dimens.spaceXs) |
| 133 | + ) { |
| 134 | + TypeChip(item.type) |
| 135 | + Text( |
| 136 | + text = item.value, |
| 137 | + style = MaterialTheme.typography.bodyLarge |
| 138 | + ) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +@Composable |
| 144 | +private fun TypeChip(type: HighlightType) { |
| 145 | + Surface( |
| 146 | + color = MaterialTheme.colorScheme.secondaryContainer, |
| 147 | + contentColor = MaterialTheme.colorScheme.onSecondaryContainer, |
| 148 | + shape = RoundedCornerShape(8.dp) |
| 149 | + ) { |
| 150 | + Text( |
| 151 | + text = stringResource(typeLabelRes(type)), |
| 152 | + style = MaterialTheme.typography.labelSmall, |
| 153 | + fontWeight = FontWeight.SemiBold, |
| 154 | + modifier = Modifier.padding(horizontal = Dimens.spaceXs, vertical = 4.dp) |
| 155 | + ) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +private fun typeLabelRes(type: HighlightType): Int = when (type) { |
| 160 | + HighlightType.URL -> R.string.screen_highlights_type_url |
| 161 | + HighlightType.PHONE -> R.string.screen_highlights_type_phone |
| 162 | + HighlightType.EMAIL -> R.string.screen_highlights_type_email |
| 163 | + HighlightType.DATE -> R.string.screen_highlights_type_date |
| 164 | + HighlightType.ADDRESS -> R.string.screen_highlights_type_address |
| 165 | + HighlightType.OTHER -> R.string.screen_highlights_type_other |
| 166 | +} |
| 167 | + |
| 168 | +@Composable |
| 169 | +private fun EmptyContent(onRetry: () -> Unit) { |
| 170 | + Column( |
| 171 | + modifier = Modifier.fillMaxSize(), |
| 172 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 173 | + verticalArrangement = Arrangement.Center |
| 174 | + ) { |
| 175 | + Text( |
| 176 | + text = stringResource(R.string.screen_highlights_none_found), |
| 177 | + style = MaterialTheme.typography.bodyLarge, |
| 178 | + textAlign = TextAlign.Center |
| 179 | + ) |
| 180 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 181 | + ActionButton( |
| 182 | + textResId = R.string.screen_highlights_retry, |
| 183 | + onClick = onRetry |
| 184 | + ) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +@Composable |
| 189 | +private fun FailedContent(state: ScreenHighlightsUiState.Failed, onRetry: () -> Unit) { |
| 190 | + Column( |
| 191 | + modifier = Modifier.fillMaxSize(), |
| 192 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 193 | + verticalArrangement = Arrangement.Center |
| 194 | + ) { |
| 195 | + Text( |
| 196 | + text = stringResource(state.messageRes), |
| 197 | + style = MaterialTheme.typography.bodyLarge, |
| 198 | + textAlign = TextAlign.Center |
| 199 | + ) |
| 200 | + if (state.canRetry) { |
| 201 | + Spacer(modifier = Modifier.height(Dimens.spaceM)) |
| 202 | + ActionButton( |
| 203 | + textResId = R.string.screen_highlights_retry, |
| 204 | + onClick = onRetry |
| 205 | + ) |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments