Yokan / rebuild
Course 1 8624561

SignedCatalogSheetState.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/browse/SignedCatalogSheetState.kt

implementation153 lines
1package eu.friedcutlet.yokan.ui.screens.browse23import androidx.compose.runtime.Immutable45/** Display data only. The owner retains the parsed proposal and performs all trust checks. */6@Immutable7data class SignedCatalogIdentity(8    val catalogId: String,9    val url: String,10    val keyId: String,11    val fingerprint: String,12)1314enum class SignedCatalogError {15    InvalidCode, TrustConfirmationRequired, NotEnrolled, TrustMismatch, StorageFailure, StorageRejected,16    Timeout, DownloadFailed, CatalogRejected, ArtifactPolicyRejected,17    ArtifactUnavailable, ArtifactRejected, Conflict,18}1920enum class SignedCatalogRecipeStatus { Available, Installed, AlreadyInstalled }2122@Immutable23data class SignedCatalogRecipe(24    val stableId: String,25    val version: String,26    val sizeLabel: String,27    val status: SignedCatalogRecipeStatus = SignedCatalogRecipeStatus.Available,28    val error: SignedCatalogError? = null,29)3031/** Owns a snapshot, without exposing a mutable list to callers. IDs must be unique. */32@Immutable33class SignedCatalogPreview(34    val revisionLabel: String,35    val expiryLabel: String,36    recipes: List<SignedCatalogRecipe>,37) {38    private val rows = recipes.toList()39    init { require(rows.map { it.stableId }.distinct().size == rows.size) }40    val size: Int get() = rows.size41    fun recipeAt(index: Int): SignedCatalogRecipe = rows[index]42}4344@Immutable45sealed interface SignedCatalogStage {46    data class Paste(val code: String = "", val error: SignedCatalogError? = null) : SignedCatalogStage47    data class Review(48        val identity: SignedCatalogIdentity,49        val trustConfirmed: Boolean = false,50        val error: SignedCatalogError? = null,51    ) : SignedCatalogStage52    data class Enrolled(53        val identity: SignedCatalogIdentity,54        val alreadyEnrolled: Boolean = false,55        val preview: SignedCatalogPreview? = null,56        val refreshError: SignedCatalogError? = null,57    ) : SignedCatalogStage58}5960@Immutable61sealed interface SignedCatalogOperation {62    data object Idle : SignedCatalogOperation63    data object Enrolling : SignedCatalogOperation64    data object Refreshing : SignedCatalogOperation65    data class Installing(val stableId: String) : SignedCatalogOperation66}6768@Immutable69data class SignedCatalogSheetState(70    val stage: SignedCatalogStage = SignedCatalogStage.Paste(),71    val operation: SignedCatalogOperation = SignedCatalogOperation.Idle,72) {73    val busy: Boolean get() = operation != SignedCatalogOperation.Idle74    val canReview: Boolean get() = !busy && (stage as? SignedCatalogStage.Paste)?.code?.isNotBlank() == true75    val canEnroll: Boolean get() = !busy && (stage as? SignedCatalogStage.Review)?.let {76        it.trustConfirmed && it.error != SignedCatalogError.TrustMismatch77    } == true7879    // A failed refresh invalidates even a retained preview. Never render it as verified.80    val verifiedPreview: SignedCatalogPreview? get() = (stage as? SignedCatalogStage.Enrolled)81        ?.takeIf { it.refreshError == null && operation != SignedCatalogOperation.Refreshing }?.preview8283    fun canInstall(stableId: String): Boolean {84        if (busy) return false85        val preview = verifiedPreview ?: return false86        return (0 until preview.size).any { index ->87            val row = preview.recipeAt(index)88            row.stableId == stableId && row.status == SignedCatalogRecipeStatus.Available &&89                (row.error == null || row.error == SignedCatalogError.DownloadFailed || row.error == SignedCatalogError.Timeout)90        }91    }92}9394/** Localized by the host; no raw exceptions or service messages should be shown. */95enum class SignedCatalogText {96    Title, Close, PasteExplanation, ShareCode, ReviewCatalog, ReviewExplanation,97    CatalogId, CatalogUrl, KeyId, Fingerprint, TrustConfirmation, EnrollCatalog,98    EditCode, Enrolling, Enrolled, AlreadyEnrolled, RefreshCatalog, Refreshing,99    VerifiedExplanation, Revision, Expires, EmptyRecipes, Version, Size,100    Install, Installing, Installed, AlreadyInstalled,101}102103internal fun CatalogEnrollmentUiState.toSignedCatalogSheetState(): SignedCatalogSheetState {104    val displayError = error?.toSignedCatalogError()105    val review = review106    val identity = review?.let {107        SignedCatalogIdentity(it.catalogId, it.catalogEnvelopeUrl, it.keyId, it.publicKeyFingerprint)108    }109    val stage = when {110        review == null || !reviewing -> SignedCatalogStage.Paste(draftCode, displayError)111        enrolled -> SignedCatalogStage.Enrolled(112            identity = requireNotNull(identity),113            alreadyEnrolled = alreadyEnrolled,114            preview = preview?.let { catalog ->115                SignedCatalogPreview(116                    revisionLabel = catalog.revision.toString(),117                    expiryLabel = catalog.expiresAtEpochSeconds.toString(),118                    recipes = catalog.artifacts.map { artifact ->119                        SignedCatalogRecipe(120                            stableId = artifact.stableId,121                            version = artifact.releaseVersion,122                            sizeLabel = artifact.sizeBytes.toString(),123                            status = installedRecipes[artifact.stableId] ?: SignedCatalogRecipeStatus.Available,124                        )125                    },126                )127            },128            refreshError = displayError,129        )130        else -> SignedCatalogStage.Review(131            identity = requireNotNull(identity),132            trustConfirmed = trustConfirmed,133            error = displayError,134        )135    }136    val signedOperation = when (operation) {137        CatalogEnrollmentOperation.Enrolling -> SignedCatalogOperation.Enrolling138        CatalogEnrollmentOperation.Refreshing -> SignedCatalogOperation.Refreshing139        CatalogEnrollmentOperation.Installing -> SignedCatalogOperation.Installing((retry as? CatalogEnrollmentRetry.Install)?.stableId.orEmpty())140        null -> SignedCatalogOperation.Idle141    }142    return SignedCatalogSheetState(stage, signedOperation)143}144145private fun CatalogEnrollmentDisplayError.toSignedCatalogError(): SignedCatalogError = when (this) {146    CatalogEnrollmentDisplayError.InvalidCode -> SignedCatalogError.InvalidCode147    CatalogEnrollmentDisplayError.ConfirmationRequired -> SignedCatalogError.TrustConfirmationRequired148    CatalogEnrollmentDisplayError.EnrollmentRejected -> SignedCatalogError.StorageRejected149    CatalogEnrollmentDisplayError.RefreshRejected -> SignedCatalogError.CatalogRejected150    CatalogEnrollmentDisplayError.InstallRejected -> SignedCatalogError.ArtifactRejected151    CatalogEnrollmentDisplayError.RefreshRequired -> SignedCatalogError.Conflict152    CatalogEnrollmentDisplayError.InvalidSelection -> SignedCatalogError.ArtifactUnavailable153}154

Search chapters, reference and source filenames.