securemail: drawer folders, chip compose with contact picker, spam routing
- Commit
e8c1fddaf45f296eb23933b0e126d4ca1d1c03dd- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-24 07:23 UTC (1 month ago)
- Parents
ee85bca
Diff
securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt b/securemail/src/main/java/com/vayunmathur/securemail/Navigation.ktindex edf935400b..0b61d8eb02 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt@@ -5,24 +5,35 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.LocalContext import androidx.fragment.app.FragmentActivity+import com.vayunmathur.library.ui.DrawerValue+import com.vayunmathur.library.ui.ModalNavigationDrawer+import com.vayunmathur.library.ui.rememberDrawerState import com.vayunmathur.library.util.MainNavigation import com.vayunmathur.library.util.rememberNavBackStack+import com.vayunmathur.securemail.data.MailFolder+import com.vayunmathur.securemail.domain.mail.AddressValidator import com.vayunmathur.securemail.platform.MailViewModel import com.vayunmathur.securemail.ui.ComposeScreen import com.vayunmathur.securemail.ui.InboxScreen import com.vayunmathur.securemail.ui.LockScreen+import com.vayunmathur.securemail.ui.MailDrawerContent import com.vayunmathur.securemail.ui.OnboardingScreen import com.vayunmathur.securemail.ui.OutboxScreen import com.vayunmathur.securemail.ui.ReaderScreen import com.vayunmathur.securemail.ui.SettingsScreen+import kotlinx.coroutines.launch @Composable fun Navigation(viewModel: MailViewModel, initialRoute: Route? = null) { val state = viewModel.state val context = LocalContext.current- val messages by viewModel.messages.collectAsState(emptyList())+ val scope = rememberCoroutineScope()+ val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)+ val folderMessages by viewModel.folderMessages.collectAsState(emptyList())+ val allMessages by viewModel.messages.collectAsState(emptyList()) val outbox by viewModel.outboxCount.collectAsState(0) val outboxMessages by viewModel.outboxMessages.collectAsState(emptyList()) // The stack follows the account flag, not navigation events: once an address@@ -49,88 +60,144 @@ fun Navigation(viewModel: MailViewModel, initialRoute: Route? = null) { if (initialRoute != null) backStack.add(initialRoute) } - MainNavigation(backStack) {- entry<Route.Onboarding> {- OnboardingScreen(- state = state,- onClaim = viewModel::claimAddress,- onDismissMessages = viewModel::dismissMessages,- )- }- entry<Route.Lock> {- LockScreen(- state = state,- onUnlock = {- (context as? FragmentActivity)?.let { viewModel.unlock(it) }+ fun closeDrawerThen(action: () -> Unit) {+ scope.launch { drawerState.close() }+ action()+ }++ ModalNavigationDrawer(+ drawerState = drawerState,+ drawerContent = {+ MailDrawerContent(+ selectedFolder = viewModel.selectedFolder,+ showingOutbox = backStack.last() == Route.Outbox,+ outboxCount = outbox,+ address = state.address,+ onSelectFolder = { folder ->+ closeDrawerThen {+ viewModel.selectFolder(folder)+ backStack.reset(Route.Inbox)+ }+ },+ onSelectOutbox = {+ closeDrawerThen { backStack.add(Route.Outbox) } },- onDismissMessages = viewModel::dismissMessages,- )- }- entry<Route.Inbox> {- InboxScreen(- state = state,- onOpen = { backStack.add(Route.Reader(it)) },- onCompose = { backStack.add(Route.Composer()) },- onOutbox = { backStack.add(Route.Outbox) },- onSettings = { backStack.add(Route.Settings) },- onRefresh = viewModel::refresh,- onConsumeMessage = viewModel::dismissMessages,- messages = messages,- outbox = outbox,- )- }- entry<Route.Composer> { route ->- ComposeScreen(- state = state,- initialTo = route.to,- initialSubject = route.subject,- initialBody = route.body,- onSend = viewModel::send,- onBack = { backStack.pop() },- onSent = { backStack.pop() },- onConsumeMessage = viewModel::dismissMessages, )- }- entry<Route.Reader> { route ->- val msg = remember(route.messageId, messages) {- messages.firstOrNull { it.id == route.messageId }+ },+ ) {+ MainNavigation(backStack) {+ entry<Route.Onboarding> {+ OnboardingScreen(+ state = state,+ onClaim = viewModel::claimAddress,+ onDismissMessages = viewModel::dismissMessages,+ )+ }+ entry<Route.Lock> {+ LockScreen(+ state = state,+ onUnlock = {+ (context as? FragmentActivity)?.let { viewModel.unlock(it) }+ },+ onDismissMessages = viewModel::dismissMessages,+ ) }- LaunchedEffect(route.messageId) {- viewModel.decryptStored(route.messageId)- viewModel.markRead(route.messageId)+ entry<Route.Inbox> {+ InboxScreen(+ state = state,+ folder = viewModel.selectedFolder,+ onOpenDrawer = { scope.launch { drawerState.open() } },+ onOpen = { backStack.add(Route.Reader(it)) },+ onCompose = { backStack.add(Route.Composer()) },+ onSettings = { backStack.add(Route.Settings) },+ onRefresh = viewModel::refresh,+ onConsumeMessage = viewModel::dismissMessages,+ onTrash = viewModel::moveToTrash,+ onRestore = viewModel::restoreFromTrash,+ onEmptyTrash = viewModel::emptyTrash,+ messages = folderMessages,+ ) }- ReaderScreen(- state = state,- messageId = route.messageId,- onReply = { m ->- backStack.add(- Route.Composer(- to = m.from,- subject = "Re: ${m.decryptedSubject ?: ""}",- inReplyTo = m.id,- ),+ entry<Route.Composer> { route ->+ LaunchedEffect(route.to, route.subject, route.body) {+ viewModel.seedDraft(+ to = AddressValidator.parseRecipients(route.to), )- },- onBack = { backStack.pop() },- message = msg,- )- }- entry<Route.Outbox> {- OutboxScreen(- state = state,- onRetry = viewModel::retryOutbox,- onBack = { backStack.pop() },- pending = outboxMessages,- )- }- entry<Route.Settings> {- SettingsScreen(- state = state,- onExportPublicKey = viewModel::exportPublicKey,- onForget = viewModel::forgetIdentity,- onBack = { backStack.pop() },- onDismissMessages = viewModel::dismissMessages,- )+ }+ ComposeScreen(+ state = state,+ draftTo = viewModel.draftTo,+ draftCc = viewModel.draftCc,+ draftBcc = viewModel.draftBcc,+ initialSubject = route.subject,+ initialBody = route.body,+ onAddRecipient = viewModel::addDraftRecipient,+ onRemoveRecipient = viewModel::removeDraftRecipient,+ onSend = { to, cc, bcc, subject, body, inReplyTo ->+ viewModel.send(+ to = to,+ cc = cc,+ bcc = bcc,+ subject = subject,+ body = body,+ inReplyTo = inReplyTo,+ onDone = { sent ->+ if (sent) {+ viewModel.clearDraft()+ backStack.pop()+ }+ },+ )+ },+ onBack = { backStack.pop() },+ onConsumeMessage = viewModel::dismissMessages,+ )+ }+ entry<Route.Reader> { route ->+ val msg = remember(route.messageId, allMessages, folderMessages) {+ folderMessages.firstOrNull { it.id == route.messageId }+ ?: allMessages.firstOrNull { it.id == route.messageId }+ }+ LaunchedEffect(route.messageId) {+ viewModel.decryptStored(route.messageId)+ viewModel.markRead(route.messageId)+ }+ ReaderScreen(+ state = state,+ messageId = route.messageId,+ onReply = { m ->+ backStack.add(+ Route.Composer(+ to = m.from,+ subject = "Re: ${m.decryptedSubject ?: ""}",+ inReplyTo = m.id,+ ),+ )+ },+ onBack = { backStack.pop() },+ message = msg,+ )+ }+ entry<Route.Outbox> {+ OutboxScreen(+ state = state,+ onRetry = viewModel::retryOutbox,+ onBack = { backStack.pop() },+ onOpenDrawer = { scope.launch { drawerState.open() } },+ onCompose = { backStack.add(Route.Composer()) },+ onSettings = { backStack.add(Route.Settings) },+ pending = outboxMessages,+ )+ }+ entry<Route.Settings> {+ SettingsScreen(+ state = state,+ onExportPublicKey = viewModel::exportPublicKey,+ onForget = viewModel::forgetIdentity,+ onBack = { backStack.pop() },+ onDismissMessages = viewModel::dismissMessages,+ )+ } } } }
securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.ktindex 513925997c..ec93079198 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt@@ -89,6 +89,58 @@ class MailViewModel(app: Application) : AndroidViewModel(app) { repo.messageDao().observeFolder(folder) } + // ------------------------------------------------------------------+ // Compose draft: recipients survive picker navigation (ViewModel, not+ // local remember — the form is disposed when the picker opens).+ // ------------------------------------------------------------------++ /** Draft recipients (To) for the compose screen. */+ var draftTo by mutableStateOf(listOf<String>())+ private set++ /** Draft recipients (Cc) for the compose screen. */+ var draftCc by mutableStateOf(listOf<String>())+ private set++ /** Draft recipients (Bcc) for the compose screen. */+ var draftBcc by mutableStateOf(listOf<String>())+ private set++ /** Seeds the draft (reply/forward prefill, share-intent entry). */+ fun seedDraft(to: List<String> = emptyList()) {+ draftTo = to.filter { it.isNotBlank() }.distinct()+ draftCc = emptyList()+ draftBcc = emptyList()+ }++ /** Clears the draft after a successful send. */+ fun clearDraft() {+ draftTo = emptyList()+ draftCc = emptyList()+ draftBcc = emptyList()+ }++ /** Adds a recipient chip to To/Cc/Bcc (0/1/2); returns false when invalid/duplicate. */+ fun addDraftRecipient(target: Int, address: String): Boolean {+ val clean = address.trim().lowercase()+ if (!AddressValidator.isValidAddress(clean)) return false+ when (target) {+ 1 -> if (draftCc.any { it.equals(clean, ignoreCase = true) }) return false else draftCc += clean+ 2 -> if (draftBcc.any { it.equals(clean, ignoreCase = true) }) return false else draftBcc += clean+ else -> if (draftTo.any { it.equals(clean, ignoreCase = true) }) return false else draftTo += clean+ }+ return true+ }++ /** Removes a recipient chip from To/Cc/Bcc (0/1/2). */+ fun removeDraftRecipient(target: Int, address: String) {+ when (target) {+ 1 -> draftCc -= address+ 2 -> draftBcc -= address+ else -> draftTo -= address+ }+ }+ /** Moves a message to Trash (local vault only). */ fun moveToTrash(id: String) { viewModelScope.launch {@@ -435,21 +487,27 @@ class MailViewModel(app: Application) : AndroidViewModel(app) { * Sends a message: encrypts per-recipient on device to every WKD key found, * publishes over the live socket when up, else queues to the outbox worker. * Recipients without a key receive the message unencrypted (badged `plain`- * on receipt) — there is no opt-in; no WKD key simply means no encryption.+ * on receipt) - there is no opt-in; no WKD key simply means no encryption. * Changed/lost keys still abort the send for explicit acknowledgement.+ * Cc/Bcc are merged into the recipient set (Bcc is best-effort on the+ * relay: each recipient gets an individually-encrypted blob, so no+ * recipient sees the others' addresses beyond their own envelope). */ fun send( to: List<String>,+ cc: List<String> = emptyList(),+ bcc: List<String> = emptyList(), subject: String, body: String, inReplyTo: String? = null, onDone: (sent: Boolean) -> Unit = {}, ) {+ val allTo = (to + cc + bcc).distinct() state = state.copy(busy = true, error = null) viewModelScope.launch { val outcome = withContext(Dispatchers.IO) { runCatching {- val trust = resolveTrust(to)+ val trust = resolveTrust(allTo) when (val decision = KeyLookupPolicy.decide(trust)) { is KeyLookupPolicy.SendDecision.NeedsKeyChangeAck -> error("Key changed for ${decision.changed.first().address}. Review in Settings first.")@@ -489,7 +547,7 @@ class MailViewModel(app: Application) : AndroidViewModel(app) { } } // Sent-folder copy: plaintext (it is our own composition).- storeSent(to, subject, body)+ storeSent(allTo, subject, body) if (queued > 0) "Queued $queued message(s) — retry from Outbox when online." else "Sent." } }
securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt b/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.ktindex ecc34a318e..bef9c0b94f 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt@@ -1,115 +1,172 @@ package com.vayunmathur.securemail.ui +import android.app.Activity+import android.content.Intent+import android.provider.ContactsContract+import androidx.activity.compose.rememberLauncherForActivityResult+import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.text.KeyboardActions+import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier-import androidx.compose.foundation.text.KeyboardOptions+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.DesktopMaxWidthContainer import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedTextField import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.library.ui.rememberMessenger-import com.vayunmathur.securemail.domain.mail.AddressValidator import com.vayunmathur.securemail.platform.MailUiState import kotlinx.coroutines.launch /**- * Compose: encrypts to every recipient with a WKD key; recipients without- * one receive the message unencrypted (badged `plain` on receipt). There is- * no opt-in toggle — no WKD key simply means no encryption for that- * recipient. Changed/lost keys still hard-stop the send.+ * Compose: no top app bar (full-bleed form); recipients as Enter-to-add+ * chips (To/Cc/Bcc) with a contacts-picker `+`; subject single-line+ * (`Next`), body multi-line (`Done`). Draft recipients live in the ViewModel+ * so picker navigation doesn't wipe them. */ @Composable fun ComposeScreen( state: MailUiState,- initialTo: String,+ draftTo: List<String>,+ draftCc: List<String>,+ draftBcc: List<String>, initialSubject: String, initialBody: String,- onSend: (List<String>, String, String, String?, (Boolean) -> Unit) -> Unit,+ onAddRecipient: (Int, String) -> Boolean,+ onRemoveRecipient: (Int, String) -> Unit,+ onSend: (List<String>, List<String>, List<String>, String, String, String?) -> Unit, onBack: () -> Unit,- onSent: () -> Unit = {}, onConsumeMessage: () -> Unit = {}, ) {+ val context = LocalContext.current val messenger = rememberMessenger() ConsumeMessage(state.message, messenger, onConsumeMessage)- var to by remember { mutableStateOf(initialTo) } var subject by remember { mutableStateOf(initialSubject) } var body by remember { mutableStateOf(initialBody) } val scope = rememberCoroutineScope()- // Trust resolution needs the ViewModel; the parent passes a lambda that- // already gates. Here we only pre-validate syntax inline.- val recipients = remember(to) { AddressValidator.parseRecipients(to) }- val invalid = remember(recipients) { recipients.filterNot { AddressValidator.isValidAddress(it) } }- AppScaffold(title = "Compose", scrollBehavior = appBarScrollBehavior()) { padding ->- DesktopMaxWidthContainer(Modifier.padding(padding)) {- Column(- modifier = Modifier- .fillMaxWidth()- .padding(16.dp)- .verticalScroll(rememberScrollState()),- verticalArrangement = Arrangement.spacedBy(12.dp),- ) {- state.error?.let { Text(it, color = MaterialTheme.colorScheme.error) }- OutlinedTextField(- value = to,- onValueChange = { to = it },- label = { Text("To (comma separated)") },- modifier = Modifier.fillMaxWidth(),- keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),- )- if (invalid.isNotEmpty()) {- Text(- "Check: ${invalid.joinToString()}",- color = MaterialTheme.colorScheme.error,- style = MaterialTheme.typography.labelMedium,- )- }- OutlinedTextField(- value = subject,- onValueChange = { subject = it },- label = { Text("Subject") },- modifier = Modifier.fillMaxWidth(),- singleLine = true,- keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.EmailSubject),- )- OutlinedTextField(- value = body,- onValueChange = { body = it },- label = { Text("Message") },- modifier = Modifier.fillMaxWidth(),- minLines = 8,- )- Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {- Button(- onClick = {- scope.launch {- onSend(recipients, subject, body, null) { sent ->- if (sent) onSent()- }- }- },- enabled = !state.busy && recipients.isNotEmpty() && invalid.isEmpty() && body.isNotBlank(),- modifier = Modifier.weight(1f),- ) { Text(if (state.busy) "Sending…" else "Send") }- Button(onClick = onBack, modifier = Modifier.weight(1f)) { Text("Cancel") }+ var pickTarget by remember { mutableStateOf(0) }+ var pickTick by remember { mutableStateOf(0) }+ val contactPicker = rememberLauncherForActivityResult(+ ActivityResultContracts.StartActivityForResult(),+ ) { result ->+ if (result.resultCode == Activity.RESULT_OK) {+ val email = result.data?.data?.let { contactEmail(context, it) }+ if (!email.isNullOrBlank()) {+ if (!onAddRecipient(pickTarget, email)) {+ scope.launch { messenger.show("Invalid or duplicate address.") } } } } }+ LaunchedEffect(pickTick) {+ if (pickTick > 0) {+ contactPicker.launch(+ Intent(+ Intent.ACTION_PICK,+ ContactsContract.CommonDataKinds.Email.CONTENT_URI,+ ),+ )+ }+ }+ val allRecipients = draftTo + draftCc + draftBcc+ DesktopMaxWidthContainer(Modifier.fillMaxSize()) {+ Column(+ modifier = Modifier+ .fillMaxWidth()+ .padding(16.dp)+ .verticalScroll(rememberScrollState()),+ verticalArrangement = Arrangement.spacedBy(12.dp),+ ) {+ state.error?.let { Text(it, color = MaterialTheme.colorScheme.error) }+ RecipientChipField(+ label = "To",+ recipients = draftTo,+ onAdd = { onAddRecipient(0, it) },+ onRemove = { onRemoveRecipient(0, it) },+ onPickContact = { pickTarget = 0; pickTick++ },+ onInvalid = { scope.launch { messenger.show("Enter a valid email address.") } },+ )+ RecipientChipField(+ label = "Cc",+ recipients = draftCc,+ onAdd = { onAddRecipient(1, it) },+ onRemove = { onRemoveRecipient(1, it) },+ onPickContact = { pickTarget = 1; pickTick++ },+ onInvalid = { scope.launch { messenger.show("Enter a valid email address.") } },+ )+ RecipientChipField(+ label = "Bcc",+ recipients = draftBcc,+ onAdd = { onAddRecipient(2, it) },+ onRemove = { onRemoveRecipient(2, it) },+ onPickContact = { pickTarget = 2; pickTick++ },+ onInvalid = { scope.launch { messenger.show("Enter a valid email address.") } },+ )+ OutlinedTextField(+ value = subject,+ onValueChange = { subject = it },+ label = { Text("Subject") },+ modifier = Modifier.fillMaxWidth(),+ singleLine = true,+ keyboardOptions = KeyboardOptions(+ keyboardType = KeyboardType.Text,+ imeAction = ImeAction.Next,+ ),+ )+ OutlinedTextField(+ value = body,+ onValueChange = { body = it },+ label = { Text("Message") },+ modifier = Modifier.fillMaxWidth(),+ minLines = 8,+ keyboardOptions = KeyboardOptions(+ keyboardType = KeyboardType.Text,+ imeAction = ImeAction.Done,+ ),+ keyboardActions = KeyboardActions(onDone = { /* keep focus; Send submits */ }),+ )+ Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {+ Button(+ onClick = {+ scope.launch {+ onSend(draftTo, draftCc, draftBcc, subject, body, null)+ }+ },+ enabled = !state.busy && allRecipients.isNotEmpty() && body.isNotBlank(),+ modifier = Modifier.weight(1f),+ ) { Text(if (state.busy) "Sending…" else "Send") }+ Button(onClick = onBack, modifier = Modifier.weight(1f)) { Text("Cancel") }+ }+ }+ } }++/** Reads the email address from a contact-picker result URI (per-item grant, no permission needed). */+internal fun contactEmail(context: android.content.Context, uri: android.net.Uri): String? =+ runCatching {+ context.contentResolver.query(+ uri,+ arrayOf(ContactsContract.CommonDataKinds.Email.ADDRESS),+ null,+ null,+ null,+ )?.use { if (it.moveToFirst()) it.getString(0) else null }+ }.getOrNull()
securemail/src/main/java/com/vayunmathur/securemail/ui/InboxScreen.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/ui/InboxScreen.kt b/securemail/src/main/java/com/vayunmathur/securemail/ui/InboxScreen.ktindex 4b3602b3f0..e6a43c6197 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/ui/InboxScreen.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/ui/InboxScreen.kt@@ -9,40 +9,61 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.runtime.Composable-import androidx.compose.runtime.collectAsState-import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.DesktopMaxWidthContainer+import com.vayunmathur.library.ui.FloatingActionButton+import com.vayunmathur.library.ui.IconAdd+import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.IconDelete+import com.vayunmathur.library.ui.IconMenu+import com.vayunmathur.library.ui.IconSettings import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.library.ui.rememberMessenger+import com.vayunmathur.securemail.data.MailFolder import com.vayunmathur.securemail.data.StoredMessage import com.vayunmathur.securemail.platform.MailUiState import java.text.DateFormat import java.util.Date -/** Inbox: vault list of stored envelopes; bodies decrypt on open. */+/**+ * Folder list: renders the selected vault folder (Inbox/Sent/Spam/Trash).+ * The drawer owns navigation; this screen owns the menu button, settings+ * gear, compose FAB, and per-message trash/restore.+ */ @Composable fun InboxScreen( state: MailUiState,+ folder: MailFolder,+ onOpenDrawer: () -> Unit, onOpen: (String) -> Unit, onCompose: () -> Unit,- onOutbox: () -> Unit, onSettings: () -> Unit, onRefresh: () -> Unit, onConsumeMessage: () -> Unit,+ onTrash: (String) -> Unit,+ onRestore: (String) -> Unit,+ onEmptyTrash: () -> Unit, messages: List<StoredMessage> = emptyList(),- outbox: Int = 0, ) { val messenger = rememberMessenger() ConsumeMessage(state.message, messenger, onConsumeMessage)+ val title = when (folder) {+ MailFolder.INBOX -> "Inbox"+ MailFolder.SENT -> "Sent"+ MailFolder.SPAM -> "Spam"+ MailFolder.TRASH -> "Trash"+ } AppScaffold(- title = state.address.ifBlank { "SecureMail" },+ title = title,+ navigationIcon = { IconButton(onClick = onOpenDrawer) { IconMenu() } },+ actions = { IconButton(onClick = onSettings) { IconSettings() } },+ floatingActionButton = { FloatingActionButton(onClick = onCompose) { IconAdd() } }, scrollBehavior = appBarScrollBehavior(), ) { padding -> DesktopMaxWidthContainer(Modifier.padding(padding)) {@@ -56,15 +77,20 @@ fun InboxScreen( style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.secondary, )- Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {- Button(onClick = onCompose, modifier = Modifier.weight(1f)) { Text("Compose") }- Button(onClick = onOutbox, modifier = Modifier.weight(1f)) { Text("Outbox ($outbox)") }- Button(onClick = onSettings, modifier = Modifier.weight(1f)) { Text("Settings") }+ if (folder == MailFolder.TRASH && messages.isNotEmpty()) {+ Button(onClick = onEmptyTrash, modifier = Modifier.fillMaxWidth()) {+ Text("Empty trash")+ } } if (messages.isEmpty()) { Card(modifier = Modifier.fillMaxWidth()) { Text(- "No mail yet. Share your address to receive encrypted mail.",+ when (folder) {+ MailFolder.INBOX -> "No mail yet. Share your address to receive encrypted mail."+ MailFolder.SENT -> "Nothing sent yet."+ MailFolder.SPAM -> "No spam. Verification failures land here."+ MailFolder.TRASH -> "Trash is empty."+ }, modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.bodyMedium, )@@ -72,7 +98,13 @@ fun InboxScreen( } else { LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) { items(messages, key = { it.id }) { msg ->- MessageRow(msg, onOpen = { onOpen(msg.id) })+ MessageRow(+ msg = msg,+ onOpen = { onOpen(msg.id) },+ inTrash = folder == MailFolder.TRASH,+ onTrash = { onTrash(msg.id) },+ onRestore = { onRestore(msg.id) },+ ) } } }@@ -82,7 +114,13 @@ fun InboxScreen( } @Composable-private fun MessageRow(msg: StoredMessage, onOpen: () -> Unit) {+private fun MessageRow(+ msg: StoredMessage,+ onOpen: () -> Unit,+ inTrash: Boolean,+ onTrash: () -> Unit,+ onRestore: () -> Unit,+) { Card(modifier = Modifier.fillMaxWidth().clickable(onClick = onOpen)) { Column( modifier = Modifier.padding(12.dp).fillMaxWidth(),@@ -124,6 +162,13 @@ private fun MessageRow(msg: StoredMessage, onOpen: () -> Unit) { style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.secondary, )+ Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {+ if (inTrash) {+ Button(onClick = onRestore) { Text("Restore") }+ } else {+ IconButton(onClick = onTrash) { IconDelete() }+ }+ } } } }
securemail/src/main/java/com/vayunmathur/securemail/ui/MailDrawer.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/ui/MailDrawer.kt b/securemail/src/main/java/com/vayunmathur/securemail/ui/MailDrawer.ktnew file mode 100644index 0000000000..3decc2b9f8--- /dev/null+++ b/securemail/src/main/java/com/vayunmathur/securemail/ui/MailDrawer.kt@@ -0,0 +1,86 @@+package com.vayunmathur.securemail.ui++import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxHeight+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.verticalScroll+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.IconDelete+import com.vayunmathur.library.ui.IconInbox+import com.vayunmathur.library.ui.IconMail+import com.vayunmathur.library.ui.IconSend+import com.vayunmathur.library.ui.IconWarning+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.ModalDrawerSheet+import com.vayunmathur.library.ui.NavigationDrawerItem+import com.vayunmathur.library.ui.Text+import com.vayunmathur.securemail.data.MailFolder++/**+ * Folder drawer: fixed Inbox / Outbox / Sent / Spam / Trash (no accounts —+ * SecureMail is single-identity). Selection drives `selectedFolder` in the+ * ViewModel; Outbox renders the queue, the rest filter the vault.+ */+@Composable+fun MailDrawerContent(+ selectedFolder: MailFolder,+ showingOutbox: Boolean,+ outboxCount: Int,+ address: String,+ onSelectFolder: (MailFolder) -> Unit,+ onSelectOutbox: () -> Unit,+) {+ ModalDrawerSheet {+ Column(+ modifier = Modifier.fillMaxHeight().verticalScroll(rememberScrollState()),+ ) {+ Text(+ "SecureMail",+ style = MaterialTheme.typography.titleLarge,+ modifier = Modifier.padding(horizontal = 28.dp, vertical = 20.dp),+ )+ Text(+ address,+ style = MaterialTheme.typography.labelMedium,+ color = MaterialTheme.colorScheme.secondary,+ modifier = Modifier.padding(horizontal = 28.dp),+ )+ Spacer(Modifier.height(12.dp))+ NavigationDrawerItem(+ label = { Text("Inbox") },+ icon = { IconInbox() },+ selected = !showingOutbox && selectedFolder == MailFolder.INBOX,+ onClick = { onSelectFolder(MailFolder.INBOX) },+ )+ NavigationDrawerItem(+ label = { Text(if (outboxCount > 0) "Outbox ($outboxCount)" else "Outbox") },+ icon = { IconMail() },+ selected = showingOutbox,+ onClick = onSelectOutbox,+ )+ NavigationDrawerItem(+ label = { Text("Sent") },+ icon = { IconSend() },+ selected = !showingOutbox && selectedFolder == MailFolder.SENT,+ onClick = { onSelectFolder(MailFolder.SENT) },+ )+ NavigationDrawerItem(+ label = { Text("Spam") },+ icon = { IconWarning() },+ selected = !showingOutbox && selectedFolder == MailFolder.SPAM,+ onClick = { onSelectFolder(MailFolder.SPAM) },+ )+ NavigationDrawerItem(+ label = { Text("Trash") },+ icon = { IconDelete() },+ selected = !showingOutbox && selectedFolder == MailFolder.TRASH,+ onClick = { onSelectFolder(MailFolder.TRASH) },+ )+ }+ }+}
securemail/src/main/java/com/vayunmathur/securemail/ui/OutboxScreen.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/ui/OutboxScreen.kt b/securemail/src/main/java/com/vayunmathur/securemail/ui/OutboxScreen.ktindex a88687b954..affdb23aa8 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/ui/OutboxScreen.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/ui/OutboxScreen.kt@@ -15,10 +15,14 @@ import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.DesktopMaxWidthContainer+import com.vayunmathur.library.ui.FloatingActionButton+import com.vayunmathur.library.ui.IconAdd+import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.IconMenu+import com.vayunmathur.library.ui.IconSettings import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior-import com.vayunmathur.securemail.data.MailRepository import com.vayunmathur.securemail.data.OutboxMessage import com.vayunmathur.securemail.platform.MailUiState @@ -28,9 +32,18 @@ fun OutboxScreen( state: MailUiState, onRetry: () -> Unit, onBack: () -> Unit,+ onOpenDrawer: () -> Unit = {},+ onCompose: () -> Unit = {},+ onSettings: () -> Unit = {}, pending: List<OutboxMessage> = emptyList(), ) {- AppScaffold(title = "Outbox", scrollBehavior = appBarScrollBehavior()) { padding ->+ AppScaffold(+ title = "Outbox",+ navigationIcon = { IconButton(onClick = onOpenDrawer) { IconMenu() } },+ actions = { IconButton(onClick = onSettings) { IconSettings() } },+ floatingActionButton = { FloatingActionButton(onClick = onCompose) { IconAdd() } },+ scrollBehavior = appBarScrollBehavior(),+ ) { padding -> DesktopMaxWidthContainer(Modifier.padding(padding)) { Column( modifier = Modifier.fillMaxWidth().padding(16.dp),
securemail/src/main/java/com/vayunmathur/securemail/ui/RecipientChipField.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/ui/RecipientChipField.kt b/securemail/src/main/java/com/vayunmathur/securemail/ui/RecipientChipField.ktnew file mode 100644index 0000000000..5a999217f9--- /dev/null+++ b/securemail/src/main/java/com/vayunmathur/securemail/ui/RecipientChipField.kt@@ -0,0 +1,100 @@+package com.vayunmathur.securemail.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.FlowRow+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.text.KeyboardActions+import androidx.compose.foundation.text.KeyboardOptions+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.text.input.ImeAction+import androidx.compose.ui.text.input.KeyboardType+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.Card+import com.vayunmathur.library.ui.IconAdd+import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.IconRemoveCircle+import com.vayunmathur.library.ui.InputChip+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.OutlinedTextField+import com.vayunmathur.library.ui.Text++/**+ * Recipient chip field (shared by To/Cc/Bcc): type an address + Enter (Done)+ * to add it as a chip; tap a chip's ✕ to remove; `+` opens the contacts+ * picker. Invalid addresses surface [onInvalid] (messenger) instead of+ * adding. Mirrors the `MultiCategoryPicker` chip rendering+ * (`InputChip + IconRemoveCircle`) with an editable field.+ */+@Composable+fun RecipientChipField(+ label: String,+ recipients: List<String>,+ onAdd: (String) -> Boolean,+ onRemove: (String) -> Unit,+ onPickContact: () -> Unit,+ onInvalid: () -> Unit,+) {+ var text by remember { mutableStateOf("") }+ fun submit() {+ val clean = text.trim()+ if (clean.isEmpty()) return+ if (onAdd(clean)) {+ text = ""+ } else {+ onInvalid()+ }+ }+ Card(modifier = Modifier.fillMaxWidth()) {+ Column(+ modifier = Modifier.padding(12.dp).fillMaxWidth(),+ verticalArrangement = Arrangement.spacedBy(8.dp),+ ) {+ Text(label, style = MaterialTheme.typography.labelMedium)+ if (recipients.isNotEmpty()) {+ FlowRow(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(8.dp),+ verticalArrangement = Arrangement.spacedBy(8.dp),+ ) {+ recipients.forEach { address ->+ InputChip(+ selected = false,+ onClick = { onRemove(address) },+ label = { Text(address) },+ trailingIcon = { IconRemoveCircle(Modifier.size(18.dp)) },+ )+ }+ }+ }+ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(8.dp),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ OutlinedTextField(+ value = text,+ onValueChange = { text = it },+ label = { Text("Type address, Enter to add") },+ modifier = Modifier.weight(1f),+ singleLine = true,+ keyboardOptions = KeyboardOptions(+ keyboardType = KeyboardType.Email,+ imeAction = ImeAction.Done,+ ),+ keyboardActions = KeyboardActions(onDone = { submit() }),+ )+ IconButton(onClick = onPickContact) { IconAdd() }+ }+ }+ }+}
securemail/src/screenshotTest/kotlin/com/vayunmathur/securemail/ui/MetadataPreviews.kt
diff --git a/securemail/src/screenshotTest/kotlin/com/vayunmathur/securemail/ui/MetadataPreviews.kt b/securemail/src/screenshotTest/kotlin/com/vayunmathur/securemail/ui/MetadataPreviews.ktindex 98b1ae407a..4dfaa9e0e5 100644--- a/securemail/src/screenshotTest/kotlin/com/vayunmathur/securemail/ui/MetadataPreviews.kt+++ b/securemail/src/screenshotTest/kotlin/com/vayunmathur/securemail/ui/MetadataPreviews.kt@@ -44,11 +44,16 @@ class MetadataPreviews { address = "[email protected]", socketConnected = true, ),+ folder = com.vayunmathur.securemail.data.MailFolder.INBOX,+ onOpenDrawer = {}, onOpen = {}, onCompose = {},- onOutbox = {}, onSettings = {}, onRefresh = {},+ onConsumeMessage = {},+ onTrash = {},+ onRestore = {},+ onEmptyTrash = {}, messages = listOf( com.vayunmathur.securemail.data.StoredMessage( id = "m_1",@@ -70,7 +75,6 @@ class MetadataPreviews { blobBase64 = "e30=", ), ),- outbox = 1, ) } }@@ -82,10 +86,14 @@ class MetadataPreviews { DynamicTheme(darkTheme = true) { ComposeScreen( state = MailUiState(address = "[email protected]"),- initialTo = "[email protected]",+ draftTo = listOf("[email protected]"),+ draftCc = emptyList(),+ draftBcc = emptyList(), initialSubject = "Hello from SecureMail", initialBody = "This message is end-to-end encrypted with PGP.",- onSend = { _, _, _, _, _ -> },+ onAddRecipient = { _, _ -> true },+ onRemoveRecipient = { _, _ -> },+ onSend = { _, _, _, _, _, _ -> }, onBack = {}, ) }
securemail/src/test/java/com/vayunmathur/securemail/network/MailSocketTest.kt
diff --git a/securemail/src/test/java/com/vayunmathur/securemail/network/MailSocketTest.kt b/securemail/src/test/java/com/vayunmathur/securemail/network/MailSocketTest.ktindex eecf10d341..4c94b37062 100644--- a/securemail/src/test/java/com/vayunmathur/securemail/network/MailSocketTest.kt+++ b/securemail/src/test/java/com/vayunmathur/securemail/network/MailSocketTest.kt@@ -47,10 +47,41 @@ class MailSocketTest { raw.copyInto(buf, 6 + idBytes.size) val parsed = MailSocket.parseMsg(buf)!! assertEquals(id, parsed.first)- assertEquals(false, parsed.second)+ assertEquals(MailSocket.MsgFlags(unencrypted = false, spam = false), parsed.second) assertContentEquals(raw, parsed.third) } + @Test+ fun parseMsgSpamFlag() {+ val id = "spam-1"+ val idBytes = id.toByteArray(Charsets.UTF_8)+ val raw = byteArrayOf(1, 2, 3)+ val buf = ByteArray(1 + 1 + 4 + idBytes.size + raw.size)+ buf[0] = MailSocket.OP_MSG+ buf[1] = MailSocket.FLAG_SPAM+ buf[2] = 0; buf[3] = 0; buf[4] = 0; buf[5] = idBytes.size.toByte()+ idBytes.copyInto(buf, 6)+ raw.copyInto(buf, 6 + idBytes.size)+ val parsed = MailSocket.parseMsg(buf)!!+ assertEquals(id, parsed.first)+ // Spam bit set, plaintext bit clear → encrypted spam.+ assertEquals(MailSocket.MsgFlags(unencrypted = false, spam = true), parsed.second)+ assertContentEquals(raw, parsed.third)+ }++ @Test+ fun parseMsgPlainAndSpamBits() {+ val id = "both-1"+ val idBytes = id.toByteArray(Charsets.UTF_8)+ val buf = ByteArray(1 + 1 + 4 + idBytes.size)+ buf[0] = MailSocket.OP_MSG+ buf[1] = (MailSocket.FLAG_PLAIN.toInt() or MailSocket.FLAG_SPAM.toInt()).toByte()+ buf[2] = 0; buf[3] = 0; buf[4] = 0; buf[5] = idBytes.size.toByte()+ idBytes.copyInto(buf, 6)+ val parsed = MailSocket.parseMsg(buf)!!+ assertEquals(MailSocket.MsgFlags(unencrypted = true, spam = true), parsed.second)+ }+ @Test fun parseMsgRejectsTruncated() { assertNull(MailSocket.parseMsg(byteArrayOf(MailSocket.OP_MSG)))