SignedCatalogSheet.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/browse/SignedCatalogSheet.kt
1package eu.friedcutlet.yokan.ui.screens.browse23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.Row6import androidx.compose.foundation.layout.fillMaxWidth7import androidx.compose.foundation.layout.padding8import androidx.compose.foundation.layout.size9import androidx.compose.foundation.lazy.LazyColumn10import androidx.compose.foundation.selection.toggleable11import androidx.compose.foundation.text.selection.SelectionContainer12import androidx.compose.material3.Button13import androidx.compose.material3.Checkbox14import androidx.compose.material3.CircularProgressIndicator15import androidx.compose.material3.ExperimentalMaterial3Api16import androidx.compose.material3.HorizontalDivider17import androidx.compose.material3.MaterialTheme18import androidx.compose.material3.ModalBottomSheet19import androidx.compose.material3.OutlinedTextField20import androidx.compose.material3.SheetState21import androidx.compose.material3.Text22import androidx.compose.material3.TextButton23import androidx.compose.runtime.Composable24import androidx.compose.ui.Alignment25import androidx.compose.ui.Modifier26import androidx.compose.ui.semantics.LiveRegionMode27import androidx.compose.ui.semantics.Role28import androidx.compose.ui.semantics.heading29import androidx.compose.ui.semantics.liveRegion30import androidx.compose.ui.semantics.semantics31import androidx.compose.ui.text.font.FontFamily32import androidx.compose.ui.unit.dp3334/**35 * Host once outside the Browse pager. Visibility, SheetState, localized text and all business36 * state belong to the caller. Callbacks must also guard duplicate operations in their owner.37 * Editing must clear the proposal/confirmation; dismissing is not rollback of a committed install.38 *39 * Suggested copy: "Add signed catalog", "Review catalog", "Enroll catalog", "Refresh catalog".40 * ReviewExplanation should ask users to compare the fingerprint with an independent publisher41 * source. TrustConfirmation: "I trust this signing key for this catalog". VerifiedExplanation:42 * "Signature verified with your trusted key" — not a claim that the publisher or recipe is safe.43 */44@OptIn(ExperimentalMaterial3Api::class)45@Composable46fun SignedCatalogSheet(47 state: SignedCatalogSheetState,48 sheetState: SheetState,49 text: (SignedCatalogText) -> String,50 errorText: (SignedCatalogError) -> String,51 onDismiss: () -> Unit,52 onCodeChange: (String) -> Unit,53 onReview: () -> Unit,54 onEditCode: () -> Unit,55 onTrustChange: (Boolean) -> Unit,56 onEnroll: () -> Unit,57 onRefresh: () -> Unit,58 onInstall: (String) -> Unit,59 modifier: Modifier = Modifier,60) {61 ModalBottomSheet(onDismissRequest = onDismiss, sheetState = sheetState, modifier = modifier) {62 LazyColumn(63 modifier = Modifier.fillMaxWidth(),64 verticalArrangement = Arrangement.spacedBy(20.dp),65 contentPadding = androidx.compose.foundation.layout.PaddingValues(start = 24.dp, end = 24.dp, bottom = 32.dp),66 ) {67 item(key = "header", contentType = "header") {68 Row(verticalAlignment = Alignment.CenterVertically) {69 Text(text(SignedCatalogText.Title), Modifier.weight(1f).semantics { heading() }, style = MaterialTheme.typography.headlineSmall)70 TextButton(onClick = onDismiss) { Text(text(SignedCatalogText.Close)) }71 }72 }73 when (val stage = state.stage) {74 is SignedCatalogStage.Paste -> item(key = "paste", contentType = "form") {75 Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {76 Text(text(SignedCatalogText.PasteExplanation), style = MaterialTheme.typography.bodyMedium)77 OutlinedTextField(78 value = stage.code,79 onValueChange = onCodeChange,80 enabled = !state.busy,81 label = { Text(text(SignedCatalogText.ShareCode)) },82 modifier = Modifier.fillMaxWidth(),83 minLines = 3,84 maxLines = 5,85 isError = stage.error != null,86 )87 stage.error?.let { CatalogError(it, errorText) }88 Button(onClick = onReview, enabled = state.canReview, modifier = Modifier.fillMaxWidth()) {89 Text(text(SignedCatalogText.ReviewCatalog))90 }91 }92 }93 is SignedCatalogStage.Review -> {94 item(key = "identity", contentType = "identity") { CatalogIdentity(stage.identity, text) }95 item(key = "trust", contentType = "form") {96 Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {97 Text(text(SignedCatalogText.ReviewExplanation), style = MaterialTheme.typography.bodyMedium)98 Row(99 modifier = Modifier.fillMaxWidth().toggleable(100 value = stage.trustConfirmed, enabled = !state.busy,101 role = Role.Checkbox, onValueChange = onTrustChange,102 ).padding(vertical = 12.dp),103 verticalAlignment = Alignment.CenterVertically,104 horizontalArrangement = Arrangement.spacedBy(12.dp),105 ) {106 Checkbox(checked = stage.trustConfirmed, onCheckedChange = null, enabled = !state.busy)107 Text(text(SignedCatalogText.TrustConfirmation), style = MaterialTheme.typography.bodyLarge)108 }109 stage.error?.let { CatalogError(it, errorText) }110 Button(onClick = onEnroll, enabled = state.canEnroll, modifier = Modifier.fillMaxWidth()) {111 Text(text(SignedCatalogText.EnrollCatalog))112 }113 TextButton(onClick = onEditCode, enabled = !state.busy) { Text(text(SignedCatalogText.EditCode)) }114 }115 }116 }117 is SignedCatalogStage.Enrolled -> {118 item(key = "identity", contentType = "identity") { CatalogIdentity(stage.identity, text) }119 item(key = "refresh", contentType = "form") {120 Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {121 Text(text(if (stage.alreadyEnrolled) SignedCatalogText.AlreadyEnrolled else SignedCatalogText.Enrolled), Modifier.semantics { liveRegion = LiveRegionMode.Polite })122 stage.refreshError?.let { CatalogError(it, errorText) }123 Button(onClick = onRefresh, enabled = !state.busy, modifier = Modifier.fillMaxWidth()) {124 Text(text(SignedCatalogText.RefreshCatalog))125 }126 }127 }128 }129 }130 if (state.busy) item(key = "progress", contentType = "status") {131 val label = when (state.operation) {132 SignedCatalogOperation.Enrolling -> SignedCatalogText.Enrolling133 SignedCatalogOperation.Refreshing -> SignedCatalogText.Refreshing134 else -> SignedCatalogText.Installing135 }136 Row(137 Modifier.semantics(mergeDescendants = true) { liveRegion = LiveRegionMode.Polite },138 horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically,139 ) {140 CircularProgressIndicator(Modifier.size(20.dp), strokeWidth = 2.dp)141 Text(text(label), style = MaterialTheme.typography.bodyMedium)142 }143 }144 state.verifiedPreview?.let { preview ->145 item(key = "verified", contentType = "status") {146 Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {147 HorizontalDivider()148 Text(text(SignedCatalogText.VerifiedExplanation), style = MaterialTheme.typography.titleMedium)149 CatalogValue(text(SignedCatalogText.Revision), preview.revisionLabel)150 CatalogValue(text(SignedCatalogText.Expires), preview.expiryLabel)151 if (preview.size == 0) Text(text(SignedCatalogText.EmptyRecipes))152 }153 }154 items(preview.size, key = { "recipe:${preview.recipeAt(it).stableId}" }, contentType = { "recipe" }) { index ->155 val recipe = preview.recipeAt(index)156 Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {157 SelectionContainer { Text(recipe.stableId, style = MaterialTheme.typography.titleMedium) }158 CatalogValue(text(SignedCatalogText.Version), recipe.version)159 CatalogValue(text(SignedCatalogText.Size), recipe.sizeLabel)160 recipe.error?.let { CatalogError(it, errorText) }161 val label = when {162 (state.operation as? SignedCatalogOperation.Installing)?.stableId == recipe.stableId -> SignedCatalogText.Installing163 recipe.status == SignedCatalogRecipeStatus.Installed -> SignedCatalogText.Installed164 recipe.status == SignedCatalogRecipeStatus.AlreadyInstalled -> SignedCatalogText.AlreadyInstalled165 else -> SignedCatalogText.Install166 }167 Button(onClick = { onInstall(recipe.stableId) }, enabled = state.canInstall(recipe.stableId)) {168 Text(text(label))169 }170 HorizontalDivider()171 }172 }173 }174 }175 }176}177178@Composable179private fun CatalogIdentity(identity: SignedCatalogIdentity, text: (SignedCatalogText) -> String) {180 Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {181 CatalogValue(text(SignedCatalogText.CatalogId), identity.catalogId)182 CatalogValue(text(SignedCatalogText.CatalogUrl), identity.url)183 CatalogValue(text(SignedCatalogText.KeyId), identity.keyId)184 CatalogValue(text(SignedCatalogText.Fingerprint), identity.fingerprint, monospace = true)185 }186}187188@Composable189private fun CatalogValue(label: String, value: String, monospace: Boolean = false) {190 Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {191 Text(label, style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)192 SelectionContainer {193 Text(value, modifier = Modifier.fillMaxWidth(), style = MaterialTheme.typography.bodyMedium,194 fontFamily = if (monospace) FontFamily.Monospace else null, softWrap = true)195 }196 }197}198199@Composable200private fun CatalogError(error: SignedCatalogError, errorText: (SignedCatalogError) -> String) {201 Text(errorText(error), modifier = Modifier.semantics { liveRegion = LiveRegionMode.Polite },202 color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodyMedium)203}204