FeedbackBody.kt

5.0 KB · raw

1/*2 * D8 — drop-in replacement for the private `FeedbackBody` in3 * `ui/settings/SettingsCardBodies.kt`.4 *5 * This file is NOT meant to be added as-is — it lives in the same file as the6 * other *Body composables and reuses that file's private helpers (SubLabel,7 * Pill, SubtleButton, Hint). The orchestrator should:8 *   1. Change the call site at line 78 from `FeedbackBody(fg)` to9 *      `FeedbackBody(fg, vm)`.10 *   2. Replace the existing `FeedbackBody(fg: Color)` (lines 581-623) with the11 *      `FeedbackBody` below.12 *   3. Add the imports listed in INTEGRATION.md.13 *14 * Behaviour vs. the old version:15 *   - "Send" now calls `vm.sendFeedback(category, message)` instead of flipping16 *     a local `sent` flag.17 *   - State is read from `vm.feedbackState` (FeedbackState): Sending shows a18 *     spinner and disables the button; Sent shows the existing thank-you card;19 *     Error shows an inline breach-coloured message and re-enables Send (retry).20 *   - On entering the card / after a Sent, the VM state resets to Idle (call21 *     `vm.resetFeedback()` from a DisposableEffect — see snippet).22 */2324@OptIn(ExperimentalLayoutApi::class)25@Composable26private fun FeedbackBody(fg: Color, vm: SettingsViewModel) {27    val categories = listOf("Bug report", "Feature request", "Question", "Other")28    var category by remember { mutableStateOf(categories[0]) }29    var message by remember { mutableStateOf("") }30    val state by vm.feedbackState.collectAsState()3132    // Reset transient state when the card leaves composition so reopening the33    // card starts fresh rather than on a stale Sent/Error.34    DisposableEffect(Unit) { onDispose { vm.resetFeedback() } }3536    if (state is FeedbackState.Sent) {37        Box(38            Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp)).background(fg.copy(alpha = 0.08f))39                .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(10.dp)).padding(16.dp),40        ) {41            Column {42                Text("Thank you", color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp)43                Text("Your note is in the queue. Someone will process it soon.", color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 12.sp, modifier = Modifier.padding(top = 4.dp))44            }45        }46        return47    }4849    val sending = state is FeedbackState.Sending5051    Text("Help us improve Peach. Every report goes directly to our inbox.", color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 12.sp, lineHeight = 17.sp)52    SubLabel("TYPE", fg)53    FlowRow(horizontalArrangement = Arrangement.spacedBy(6.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {54        categories.forEach { cat -> Pill(cat, category == cat, fg) { if (!sending) category = cat } }55    }56    SubLabel("MESSAGE", fg)57    Box(58        Modifier.fillMaxWidth().height(110.dp).clip(RoundedCornerShape(8.dp))59            .background(Color(0x2E000000)).border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))60            .padding(10.dp),61    ) {62        if (message.isEmpty()) Text("Describe the issue or idea...", color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Body, fontSize = 12.sp)63        BasicTextField(64            value = message, onValueChange = { message = it },65            enabled = !sending,66            textStyle = TextStyle(color = fg, fontFamily = PeachType.Body, fontSize = 12.sp),67            cursorBrush = SolidColor(fg), modifier = Modifier.fillMaxWidth(),68        )69    }70    Spacer(Modifier.height(8.dp))7172    Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) {73        // Send is disabled while sending and while the message is blank; on74        // Error it re-enables to act as a retry.75        val canSend = !sending && message.isNotBlank()76        Box(77            Modifier.clip(RoundedCornerShape(8.dp))78                .border(1.dp, fg.copy(alpha = if (canSend) 0.28f else 0.14f), RoundedCornerShape(8.dp))79                .background(fg.copy(alpha = if (canSend) 0.08f else 0.04f))80                .clickable(enabled = canSend) { vm.sendFeedback(category, message) }81                .padding(horizontal = 12.dp, vertical = 6.dp),82        ) {83            Text(84                if (state is FeedbackState.Error) "Retry" else "Send",85                color = fg.copy(alpha = if (canSend) 1f else 0.4f),86                fontFamily = PeachType.Mono, fontSize = 11.sp,87            )88        }89        if (sending) {90            CircularProgressIndicator(91                color = LocalPeachAccent.current,92                strokeWidth = 2.dp,93                modifier = Modifier.size(16.dp),94            )95        }96    }9798    (state as? FeedbackState.Error)?.let { err ->99        Text(100            err.message,101            modifier = Modifier.padding(top = 8.dp),102            color = com.peachpasswords.ui.theme.PeachColors.Breach,103            fontFamily = PeachType.Mono,104            fontSize = 11.sp,105            lineHeight = 15.sp,106        )107    }108109    Hint("Sent anonymously · no account required", fg)110}