|
| 1 | +package com.pulseloop.ui.screens |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.border |
| 5 | +import androidx.compose.foundation.clickable |
| 6 | +import androidx.compose.foundation.layout.* |
| 7 | +import androidx.compose.foundation.lazy.LazyColumn |
| 8 | +import androidx.compose.foundation.lazy.items |
| 9 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 10 | +import androidx.compose.material.icons.Icons |
| 11 | +import androidx.compose.material.icons.filled.Delete |
| 12 | +import androidx.compose.material3.* |
| 13 | +import androidx.compose.runtime.* |
| 14 | +import androidx.compose.ui.Alignment |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.draw.clip |
| 17 | +import androidx.compose.ui.text.font.FontWeight |
| 18 | +import androidx.compose.ui.text.style.TextOverflow |
| 19 | +import androidx.compose.ui.unit.dp |
| 20 | +import androidx.compose.ui.unit.sp |
| 21 | +import com.pulseloop.data.dao.CoachConversationSummary |
| 22 | +import com.pulseloop.ui.theme.PulseColors |
| 23 | +import java.time.Instant |
| 24 | +import java.time.LocalDate |
| 25 | +import java.time.ZoneId |
| 26 | +import java.time.format.DateTimeFormatter |
| 27 | +import java.time.format.FormatStyle |
| 28 | +import java.util.Locale |
| 29 | + |
| 30 | +/** |
| 31 | + * The coach's past conversations (issue #131): tap one to reopen it, swipe it away to delete it, |
| 32 | + * or delete them all. Every delete asks first — it takes the messages, the tool-call trace and any |
| 33 | + * attached photos with it, and there is no undo. |
| 34 | + */ |
| 35 | +@OptIn(ExperimentalMaterial3Api::class) |
| 36 | +@Composable |
| 37 | +fun CoachHistorySheet( |
| 38 | + conversations: List<CoachConversationSummary>, |
| 39 | + currentId: String?, |
| 40 | + onOpen: (String) -> Unit, |
| 41 | + onDelete: (String) -> Unit, |
| 42 | + onDeleteAll: () -> Unit, |
| 43 | + onDismiss: () -> Unit, |
| 44 | +) { |
| 45 | + val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = false) |
| 46 | + var pendingDelete by remember { mutableStateOf<CoachConversationSummary?>(null) } |
| 47 | + var confirmDeleteAll by remember { mutableStateOf(false) } |
| 48 | + |
| 49 | + ModalBottomSheet(onDismissRequest = onDismiss, sheetState = sheetState, containerColor = PulseColors.background) { |
| 50 | + Column( |
| 51 | + Modifier |
| 52 | + .fillMaxWidth() |
| 53 | + .padding(horizontal = 16.dp) |
| 54 | + .padding(bottom = 24.dp), |
| 55 | + verticalArrangement = Arrangement.spacedBy(12.dp), |
| 56 | + ) { |
| 57 | + Text("Past conversations", fontSize = 17.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.textPrimary) |
| 58 | + if (conversations.isEmpty()) { |
| 59 | + Text( |
| 60 | + "No conversations yet. Ask the coach something and it will show up here.", |
| 61 | + fontSize = 13.sp, color = PulseColors.textMuted, |
| 62 | + ) |
| 63 | + } else { |
| 64 | + Text("Swipe a conversation to delete it.", fontSize = 12.sp, color = PulseColors.textMuted) |
| 65 | + LazyColumn( |
| 66 | + Modifier.weight(1f, fill = false), |
| 67 | + verticalArrangement = Arrangement.spacedBy(8.dp), |
| 68 | + ) { |
| 69 | + items(conversations, key = { it.id }) { convo -> |
| 70 | + HistoryRow( |
| 71 | + convo = convo, |
| 72 | + isCurrent = convo.id == currentId, |
| 73 | + onOpen = { onOpen(convo.id) }, |
| 74 | + onSwipedAway = { pendingDelete = convo }, |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | + TextButton( |
| 79 | + onClick = { confirmDeleteAll = true }, |
| 80 | + modifier = Modifier.align(Alignment.CenterHorizontally), |
| 81 | + ) { Text("Delete all conversations", color = PulseColors.danger) } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + pendingDelete?.let { convo -> |
| 87 | + AlertDialog( |
| 88 | + onDismissRequest = { pendingDelete = null }, |
| 89 | + title = { Text("Delete this conversation?") }, |
| 90 | + text = { Text("“${CoachHistoryText.title(convo.firstQuestion)}” and its replies will be deleted. This can't be undone.") }, |
| 91 | + confirmButton = { |
| 92 | + TextButton(onClick = { onDelete(convo.id); pendingDelete = null }) { |
| 93 | + Text("Delete", color = PulseColors.danger) |
| 94 | + } |
| 95 | + }, |
| 96 | + dismissButton = { TextButton(onClick = { pendingDelete = null }) { Text("Cancel") } }, |
| 97 | + ) |
| 98 | + } |
| 99 | + if (confirmDeleteAll) { |
| 100 | + AlertDialog( |
| 101 | + onDismissRequest = { confirmDeleteAll = false }, |
| 102 | + title = { Text("Delete all conversations?") }, |
| 103 | + text = { Text("All ${conversations.size} conversations and their replies will be deleted. This can't be undone.") }, |
| 104 | + confirmButton = { |
| 105 | + TextButton(onClick = { confirmDeleteAll = false; onDeleteAll() }) { |
| 106 | + Text("Delete all", color = PulseColors.danger) |
| 107 | + } |
| 108 | + }, |
| 109 | + dismissButton = { TextButton(onClick = { confirmDeleteAll = false }) { Text("Cancel") } }, |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +@OptIn(ExperimentalMaterial3Api::class) |
| 115 | +@Composable |
| 116 | +private fun HistoryRow( |
| 117 | + convo: CoachConversationSummary, |
| 118 | + isCurrent: Boolean, |
| 119 | + onOpen: () -> Unit, |
| 120 | + onSwipedAway: () -> Unit, |
| 121 | +) { |
| 122 | + // The swipe only asks; the row snaps back and the dialog decides. Returning false keeps the |
| 123 | + // state from settling on "dismissed", which would leave a cancelled row swiped off screen. |
| 124 | + val dismissState = rememberSwipeToDismissBoxState( |
| 125 | + confirmValueChange = { value -> |
| 126 | + if (value != SwipeToDismissBoxValue.Settled) onSwipedAway() |
| 127 | + false |
| 128 | + }, |
| 129 | + ) |
| 130 | + val shape = RoundedCornerShape(12.dp) |
| 131 | + SwipeToDismissBox( |
| 132 | + state = dismissState, |
| 133 | + backgroundContent = { |
| 134 | + Box( |
| 135 | + Modifier |
| 136 | + .fillMaxSize() |
| 137 | + .clip(shape) |
| 138 | + .background(PulseColors.danger.copy(alpha = 0.25f)) |
| 139 | + .padding(horizontal = 16.dp), |
| 140 | + contentAlignment = Alignment.CenterEnd, |
| 141 | + ) { |
| 142 | + Icon(Icons.Filled.Delete, "Delete", tint = PulseColors.danger) |
| 143 | + } |
| 144 | + }, |
| 145 | + ) { |
| 146 | + Column( |
| 147 | + Modifier |
| 148 | + .fillMaxWidth() |
| 149 | + .clip(shape) |
| 150 | + .background(PulseColors.card) |
| 151 | + .border(1.dp, if (isCurrent) PulseColors.accent else PulseColors.borderSubtle, shape) |
| 152 | + .clickable(onClick = onOpen) |
| 153 | + .padding(horizontal = 14.dp, vertical = 10.dp), |
| 154 | + ) { |
| 155 | + Text( |
| 156 | + CoachHistoryText.title(convo.firstQuestion), |
| 157 | + fontSize = 14.sp, color = PulseColors.textPrimary, |
| 158 | + maxLines = 2, overflow = TextOverflow.Ellipsis, |
| 159 | + ) |
| 160 | + Spacer(Modifier.height(2.dp)) |
| 161 | + Text( |
| 162 | + CoachHistoryText.subtitle(convo, isCurrent), |
| 163 | + fontSize = 12.sp, color = PulseColors.textMuted, |
| 164 | + ) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/** The history row's text, kept out of the composable so it can be unit-tested. */ |
| 170 | +internal object CoachHistoryText { |
| 171 | + |
| 172 | + fun title(firstQuestion: String?): String = |
| 173 | + firstQuestion?.replace(Regex("\\s+"), " ")?.trim()?.takeIf { it.isNotEmpty() } |
| 174 | + ?: "Untitled conversation" |
| 175 | + |
| 176 | + fun subtitle( |
| 177 | + convo: CoachConversationSummary, |
| 178 | + isCurrent: Boolean, |
| 179 | + zone: ZoneId = ZoneId.systemDefault(), |
| 180 | + today: LocalDate = LocalDate.now(zone), |
| 181 | + ): String { |
| 182 | + val parts = mutableListOf(whenText(convo.updatedAt, zone, today)) |
| 183 | + parts += if (convo.messageCount == 1) "1 message" else "${convo.messageCount} messages" |
| 184 | + if (isCurrent) parts += "open now" |
| 185 | + return parts.joinToString(" · ") |
| 186 | + } |
| 187 | + |
| 188 | + fun whenText(epochMs: Long, zone: ZoneId, today: LocalDate): String { |
| 189 | + val at = Instant.ofEpochMilli(epochMs).atZone(zone) |
| 190 | + val day = at.toLocalDate() |
| 191 | + return when { |
| 192 | + day == today -> "Today " + at.format(TIME) |
| 193 | + day == today.minusDays(1) -> "Yesterday " + at.format(TIME) |
| 194 | + day.year == today.year -> at.format(SAME_YEAR) |
| 195 | + else -> at.format(OTHER_YEAR) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private val TIME = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) |
| 200 | + private val SAME_YEAR = DateTimeFormatter.ofPattern("MMM d", Locale.getDefault()) |
| 201 | + private val OTHER_YEAR = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.getDefault()) |
| 202 | +} |
0 commit comments