Modern Apps git

← Modern-Apps.git

email: migrate to canonical structure

Commit
11aeb44f4258810d4c69a2ece75bb7055475f373
Author
vayun-mathur <[email protected]>
When
2026-08-15 20:03 UTC (3 months ago)
Parents
37707cf

Diff

email/src/main/AndroidManifest.xml
diff --git a/email/src/main/AndroidManifest.xml b/email/src/main/AndroidManifest.xmlindex 82b0d74297..6180180172 100644--- a/email/src/main/AndroidManifest.xml+++ b/email/src/main/AndroidManifest.xml@@ -1,147 +1,147 @@-<?xml version="1.0" encoding="utf-8"?>-<manifest xmlns:android="http://schemas.android.com/apk/res/android"-    xmlns:tools="http://schemas.android.com/tools">--    <uses-permission android:name="android.permission.INTERNET" />-    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />-    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />-    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />-    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />--    <permission-        android:name="com.vayunmathur.email.permissions.ACCESS_EMAIL"-        android:protectionLevel="signature" />--    <application-        android:allowBackup="true"-        android:dataExtractionRules="@xml/data_extraction_rules"-        android:icon="@mipmap/ic_launcher"-        android:label="@string/app_name"-        android:roundIcon="@mipmap/ic_launcher"-        android:supportsRtl="true"-        android:networkSecurityConfig="@xml/network_security_config"-        android:theme="@style/Theme.General"-        tools:replace="android:networkSecurityConfig">-        <activity-            android:configChanges="mcc|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|keyboardHidden|layoutDirection|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"-            android:name=".MainActivity"-            android:exported="true"-            android:theme="@style/Theme.General"-            android:launchMode="singleTask">-            <intent-filter>-                <action android:name="android.intent.action.MAIN" />-                <category android:name="android.intent.category.LAUNCHER" />-            </intent-filter>-            <!-- Surfaces an in-app settings entry on the system App Info page. -->-            <intent-filter>-                <action android:name="android.intent.action.APPLICATION_PREFERENCES" />-                <category android:name="android.intent.category.DEFAULT" />-            </intent-filter>--            <intent-filter>-                <action android:name="android.intent.action.SEND" />-                <category android:name="android.intent.category.DEFAULT" />-                <data android:mimeType="text/plain" />-            </intent-filter>--            <intent-filter>-                <action android:name="android.intent.action.SENDTO" />-                <category android:name="android.intent.category.DEFAULT" />-                <data android:scheme="mailto" />-            </intent-filter>--            <!-- .eml file viewer: handle taps from Files / Downloads / file managers -->-            <intent-filter>-                <action android:name="android.intent.action.VIEW" />-                <category android:name="android.intent.category.DEFAULT" />-                <category android:name="android.intent.category.BROWSABLE" />-                <data android:scheme="content" android:mimeType="message/rfc822" />-                <data android:scheme="file" android:mimeType="message/rfc822" />-            </intent-filter>-            <intent-filter>-                <action android:name="android.intent.action.VIEW" />-                <category android:name="android.intent.category.DEFAULT" />-                <category android:name="android.intent.category.BROWSABLE" />-                <data android:scheme="content" />-                <data android:scheme="file" />-                <data android:host="*" />-                <data android:mimeType="application/octet-stream" />-                <data android:pathPattern=".*\\.eml" />-            </intent-filter>-        </activity>--        <receiver-            android:name=".widget.EmailWidgetReceiver"-            android:exported="false"-            android:label="@string/unified_inbox_label">-            <intent-filter>-                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />-            </intent-filter>-            <meta-data-                android:name="android.appwidget.provider"-                android:resource="@xml/email_widget_info" />-        </receiver>--        <service-            android:name=".data.ImapIdleService"-            android:exported="false"-            android:foregroundServiceType="dataSync" />--        <receiver-            android:name=".util.BootReceiver"-            android:exported="true"-            android:enabled="true">-            <intent-filter>-                <action android:name="android.intent.action.BOOT_COMPLETED" />-                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />-                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />-                <action android:name="android.intent.action.QUICKBOOT_POWERON" />-            </intent-filter>-        </receiver>--        <receiver-            android:name=".util.EmailNotificationActionReceiver"-            android:exported="false" />--        <provider-            android:name="androidx.core.content.FileProvider"-            android:authorities="com.vayunmathur.email.fileprovider"-            android:exported="false"-            android:grantUriPermissions="true">-            <meta-data-                android:name="android.support.FILE_PROVIDER_PATHS"-                android:resource="@xml/file_paths" />-        </provider>--        <!-- Microsoft OAuth redirect handler. Azure app registered as Mobile & Desktop-             with redirect com.vayunmathur.email://oauth per RFC8252 compliant native pattern.-             Thunderbird Desktop uses useExternalBrowser=true + custom scheme same as this.-             Single filter only — second filter with host=\"\" was invalid on Android 12+ (AOSP disallows empty host)-             and prevented intent resolution for com.vayunmathur.email://oauth. -->-        <activity-            android:name=".ui.OAuthActivity"-            android:exported="true"-            android:launchMode="singleTask"-            android:theme="@android:style/Theme.Translucent.NoTitleBar">-            <intent-filter>-                <action android:name="android.intent.action.VIEW" />-                <category android:name="android.intent.category.DEFAULT" />-                <category android:name="android.intent.category.BROWSABLE" />-                <data android:scheme="com.vayunmathur.email" android:host="oauth" />-            </intent-filter>-        </activity>--        <activity-            android:name=".intents.SearchIntent"-            android:exported="true"-            android:theme="@android:style/Theme.NoDisplay"-            android:permission="com.vayunmathur.email.permissions.ACCESS_EMAIL" />--        <activity-            android:name=".intents.GetRecentIntent"-            android:exported="true"-            android:theme="@android:style/Theme.NoDisplay"-            android:permission="com.vayunmathur.email.permissions.ACCESS_EMAIL" />-    </application>--</manifest>+<?xml version="1.0" encoding="utf-8"?>+<manifest xmlns:android="http://schemas.android.com/apk/res/android"+    xmlns:tools="http://schemas.android.com/tools">++    <uses-permission android:name="android.permission.INTERNET" />+    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />++    <permission+        android:name="com.vayunmathur.email.permissions.ACCESS_EMAIL"+        android:protectionLevel="signature" />++    <application+        android:allowBackup="true"+        android:dataExtractionRules="@xml/data_extraction_rules"+        android:icon="@mipmap/ic_launcher"+        android:label="@string/app_name"+        android:roundIcon="@mipmap/ic_launcher"+        android:supportsRtl="true"+        android:networkSecurityConfig="@xml/network_security_config"+        android:theme="@style/Theme.General"+        tools:replace="android:networkSecurityConfig">+        <activity+            android:configChanges="mcc|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|keyboardHidden|layoutDirection|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"+            android:name=".MainActivity"+            android:exported="true"+            android:theme="@style/Theme.General"+            android:launchMode="singleTask">+            <intent-filter>+                <action android:name="android.intent.action.MAIN" />+                <category android:name="android.intent.category.LAUNCHER" />+            </intent-filter>+            <!-- Surfaces an in-app settings entry on the system App Info page. -->+            <intent-filter>+                <action android:name="android.intent.action.APPLICATION_PREFERENCES" />+                <category android:name="android.intent.category.DEFAULT" />+            </intent-filter>++            <intent-filter>+                <action android:name="android.intent.action.SEND" />+                <category android:name="android.intent.category.DEFAULT" />+                <data android:mimeType="text/plain" />+            </intent-filter>++            <intent-filter>+                <action android:name="android.intent.action.SENDTO" />+                <category android:name="android.intent.category.DEFAULT" />+                <data android:scheme="mailto" />+            </intent-filter>++            <!-- .eml file viewer: handle taps from Files / Downloads / file managers -->+            <intent-filter>+                <action android:name="android.intent.action.VIEW" />+                <category android:name="android.intent.category.DEFAULT" />+                <category android:name="android.intent.category.BROWSABLE" />+                <data android:scheme="content" android:mimeType="message/rfc822" />+                <data android:scheme="file" android:mimeType="message/rfc822" />+            </intent-filter>+            <intent-filter>+                <action android:name="android.intent.action.VIEW" />+                <category android:name="android.intent.category.DEFAULT" />+                <category android:name="android.intent.category.BROWSABLE" />+                <data android:scheme="content" />+                <data android:scheme="file" />+                <data android:host="*" />+                <data android:mimeType="application/octet-stream" />+                <data android:pathPattern=".*\\.eml" />+            </intent-filter>+        </activity>++        <receiver+            android:name=".widget.EmailWidgetReceiver"+            android:exported="false"+            android:label="@string/unified_inbox_label">+            <intent-filter>+                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />+            </intent-filter>+            <meta-data+                android:name="android.appwidget.provider"+                android:resource="@xml/email_widget_info" />+        </receiver>++        <service+            android:name=".data.ImapIdleService"+            android:exported="false"+            android:foregroundServiceType="dataSync" />++        <receiver+            android:name=".platform.BootReceiver"+            android:exported="true"+            android:enabled="true">+            <intent-filter>+                <action android:name="android.intent.action.BOOT_COMPLETED" />+                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />+                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />+                <action android:name="android.intent.action.QUICKBOOT_POWERON" />+            </intent-filter>+        </receiver>++        <receiver+            android:name=".platform.EmailNotificationActionReceiver"+            android:exported="false" />++        <provider+            android:name="androidx.core.content.FileProvider"+            android:authorities="com.vayunmathur.email.fileprovider"+            android:exported="false"+            android:grantUriPermissions="true">+            <meta-data+                android:name="android.support.FILE_PROVIDER_PATHS"+                android:resource="@xml/file_paths" />+        </provider>++        <!-- Microsoft OAuth redirect handler. Azure app registered as Mobile & Desktop+             with redirect com.vayunmathur.email://oauth per RFC8252 compliant native pattern.+             Thunderbird Desktop uses useExternalBrowser=true + custom scheme same as this.+             Single filter only — second filter with host=\"\" was invalid on Android 12+ (AOSP disallows empty host)+             and prevented intent resolution for com.vayunmathur.email://oauth. -->+        <activity+            android:name=".ui.OAuthActivity"+            android:exported="true"+            android:launchMode="singleTask"+            android:theme="@android:style/Theme.Translucent.NoTitleBar">+            <intent-filter>+                <action android:name="android.intent.action.VIEW" />+                <category android:name="android.intent.category.DEFAULT" />+                <category android:name="android.intent.category.BROWSABLE" />+                <data android:scheme="com.vayunmathur.email" android:host="oauth" />+            </intent-filter>+        </activity>++        <activity+            android:name=".intents.SearchIntent"+            android:exported="true"+            android:theme="@android:style/Theme.NoDisplay"+            android:permission="com.vayunmathur.email.permissions.ACCESS_EMAIL" />++        <activity+            android:name=".intents.GetRecentIntent"+            android:exported="true"+            android:theme="@android:style/Theme.NoDisplay"+            android:permission="com.vayunmathur.email.permissions.ACCESS_EMAIL" />+    </application>++</manifest>
email/src/main/java/com/vayunmathur/email/MainActivity.kt
diff --git a/email/src/main/java/com/vayunmathur/email/MainActivity.kt b/email/src/main/java/com/vayunmathur/email/MainActivity.ktindex 8f866d4ed5..11d1e6ee4b 100644--- a/email/src/main/java/com/vayunmathur/email/MainActivity.kt+++ b/email/src/main/java/com/vayunmathur/email/MainActivity.kt@@ -1,81 +1,37 @@ package com.vayunmathur.email -import androidx.compose.ui.res.pluralStringResource import android.content.Intent-import android.net.Uri import android.os.Bundle import androidx.activity.ComponentActivity-import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge-import androidx.activity.result.contract.ActivityResultContracts-import androidx.compose.foundation.background-import androidx.compose.foundation.clickable-import androidx.compose.foundation.combinedClickable-import androidx.compose.foundation.rememberScrollState-import androidx.compose.foundation.verticalScroll-import androidx.compose.foundation.layout.*-import androidx.compose.foundation.lazy.LazyColumn-import androidx.compose.foundation.lazy.items-import com.vayunmathur.library.ui.R as UiR-import com.vayunmathur.library.util.openSettingsIfRequested-import com.vayunmathur.library.util.AppMessages-import com.vayunmathur.library.ui.*-import androidx.compose.runtime.*-import androidx.lifecycle.compose.collectAsStateWithLifecycle-import androidx.compose.ui.Alignment-import androidx.compose.ui.Modifier-import androidx.compose.ui.graphics.Color-import androidx.compose.ui.platform.LocalContext-import androidx.compose.ui.platform.LocalResources-import androidx.compose.ui.res.painterResource-import androidx.compose.ui.res.stringResource-import com.vayunmathur.email.R-import androidx.compose.ui.text.font.FontWeight-import androidx.compose.ui.text.style.TextAlign-import androidx.compose.ui.unit.dp-import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.lifecycleScope-import androidx.core.graphics.toColorInt-import androidx.core.net.toUri-import androidx.core.text.htmlEncode+import androidx.lifecycle.viewmodel.compose.viewModel+import com.vayunmathur.email.data.DateMillisBackfill import com.vayunmathur.email.data.EmailSyncWorker-import com.vayunmathur.email.util.EmlUtils-import com.vayunmathur.email.util.MessageListActions-import com.vayunmathur.email.util.MessageListUiState-import com.vayunmathur.email.util.MessageThreadActions+import com.vayunmathur.email.data.ImapIdleService+import com.vayunmathur.email.data.OutboxSendWorker+import com.vayunmathur.email.platform.AppLifecycleTracker+import com.vayunmathur.email.platform.EmailViewModel+import com.vayunmathur.email.platform.IntentState+import com.vayunmathur.email.ui.MainContent import com.vayunmathur.email.widget.EmailWidgetReceiver import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.library.network.TrustBundle-import com.vayunmathur.library.util.*+import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.library.widgets.updateWidgetPreviews-import kotlinx.coroutines.flow.debounce-import kotlinx.coroutines.launch-import kotlinx.serialization.Serializable-import com.vayunmathur.library.ui.EmptyState  class MainActivity : ComponentActivity() {      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)-        // SYSTEM permissive: users can configure arbitrary self-hosted IMAP/SMTP with self-signed certs-        // Actual permissiveness enforced by Jakarta mail ssl.trust=* + NSC system+user         NetworkClient.init(this, TrustBundle.SYSTEM)         updateWidgetPreviews(EmailWidgetReceiver::class)         handleIntent(intent)-        // IDLE push for INBOX + hourly non-INBOX poll + on-demand pull-to-refresh.-        // Purge any legacy 15-min full-poll work from older APKs and ensure hourly non-INBOX is scheduled.-        com.vayunmathur.email.data.EmailSyncWorker.scheduleHourlyNonInboxSync(this)-        // Wake the outbox sender on every cold start: if the process was killed-        // between scheduled retries, this is what gets it going again.-        com.vayunmathur.email.data.OutboxSendWorker.runNow(this)-        // Kick the IMAP IDLE service so we get push notifications even when the-        // app is in the background. The service is a no-op if there are no-        // accounts yet (it'll just stopSelf).-        com.vayunmathur.email.data.ImapIdleService.start(this)-        // One-shot backfill for dateMillis on rows persisted before that column.-        com.vayunmathur.email.data.DateMillisBackfill.runIfNeeded(lifecycleScope, this)-        // Android 13+: request POST_NOTIFICATIONS so new-mail alerts can be shown.+        EmailSyncWorker.scheduleHourlyNonInboxSync(this)+        OutboxSendWorker.runNow(this)+        ImapIdleService.start(this)+        DateMillisBackfill.runIfNeeded(lifecycleScope, this)         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {             if (checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) != android.content.pm.PackageManager.PERMISSION_GRANTED) {                 requestPermissions(arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 7331)@@ -92,20 +48,18 @@ class MainActivity : ComponentActivity() {      override fun onStart() {         super.onStart()-        com.vayunmathur.email.util.AppLifecycleTracker.isAppInForeground = true-        // If BootReceiver deferred IDLE on S+ boot, start it now that we're foreground.-        com.vayunmathur.email.util.AppLifecycleTracker.tryStartIdleIfForeground(this)+        AppLifecycleTracker.isAppInForeground = true+        AppLifecycleTracker.tryStartIdleIfForeground(this)     }      override fun onResume() {         super.onResume()-        // Extra safety: also try on resume in case onStart ran before flag set.-        com.vayunmathur.email.util.AppLifecycleTracker.tryStartIdleIfForeground(this)+        AppLifecycleTracker.tryStartIdleIfForeground(this)     }      override fun onStop() {         super.onStop()-        com.vayunmathur.email.util.AppLifecycleTracker.isAppInForeground = false+        AppLifecycleTracker.isAppInForeground = false     }      override fun onNewIntent(intent: Intent) {@@ -115,8 +69,6 @@ class MainActivity : ComponentActivity() {      private fun handleIntent(intent: Intent?) {         if (intent == null) return--        // .eml VIEW from file manager / Downloads / Files app         if (intent.action == Intent.ACTION_VIEW) {             val dataUri = intent.data             if (dataUri != null) {@@ -125,7 +77,6 @@ class MainActivity : ComponentActivity() {                 val lowerMime = mime?.lowercase() ?: ""                 val isEmlMime = lowerMime.contains("rfc822") || lowerMime.contains("mbox")                 var isEmlExtension = lastSeg.endsWith(".eml", ignoreCase = true)-                // Content URIs sometimes encode the filename in DISPLAY_NAME, not lastPathSegment                 if (!isEmlExtension && dataUri.scheme == "content") {                     try {                         contentResolver.query(dataUri, arrayOf(android.provider.OpenableColumns.DISPLAY_NAME), null, null, null)?.use { c ->@@ -143,7 +94,6 @@ class MainActivity : ComponentActivity() {                 }             }         }-         val accountEmail = intent.getStringExtra("accountEmail")         val threadId = intent.getStringExtra("threadId")         when {@@ -162,2095 +112,3 @@ class MainActivity : ComponentActivity() {         }     } }--object IntentState {-    var navigationRoute by mutableStateOf<Route?>(null)-}--@Composable-fun MainContent(viewModel: EmailViewModel) {-    val accounts by viewModel.accounts.collectAsStateWithLifecycle(emptyList())-    val navigationRoute = IntentState.navigationRoute-    val isEmlViewer = navigationRoute is Route.EmlViewer--    if (accounts.isEmpty() && !isEmlViewer) {-        // First run: jump straight into the add-account picker. The conditional-        // above auto-swaps us into EmailApp once the first account lands.-        com.vayunmathur.email.ui.AddAccountScreen(-            onBack = null,-            onAccountAdded = {},-        )-    } else {-        EmailApp(viewModel = viewModel)-    }-}--@Serializable-sealed interface Route : NavKey {-    @Serializable-    object MessageList : Route-    @Serializable-    data class MessageThread(val accountEmail: String, val threadId: String) : Route-    @Serializable-    data class Composer(-        val to: String = "",-        val subject: String = "",-        val body: String = "",-        val inReplyTo: String? = null,-        val references: String? = null,-        val draftId: Long? = null-    ) : Route-    @Serializable-    object Outbox : Route-    @Serializable-    object Drafts : Route-    @Serializable-    object AddAccount : Route-    @Serializable-    object Settings : Route-    @Serializable-    data class EmlViewer(val uriString: String) : Route-}--@Composable-fun EmailApp(viewModel: EmailViewModel) {-    val scope = rememberCoroutineScope()-    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)-    val context = LocalContext.current--    val accounts by viewModel.accounts.collectAsStateWithLifecycle(emptyList())-    val selectedAccountEmail by viewModel.selectedAccountEmail.collectAsStateWithLifecycle()-    val folders by viewModel.folders.collectAsStateWithLifecycle(emptyList())-    val selectedFolderName by viewModel.selectedFolderName.collectAsStateWithLifecycle()-    val outbox by viewModel.outbox.collectAsStateWithLifecycle(emptyList())--    val backStack = rememberNavBackStack<Route>(Route.MessageList)-    // Land on settings when opened from the system App Info page.-    backStack.openSettingsIfRequested(Route.Settings)--    val navigationRoute = IntentState.navigationRoute-    LaunchedEffect(navigationRoute) {-        if (navigationRoute != null) {-            backStack.add(navigationRoute)-            IntentState.navigationRoute = null-        }-    }--    ModalNavigationDrawer(-        drawerState = drawerState,-        drawerContent = {-            ModalDrawerSheet {-                Column(Modifier.fillMaxHeight()) {-                    // Scrollable section: header, accounts, folders, outbox.-                    Column(-                        Modifier-                            .weight(1f)-                            .verticalScroll(rememberScrollState())-                    ) {-                        Text(stringResource(R.string.unified_inbox), modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)-                        NavigationDrawerItem(-                            label = { Text(stringResource(R.string.all_accounts)) },-                            selected = selectedAccountEmail == null,-                            onClick = {-                                viewModel.selectAccount("")-                                backStack.reset(Route.MessageList)-                                scope.launch { drawerState.close() }-                            },-                            icon = { IconInbox() },-                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                        )--                        HorizontalDivider(Modifier.padding(vertical = 8.dp))--                        Text(stringResource(R.string.accounts), modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)-                        accounts.forEach { account ->-                            NavigationDrawerItem(-                                label = { Text(account.email) },-                                selected = account.email == selectedAccountEmail,-                                onClick = {-                                    viewModel.selectAccount(account.email)-                                    backStack.reset(Route.MessageList)-                                    scope.launch { drawerState.close() }-                                },-                                icon = {-                                    val accountColor = Color(account.getColor())-                                    Surface(-                                        shape = androidx.compose.foundation.shape.CircleShape,-                                        color = accountColor,-                                        modifier = Modifier.size(24.dp)-                                    ) {-                                        Box(contentAlignment = Alignment.Center) {-                                            IconMail(modifier = Modifier.size(16.dp), tint = contentColorOn(accountColor))-                                        }-                                    }-                                },-                                modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                            )-                        }-                        NavigationDrawerItem(-                            label = { Text(stringResource(R.string.add_account)) },-                            selected = false,-                            onClick = {-                                backStack.add(Route.AddAccount)-                                scope.launch { drawerState.close() }-                            },-                            icon = { IconAdd() },-                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                        )--                        if (selectedAccountEmail != null) {-                            HorizontalDivider(Modifier.padding(vertical = 8.dp))-                            Text(stringResource(R.string.folders), modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)-                            FolderList(folders, selectedFolderName) { folderName ->-                                viewModel.selectFolder(folderName)-                                backStack.reset(Route.MessageList)-                                scope.launch { drawerState.close() }-                            }-                        }--                        HorizontalDivider(Modifier.padding(vertical = 8.dp))-                        NavigationDrawerItem(-                            label = {-                                Text(-                                    if (outbox.isEmpty()) stringResource(R.string.outbox)-                                    else stringResource(R.string.outbox_with_count, outbox.size)-                                )-                            },-                            selected = false,-                            onClick = {-                                backStack.add(Route.Outbox)-                                scope.launch { drawerState.close() }-                            },-                            icon = { IconSend() },-                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                        )-                        NavigationDrawerItem(-                            label = { Text(stringResource(UiR.string.settings)) },-                            selected = false,-                            onClick = {-                                backStack.add(Route.Settings)-                                scope.launch { drawerState.close() }-                            },-                            icon = { com.vayunmathur.library.ui.IconSettings() },-                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                        )-                        NavigationDrawerItem(-                            label = { Text(stringResource(R.string.drafts)) },-                            selected = false,-                            onClick = {-                                backStack.add(Route.Drafts)-                                scope.launch { drawerState.close() }-                            },-                            icon = { com.vayunmathur.library.ui.IconEdit() },-                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                        )-                    }--                    // Pinned footer: logout stays visible no matter how long the list above is.-                    // Only show logout button when a specific account is selected (not in unified inbox)-                    if (selectedAccountEmail != null) {-                        HorizontalDivider()-                        NavigationDrawerItem(-                            label = { Text(stringResource(R.string.logout_current_account)) },-                            selected = false,-                            onClick = {-                                viewModel.logout(context)-                                scope.launch { drawerState.close() }-                            },-                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)-                        )-                    }-                }-            }-        }-    ) {-        MainNavigation(backStack) {-            entry<Route.MessageList>(metadata = ListPage()) {-                MessageListPage(-                    viewModel = viewModel,-                    onMessageClick = { msg ->-                        backStack.add(Route.MessageThread(msg.accountEmail, msg.threadId ?: msg.id.toString()))-                    },-                    onComposeClick = { backStack.add(Route.Composer()) },-                    onOpenDrawer = { scope.launch { drawerState.open() } }-                )-            }-            entry<Route.MessageThread>(metadata = ListDetailPage()) { route ->-                MessageThreadPage(-                    viewModel = viewModel,-                    accountEmail = route.accountEmail,-                    threadId = route.threadId,-                    onBack = { backStack.pop() },-                    onReply = { to, sub, ref ->-                        backStack.add(Route.Composer(to = to, subject = "Re: $sub", references = ref, inReplyTo = ref))-                    },-                    onForward = { sub, body ->-                        backStack.add(Route.Composer(subject = "Fwd: $sub", body = "\n\n---------- Forwarded message ----------\n$body"))-                    },-                    onCompose = { to, sub ->-                        backStack.add(Route.Composer(to = to, subject = sub))-                    }-                )-            }-            entry<Route.Composer>(metadata = ListDetailPage()) { route ->-                ComposerScreen(-                    viewModel = viewModel,-                    initialTo = route.to,-                    initialSubject = route.subject,-                    initialBody = route.body,-                    inReplyTo = route.inReplyTo,-                    references = route.references,-                    draftId = route.draftId,-                    onBack = { backStack.pop() }-                )-            }-            entry<Route.Outbox>(metadata = ListDetailPage()) {-                OutboxScreen(-                    viewModel = viewModel,-                    onBack = { backStack.pop() }-                )-            }-            entry<Route.AddAccount>(metadata = ListDetailPage()) {-                com.vayunmathur.email.ui.AddAccountScreen(-                    onBack = { backStack.pop() },-                    onAccountAdded = { backStack.pop() },-                )-            }-            entry<Route.Settings>(metadata = ListDetailPage()) {-                SettingsScreen(viewModel = viewModel, onBack = { backStack.pop() })-            }-            entry<Route.Drafts>(metadata = ListDetailPage()) {-                DraftsScreen(-                    viewModel = viewModel,-                    onBack = { backStack.pop() },-                    onOpenDraft = { id -> backStack.add(Route.Composer(draftId = id)) },-                )-            }-            entry<Route.EmlViewer>(metadata = ListDetailPage()) { route ->-                com.vayunmathur.email.ui.EmlViewerScreen(-                    uriString = route.uriString,-                    onBack = { backStack.pop() },-                    onComposeForward = { sub, body ->-                        backStack.add(Route.Composer(subject = sub, body = body))-                    },-                )-            }-        }-    }-}--@Composable-fun FolderList(folders: List<EmailFolder>, selectedFolder: String, onSelect: (String) -> Unit) {-    val folderTree = remember(folders) { buildFolderTree(folders) }--    // NOTE: a `Column` (not `LazyColumn`) — this composable is rendered-    // inside the drawer's outer `verticalScroll(...)` Column, and nesting a-    // lazy list inside an unbounded vertical scroller crashes with-    // "Vertically scrollable component was measured with an infinity maximum-    // height constraints". Folder lists are tiny so non-lazy is fine.-    Column {-        folderTree.forEach { root ->-            RenderFolderTree(root, 0, selectedFolder, onSelect)-        }-    }-}--data class FolderNode(val folder: EmailFolder, val children: List<FolderNode>)--fun buildFolderTree(folders: List<EmailFolder>): List<FolderNode> {-    val childrenMap = folders.groupBy { it.parentFullName }-    -    fun buildNode(folder: EmailFolder): FolderNode {-        return FolderNode(-            folder = folder,-            children = childrenMap[folder.fullName]?.map { buildNode(it) } ?: emptyList()-        )-    }-    -    return folders.filter { it.parentFullName == null }.map { buildNode(it) }-}--@Composable-fun RenderFolderTree(-    node: FolderNode,-    depth: Int,-    selectedFolder: String,-    onSelect: (String) -> Unit-) {-    NavigationDrawerItem(-        label = {-            Text(-                text = node.folder.name,-                color = if (node.folder.holdsMessages) LocalContentColor.current else LocalContentColor.current.copy(alpha = 0.5f)-            )-        },-        selected = node.folder.fullName == selectedFolder,-        onClick = { if (node.folder.holdsMessages) onSelect(node.folder.fullName) },-        modifier = Modifier-            .padding(NavigationDrawerItemDefaults.ItemPadding)-            .padding(start = (depth * 16).dp)-    )-    node.children.forEach { child ->-        RenderFolderTree(child, depth + 1, selectedFolder, onSelect)-    }-}--/** Binds [EmailViewModel] to the stateless [MessageListScreen]. */-@Composable-fun MessageListPage(-    viewModel: EmailViewModel,-    onMessageClick: (EmailMessage) -> Unit,-    onComposeClick: () -> Unit,-    onOpenDrawer: () -> Unit-) {-    val messages by viewModel.messages.collectAsStateWithLifecycle(emptyList())-    val selectedAccountEmail by viewModel.selectedAccountEmail.collectAsStateWithLifecycle()-    val selectedFolderName by viewModel.selectedFolderName.collectAsStateWithLifecycle()-    val searchQuery by viewModel.searchQuery.collectAsStateWithLifecycle()-    val selectedUids by viewModel.selectedMessageUids.collectAsStateWithLifecycle()-    val isSyncing by viewModel.isSyncing.collectAsStateWithLifecycle()-    val syncProgress by viewModel.syncProgress.collectAsStateWithLifecycle()-    val aiSummary by viewModel.aiSummary.collectAsStateWithLifecycle()-    val aiSummaryLoading by viewModel.aiSummaryLoading.collectAsStateWithLifecycle()--    MessageListScreen(-        state = MessageListUiState(-            messages = messages,-            selectedAccountEmail = selectedAccountEmail,-            selectedFolderName = selectedFolderName,-            searchQuery = searchQuery,-            selectedUids = selectedUids,-            isSyncing = isSyncing,-            syncProgress = syncProgress,-            aiSummary = aiSummary,-            aiSummaryLoading = aiSummaryLoading,-        ),-        actions = viewModel,-        onMessageClick = onMessageClick,-        onComposeClick = onComposeClick,-        onOpenDrawer = onOpenDrawer,-    )-}--/**- * The message list, with no dependency on the ViewModel so it can be rendered from a- * `@Preview` — see `src/screenshotTest`, which is where the store listing images come from.- */-@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun MessageListScreen(-    state: MessageListUiState,-    actions: MessageListActions,-    onMessageClick: (EmailMessage) -> Unit = {},-    onComposeClick: () -> Unit = {},-    onOpenDrawer: () -> Unit = {},-    /**-     * Seed for the screen's own UI-only state: whether the search field has taken over the-     * app bar. The app always takes the default; a preview sets it so search results can be-     * captured without typing into the field.-     */-    initialSearching: Boolean = false,-) {-    var isSearching by remember { mutableStateOf(initialSearching) }-    val context = LocalContext.current-    val snackbarHostState = remember { SnackbarHostState() }-    var pendingDelete by remember { mutableStateOf<EmailMessage?>(null) }--    // Show an Undo snackbar; commit the server/local delete only if not undone.-    LaunchedEffect(pendingDelete) {-        val msg = pendingDelete ?: return@LaunchedEffect-        val result = snackbarHostState.showSnackbar(-            message = "Message deleted",-            actionLabel = "Undo",-            duration = com.vayunmathur.library.ui.SnackbarDuration.Short,-        )-        if (result != com.vayunmathur.library.ui.SnackbarResult.ActionPerformed) {-            actions.deleteMessage(msg.accountEmail, msg.folderName, msg.id)-        }-        pendingDelete = null-    }--    androidx.activity.compose.BackHandler(enabled = isSearching || state.selectedUids.isNotEmpty()) {-        if (state.selectedUids.isNotEmpty()) {-            actions.clearSelection()-        } else if (state.searchQuery.isNotEmpty()) {-            actions.setSearchQuery("")-        } else {-            isSearching = false-        }-    }--    LaunchedEffect(state.searchQuery) {-        if (state.searchQuery.isNotEmpty()) {-            kotlinx.coroutines.delay(1500)-            if (state.messages.isNotEmpty()) {-                actions.requestAiSummary(state.messages)-            }-        }-    }--    Scaffold(-        snackbarHost = { SnackbarHost(snackbarHostState) },-        topBar = {-            if (state.selectedUids.isNotEmpty()) {-                TopAppBar(-                    title = { Text(stringResource(R.string.selected_count, state.selectedUids.size)) },-                    navigationIcon = {-                        IconButton(onClick = { actions.clearSelection() }) {-                            IconClose()-                        }-                    },-                    actions = {-                        IconButton(onClick = {-                            val account = state.selectedAccountEmail ?: state.messages.firstOrNull { it.id in state.selectedUids }?.accountEmail ?: return@IconButton-                            actions.bulkMarkAsRead(account, state.selectedUids.toList(), true)-                        }) {-                            IconMarkRead()-                        }-                        IconButton(onClick = {-                            val account = state.selectedAccountEmail ?: state.messages.firstOrNull { it.id in state.selectedUids }?.accountEmail ?: return@IconButton-                            actions.bulkMarkAsRead(account, state.selectedUids.toList(), false)-                        }) {-                            IconMarkUnread()-                        }-                    }-                )-            } else if (isSearching) {-                TopAppBar(-                    title = {-                        CommonSearchBar(-                            value = state.searchQuery,-                            onValueChange = { actions.setSearchQuery(it) },-                            padding = PaddingValues(0.dp)-                        )-                    },-                    navigationIcon = {-                        IconNavigation {-                            isSearching = false-                            actions.setSearchQuery("")-                        }-                    }-                )-            } else {-                TopAppBar(-                    title = { Text(if (state.selectedAccountEmail == null) stringResource(R.string.unified_inbox) else state.selectedFolderName) },-                    navigationIcon = {-                        IconButton(onClick = onOpenDrawer) {-                            IconMenu()-                        }-                    },-                    actions = {-                        IconButton(onClick = { isSearching = true }) {-                            IconSearch()-                        }-                    }-                )-            }-        },-        floatingActionButton = {-            if (state.selectedUids.isEmpty()) {-                FloatingActionButton(onClick = onComposeClick) {-                    IconAdd()-                }-            }-        }-    ) { padding ->-        Column(modifier = Modifier.padding(padding).fillMaxSize()) {-            // Thin progress bar while a sync is in flight. We use a `Box` of-            // fixed 2.dp height so the bar's presence never shifts the LazyColumn-            // (the bar is rendered on top of an invisible track).-            Box(modifier = Modifier.fillMaxWidth().height(2.dp)) {-                if (state.isSyncing) {-                    LinearProgressIndicator(-                        progress = { state.syncProgress },-                        modifier = Modifier.fillMaxWidth().height(2.dp),-                    )-                }-            }-            // Quick filters over the loaded messages.-            var msgFilter by remember { mutableStateOf(0) } // 0=All, 1=Unread, 2=Attachments-            Row(-                modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),-                horizontalArrangement = Arrangement.spacedBy(8.dp),-            ) {-                com.vayunmathur.library.ui.FilterChip(selected = msgFilter == 0, onClick = { msgFilter = 0 }, label = { Text(stringResource(R.string.all)) })-                com.vayunmathur.library.ui.FilterChip(selected = msgFilter == 1, onClick = { msgFilter = 1 }, label = { Text(stringResource(R.string.unread)) })-                com.vayunmathur.library.ui.FilterChip(selected = msgFilter == 2, onClick = { msgFilter = 2 }, label = { Text(stringResource(R.string.attachments)) })-            }-            // Keyed on the list rather than derivedStateOf: `state` is a plain parameter,-            // so a keyless remember would pin whichever snapshot arrived first.-            val filteredMessages = remember(state.messages, msgFilter) {-                when (msgFilter) {-                    1 -> state.messages.filter { !it.isRead }-                    2 -> state.messages.filter { it.hasAttachments }-                    else -> state.messages-                }-            }-            com.vayunmathur.library.ui.PullToRefreshBox(-                isRefreshing = state.isSyncing,-                onRefresh = { actions.refresh(context) },-                modifier = Modifier.fillMaxSize(),-            ) {-                if (state.messages.isEmpty() && state.searchQuery.isEmpty()) {-                    Box(modifier = Modifier.fillMaxSize()) {-                        Text(-                            text = stringResource(R.string.no_messages_found_pull_down_to_refresh),-                            modifier = Modifier.align(Alignment.Center)-                        )-                    }-                } else {-                    LazyColumn(-                        modifier = Modifier.fillMaxSize()-                    ) {-                    if (state.searchQuery.isNotEmpty() && (state.aiSummaryLoading || state.aiSummary != null)) {-                        item(key = "ai_summary") {-                            Card(-                                modifier = Modifier.fillMaxWidth().padding(12.dp),-                                colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer)-                            ) {-                                Column(modifier = Modifier.padding(16.dp)) {-                                    Text(stringResource(R.string.ai_summary), style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.onSecondaryContainer)-                                    Spacer(Modifier.height(8.dp))-                                    if (state.aiSummaryLoading) {-                                        Row(verticalAlignment = Alignment.CenterVertically) {-                                            CircularProgressIndicator(modifier = Modifier.size(16.dp), strokeWidth = 2.dp)-                                            Spacer(Modifier.width(8.dp))-                                            Text(stringResource(R.string.generating_summary), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSecondaryContainer)-                                        }-                                    } else {-                                        Text(state.aiSummary ?: "", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSecondaryContainer)-                                    }-                                }-                            }-                        }-                    }-                    items(-                        filteredMessages,-                        key = { "${it.accountEmail}|${it.folderName}|${it.id}" }-                    ) { message ->-                        val isPending = pendingDelete?.let {-                            it.id == message.id && it.accountEmail == message.accountEmail && it.folderName == message.folderName-                        } == true-                        if (!isPending) {-                        val accountBandColor = Color(accountColor(message.accountEmail))-                        val isSelected = message.id in state.selectedUids-                        // Read the latest message snapshot inside the swipe handler so-                        // repeated read/unread swipes toggle the CURRENT persisted state-                        // (the confirmValueChange lambda is captured once by the state).-                        val currentMessage by rememberUpdatedState(message)-                        val dismissState = com.vayunmathur.library.ui.rememberSwipeToDismissBoxState(-                            confirmValueChange = { value ->-                                when (value) {-                                    com.vayunmathur.library.ui.SwipeToDismissBoxValue.EndToStart -> { pendingDelete = currentMessage; true }-                                    com.vayunmathur.library.ui.SwipeToDismissBoxValue.StartToEnd -> {-                                        actions.markAsRead(currentMessage.accountEmail, currentMessage.folderName, currentMessage.id, !currentMessage.isRead)-                                        false-                                    }-                                    else -> false-                                }-                            }-                        )-                        com.vayunmathur.library.ui.SwipeToDismissBox(-                            state = dismissState,-                            backgroundContent = {-                                // Only show a background icon while an active swipe is in-                                // progress in that direction — never when settled.-                                when (dismissState.dismissDirection) {-                                    com.vayunmathur.library.ui.SwipeToDismissBoxValue.EndToStart ->-                                        Box(-                                            modifier = Modifier.fillMaxSize().padding(horizontal = 20.dp),-                                            contentAlignment = Alignment.CenterEnd,-                                        ) { com.vayunmathur.library.ui.IconDelete() }-                                    com.vayunmathur.library.ui.SwipeToDismissBoxValue.StartToEnd ->-                                        Box(-                                            modifier = Modifier.fillMaxSize().padding(horizontal = 20.dp),-                                            contentAlignment = Alignment.CenterStart,-                                        ) { IconMarkRead() }-                                    else -> {}-                                }-                            },-                        ) {-                        Row(-                            modifier = Modifier-                                .combinedClickable(-                                    onClick = {-                                        if (state.selectedUids.isNotEmpty()) {-                                            actions.toggleMessageSelection(message.id)-                                        } else {-                                            onMessageClick(message)-                                        }-                                    },-                                    onLongClick = {-                                        actions.toggleMessageSelection(message.id)-                                    }-                                )-                                .background(if (isSelected) MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.3f) else Color.Transparent)-                                .height(IntrinsicSize.Min)-                        ) {-                            if (state.selectedAccountEmail == null) {-                                Surface(-                                    modifier = Modifier.width(4.dp).fillMaxHeight(),-                                    color = accountBandColor-                                ) {}-                            }-                            // Primary color band indicator for unread messages-                            Surface(-                                modifier = Modifier.width(4.dp).fillMaxHeight(),-                                color = if (message.isRead) Color.Transparent else MaterialTheme.colorScheme.primary-                            ) {}-                            ListItem(-                                leadingContent = null,-                                content = {-                                    Text(-                                        text = message.subject,-                                        style = MaterialTheme.typography.bodyLarge,-                                        fontWeight = if (message.isRead) FontWeight.Normal else FontWeight.Bold-                                    )-                                },-                                supportingContent = {-                                    Column {-                                        Text(-                                            text = senderDisplayName(message.from),-                                            style = MaterialTheme.typography.titleSmall-                                        )-                                        val preview = remember(message.body, message.isHtml) { message.previewText(100) }-                                        Text(-                                            text = preview,-                                            style = MaterialTheme.typography.bodySmall,-                                            maxLines = 1,-                                            color = MaterialTheme.colorScheme.onSurfaceVariant-                                        )-                                    }-                                },-                                trailingContent = {-                                    Column(horizontalAlignment = Alignment.End) {-                                        Text(text = message.date.substringBefore(" "), style = MaterialTheme.typography.labelSmall)-                                        if (message.hasAttachments) {-                                            IconAttachment(modifier = Modifier.size(16.dp))-                                        }-                                    }-                                },-                                modifier = Modifier.weight(1f),-                                colors = ListItemDefaults.colors(containerColor = Color.Transparent)-                            )-                        }-                        }-                        HorizontalDivider()-                        }-                    }-                }-            }-            }-        }-    }-}--/** Binds [EmailViewModel] to the stateless [MessageThreadScreen]. */-@Composable-fun MessageThreadPage(-    viewModel: EmailViewModel,-    accountEmail: String,-    threadId: String,-    onBack: () -> Unit,-    onReply: (String, String, String?) -> Unit,-    onForward: (String, String?) -> Unit,-    onCompose: (String, String) -> Unit-) {-    val messages by viewModel.getThread(accountEmail, threadId).collectAsStateWithLifecycle(emptyList())-    MessageThreadScreen(-        messages = messages,-        actions = viewModel,-        threadId = threadId,-        onBack = onBack,-        onReply = onReply,-        onForward = onForward,-        onCompose = onCompose,-    )-}--/**- * A conversation, with no dependency on the ViewModel so it can be rendered from a- * `@Preview` — see `src/screenshotTest`, which is where the store listing images come from.- *- * [threadId] is not read for display; it only keys the "mark the thread read once" latch- * so navigating to a different thread arms it again.- */-@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun MessageThreadScreen(-    messages: List<EmailMessage>,-    actions: MessageThreadActions,-    threadId: String = "",-    onBack: () -> Unit = {},-    onReply: (String, String, String?) -> Unit = { _, _, _ -> },-    onForward: (String, String?) -> Unit = { _, _ -> },-    onCompose: (String, String) -> Unit = { _, _ -> },-) {-    var hasMarkedAsRead by remember(threadId) { mutableStateOf(false) }-    val context = LocalContext.current-    var pendingExport by remember { mutableStateOf<EmailMessage?>(null) }--    // SAF CreateDocument launcher for .eml export — streams raw RFC822 from IMAP directly to user-chosen location.-    val exportLauncher = rememberLauncherForActivityResult(-        ActivityResultContracts.CreateDocument("message/rfc822")-    ) { targetUri ->-        val msg = pendingExport-        pendingExport = null-        if (targetUri != null && msg != null) {-            actions.exportEml(-                accountEmail = msg.accountEmail,-                folderName = msg.folderName,-                uid = msg.id,-                targetUri = targetUri,-            ) { ok, err ->-                val toastText = if (ok) "Saved as .eml" else "Save failed: ${err ?: "unknown"}"-                AppMessages.show(toastText)-            }-        }-    }--    // Mark all unread messages in the thread as read when the screen loads.-    // Only run once per thread navigation to allow user to mark as unread.-    LaunchedEffect(messages) {-        if (!hasMarkedAsRead && messages.isNotEmpty()) {-            messages.filter { !it.isRead }.forEach { msg ->-                actions.markAsRead(msg.accountEmail, msg.folderName, msg.id, true)-            }-            hasMarkedAsRead = true-        }-    }--    Scaffold(-        topBar = {-            TopAppBar(-                title = { Text(messages.firstOrNull()?.subject ?: stringResource(R.string.conversation)) },-                navigationIcon = { IconNavigation(onBack) }-            )-        }-    ) { padding ->-        LazyColumn(-            modifier = Modifier.padding(padding).fillMaxSize(),-            contentPadding = PaddingValues(bottom = 16.dp),-            verticalArrangement = Arrangement.spacedBy(8.dp)-        ) {-            items(messages, key = { "${it.accountEmail}|${it.folderName}|${it.id}" }) { msg ->-                MessageItem(-                    msg = msg,-                    actions = actions,-                    onBack = onBack,-                    onReply = onReply,-                    onForward = onForward,-                    onCompose = onCompose,-                    onExportEml = { toExport ->-                        pendingExport = toExport-                        val fileName = EmlUtils.sanitizeFileName(-                            toExport.subject.ifBlank { "email_${toExport.id}" }-                        )-                        exportLauncher.launch(fileName)-                    },-                )-            }-        }-    }-}--@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun MessageItem(-    msg: EmailMessage,-    actions: MessageThreadActions,-    onBack: () -> Unit,-    onReply: (String, String, String?) -> Unit,-    onForward: (String, String?) -> Unit,-    onCompose: (String, String) -> Unit,-    onExportEml: (EmailMessage) -> Unit = {},-) {-    var attachments by remember { mutableStateOf<List<Attachment>>(emptyList()) }-    var showDetails by remember { mutableStateOf(false) }-    val sheetState = rememberModalBottomSheetState()-    val context = LocalContext.current-    val resources = LocalResources.current-    var showOverflow by remember { mutableStateOf(false) }--    LaunchedEffect(msg.id) {-        attachments = actions.getAttachments(msg.accountEmail, msg.id)-        if (msg.body == null) {-            actions.fetchBodyIfNeeded(msg)-        }-    }--    val senderName = senderDisplayName(msg.from).ifEmpty { msg.from }-    val senderEmail = msg.from.substringAfter("<").substringBefore(">").trim()-    val initial = senderName.take(1).uppercase()-    val avatarColor = Color(accountColor(msg.accountEmail))--    Column {-        Row(-            modifier = Modifier-                .fillMaxWidth()-                .clickable { showDetails = true }-                .padding(16.dp),-            verticalAlignment = Alignment.CenterVertically-        ) {-            Surface(-                shape = androidx.compose.foundation.shape.CircleShape,-                color = avatarColor,-                modifier = Modifier.size(40.dp)-            ) {-                Box(contentAlignment = Alignment.Center) {-                    Text(text = initial, color = contentColorOn(avatarColor), style = MaterialTheme.typography.titleMedium)-                }-            }-            Column(-                modifier = Modifier-                    .weight(1f)-                    .padding(horizontal = 12.dp)-            ) {-                Row(verticalAlignment = Alignment.CenterVertically) {-                    Text(-                        text = senderName,-                        style = MaterialTheme.typography.titleSmall,-                        maxLines = 1,-                        overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis,-                        modifier = Modifier.weight(1f, fill = false)-                    )-                    Text(-                        text = "  •  ${msg.date.substringBeforeLast(":")}",-                        style = MaterialTheme.typography.labelSmall,-                        color = MaterialTheme.colorScheme.onSurfaceVariant-                    )-                }-                Text(-                    text = stringResource(R.string.to_me),-                    style = MaterialTheme.typography.bodySmall,-                    color = MaterialTheme.colorScheme.onSurfaceVariant-                )-            }-            Row(verticalAlignment = Alignment.CenterVertically) {-                var showSnooze by remember { mutableStateOf(false) }-                Box {-                    TextButton(onClick = { showSnooze = true }) { Text(stringResource(R.string.snooze)) }-                    DropdownMenu(expanded = showSnooze, onDismissRequest = { showSnooze = false }) {-                        val snooze = { at: Long ->-                            showSnooze = false-                            actions.snoozeMessage(msg.accountEmail, msg.folderName, msg.id, at)-                            AppMessages.show(resources.getString(R.string.snoozed))-                            onBack()-                        }-                        DropdownMenuItem(text = { Text(stringResource(R.string.later_today_6_pm)) }, onClick = { snooze(scheduleTime(18, sameDay = true)) })-                        DropdownMenuItem(text = { Text(stringResource(R.string.tomorrow_8_am)) }, onClick = { snooze(scheduleTime(8, sameDay = false)) })-                        DropdownMenuItem(text = { Text(stringResource(R.string.in_1_week)) }, onClick = { snooze(System.currentTimeMillis() + 7L * 24 * 3600_000) })-                    }-                }-                IconButton(onClick = { onReply(msg.from, msg.subject, msg.serverId) }) {-                    IconUndo()-                }-                IconButton(onClick = {-                    actions.markAsRead(msg.accountEmail, msg.folderName, msg.id, !msg.isRead)-                    if (msg.isRead) {-                        onBack()-                    }-                }) {-                    if (msg.isRead) IconMarkUnread() else IconMarkRead()-                }-                Box {-                    IconButton(onClick = { showOverflow = true }) { IconMoreVert() }-                    DropdownMenu(expanded = showOverflow, onDismissRequest = { showOverflow = false }) {-                        DropdownMenuItem(-                            text = { Text(stringResource(R.string.save_as_eml)) },-                            leadingIcon = { IconSave(modifier = Modifier.size(20.dp)) },-                            onClick = { showOverflow = false; onExportEml(msg) },-                        )-                    }-                }-            }-        }--        if (msg.isHtml && msg.body != null) {-            val settings = remember(context) { com.vayunmathur.email.data.EmailSettings.get(context) }-            val loadRemoteByDefault by settings.loadRemoteImages.collectAsStateWithLifecycle()-            var loadImages by remember(msg.id, loadRemoteByDefault) { mutableStateOf(loadRemoteByDefault) }-            var showQuotes by remember(msg.id) { mutableStateOf(false) }-            var cidMap by remember(msg.id) { mutableStateOf<Map<String, java.io.File>>(emptyMap()) }-            val bodyHasCid = remember(msg.body) { msg.body.contains("cid:", ignoreCase = true) }-            LaunchedEffect(msg.id, msg.body) {-                if (bodyHasCid) {-                    val map = actions.loadCidMap(context, msg)-                    if (map.isNotEmpty()) cidMap = map-                }-            }-            val hasQuotes = remember(msg.body) {-                listOf("gmail_quote", "yahoo_quoted", "moz-cite-prefix", "<blockquote").any { msg.body.contains(it, ignoreCase = true) }-            }-            val remoteBlocked = !loadImages && cidMap.isEmpty() && !bodyHasCid-            if (remoteBlocked) {-                Row(-                    modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 4.dp),-                    horizontalArrangement = Arrangement.SpaceBetween,-                    verticalAlignment = Alignment.CenterVertically,-                ) {-                    Text(stringResource(R.string.remote_images_blocked), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)-                    TextButton(onClick = { loadImages = true }) { Text(stringResource(R.string.load_images)) }-                }-            }-            HtmlText(-                html = msg.body,-                blockRemoteImages = remoteBlocked,-                hideQuotes = hasQuotes && !showQuotes,-                cidMap = cidMap,-                modifier = Modifier.padding(horizontal = 16.dp).fillMaxWidth()-            )-            if (hasQuotes) {-                TextButton(onClick = { showQuotes = !showQuotes }, modifier = Modifier.padding(horizontal = 8.dp)) {-                    Text(if (showQuotes) stringResource(R.string.hide_quoted_text) else stringResource(R.string.show_quoted_text))-                }-            }-        } else {-            var showQuotes by remember(msg.id) { mutableStateOf(false) }-            val (mainText, quotedText) = remember(msg.body) { splitQuotedText(msg.body ?: "(No Content)") }-            Text(-                text = if (showQuotes || quotedText.isEmpty()) (msg.body ?: stringResource(R.string.no_content)) else mainText,-                style = MaterialTheme.typography.bodyMedium,-                modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp)-            )-            if (quotedText.isNotEmpty()) {-                TextButton(onClick = { showQuotes = !showQuotes }, modifier = Modifier.padding(horizontal = 8.dp)) {-                    Text(if (showQuotes) stringResource(R.string.hide_quoted_text) else stringResource(R.string.show_quoted_text))-                }-            }-        }--        if (attachments.isNotEmpty()) {-            Column(modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)) {-                Text(stringResource(R.string.attachments_2), style = MaterialTheme.typography.labelLarge)-                attachments.forEach { att -> AttachmentItem(att, actions) }-            }-        }--        Row(modifier = Modifier.padding(16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {-            OutlinedButton(onClick = { onReply(msg.from, msg.subject, msg.serverId) }, modifier = Modifier.weight(1f)) {-                IconUndo(modifier = Modifier.size(18.dp)); Spacer(Modifier.width(8.dp)); Text(stringResource(R.string.reply))-            }-            OutlinedButton(onClick = { onForward(msg.subject, msg.body) }, modifier = Modifier.weight(1f)) {-                IconForward(modifier = Modifier.size(18.dp)); Spacer(Modifier.width(8.dp)); Text(stringResource(R.string.forward))-            }-        }--        Row(modifier = Modifier.padding(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {-            val unsubscribe = remember(msg.listUnsubscribe, msg.listUnsubscribePost, msg.body, msg.isHtml) { msg.detectUnsubscribe() }-            if (unsubscribe != null) {-                var showConfirm by remember(msg.id) { mutableStateOf(false) }-                TextButton(onClick = { showConfirm = true }) { Text(stringResource(R.string.unsubscribe)) }-                if (showConfirm) {-                    UnsubscribeDialog(method = unsubscribe, onDismiss = { showConfirm = false }, onConfirm = {-                        showConfirm = false; performUnsubscribe(unsubscribe, context, actions, onCompose)-                    })-                }-            }-            TextButton(onClick = {-                actions.blockSender(msg.from)-                AppMessages.show(resources.getString(R.string.sender_blocked))-                onBack()-            }) { Text(stringResource(R.string.block_sender)) }-        }-        HorizontalDivider()-    }--    if (showDetails) {-        ModalBottomSheet(onDismissRequest = { showDetails = false }, sheetState = sheetState) {-            Column(modifier = Modifier.padding(16.dp).fillMaxWidth()) {-                Text(text = msg.date, style = MaterialTheme.typography.labelSmall, modifier = Modifier.align(Alignment.End))-                DetailItem(label = stringResource(R.string.from_label), name = senderName, email = senderEmail, avatarColor = avatarColor)-                DetailItem(label = stringResource(R.string.to_label), name = "me", email = msg.to ?: "", avatarColor = MaterialTheme.colorScheme.surfaceVariant)-                Spacer(Modifier.height(16.dp))-                Row(verticalAlignment = Alignment.CenterVertically) {-                    IconInbox(modifier = Modifier.size(20.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant)-                    Spacer(Modifier.width(12.dp))-                    Text(text = msg.folderName, style = MaterialTheme.typography.bodyMedium)-                }-                Spacer(Modifier.height(32.dp))-            }-        }-    }-}----@Composable-fun DetailItem(label: String, name: String, email: String, avatarColor: Color) {-    Column(modifier = Modifier.padding(vertical = 8.dp)) {-        Text(text = label, style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.primary)-        Row(-            modifier = Modifier.padding(vertical = 8.dp),-            verticalAlignment = Alignment.CenterVertically-        ) {-            Surface(-                shape = androidx.compose.foundation.shape.CircleShape,-                color = avatarColor,-                modifier = Modifier.size(32.dp)-            ) {-                Box(contentAlignment = Alignment.Center) {-                    Text(text = name.take(1).uppercase(), color = contentColorOn(avatarColor), style = MaterialTheme.typography.bodyLarge)-                }-            }-            Column(modifier = Modifier.padding(start = 12.dp)) {-                Text(text = name, style = MaterialTheme.typography.bodyLarge)-                Text(text = email, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)-            }-        }-    }-}--@Composable-fun AttachmentItem(attachment: Attachment, actions: MessageThreadActions) {-    val context = LocalContext.current-    val resources = LocalResources.current-    var downloading by remember { mutableStateOf(false) }-    var localPath by remember { mutableStateOf(attachment.localUri) }--    Row(-        modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),-        verticalAlignment = Alignment.CenterVertically-    ) {-        Text(text = attachment.fileName, modifier = Modifier.weight(1f), style = MaterialTheme.typography.bodySmall)-        if (localPath != null) {-            Text(-                stringResource(R.string.open),-                color = MaterialTheme.colorScheme.primary,-                style = MaterialTheme.typography.labelSmall,-                modifier = Modifier.clickable {-                    val uri = try { localPath?.toUri() } catch (e: Exception) { null }-                    if (uri != null) {-                        val intent = Intent(Intent.ACTION_VIEW).apply {-                            setDataAndType(uri, attachment.mimeType.ifBlank { "*/*" })-                            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)-                        }-                        try {-                            context.startActivity(Intent.createChooser(intent, null))-                        } catch (e: Exception) {-                            AppMessages.show(resources.getString(R.string.no_app_can_open_this_file))-                        }-                    }-                }-            )-        } else {-            IconButton(onClick = {-                downloading = true-                actions.downloadAttachment(attachment, { path ->-                    downloading = false-                    localPath = path-                    AppMessages.show(resources.getString(R.string.saved_to_downloads))-                }, { error ->-                    downloading = false-                    AppMessages.show(resources.getString(R.string.download_failed, error))-                })-            }, enabled = !downloading) {-                if (downloading) CircularProgressIndicator(modifier = Modifier.size(16.dp))-                else IconDownload(modifier = Modifier.size(16.dp))-            }-        }-    }-}--@OptIn(ExperimentalMaterial3Api::class, kotlinx.coroutines.FlowPreview::class)-@Composable-fun ComposerScreen(-    viewModel: EmailViewModel,-    initialTo: String = "",-    initialSubject: String = "",-    initialBody: String = "",-    inReplyTo: String? = null,-    references: String? = null,-    draftId: Long? = null,-    onBack: () -> Unit,-) {-    val accounts by viewModel.accounts.collectAsStateWithLifecycle(emptyList())-    val selectedAccount by viewModel.selectedAccount.collectAsStateWithLifecycle()-    val context = LocalContext.current-    val resources = LocalResources.current--    var fromAccount by remember(selectedAccount, accounts) {-        mutableStateOf(selectedAccount ?: accounts.firstOrNull())-    }--    var to by remember { mutableStateOf(initialTo) }-    var cc by remember { mutableStateOf("") }-    var bcc by remember { mutableStateOf("") }-    var showCcBcc by remember { mutableStateOf(false) }-    var subject by remember { mutableStateOf(initialSubject) }-    val bodyController = remember { com.vayunmathur.email.composer.EmailHtmlEditorController(initialBody) }-    var sending by remember { mutableStateOf(false) }-    var attachments by remember { mutableStateOf<List<Uri>>(emptyList()) }--    var showAccountPicker by remember { mutableStateOf(false) }-    var showSchedule by remember { mutableStateOf(false) }--    var pickTarget by remember { mutableStateOf(0) }-    var pickTick by remember { mutableStateOf(0) }-    val contactPicker = rememberLauncherForActivityResult(-        ActivityResultContracts.StartActivityForResult()-    ) { result ->-        if (result.resultCode == android.app.Activity.RESULT_OK) {-            val email = result.data?.data?.let { contactEmail(context, it) }-            if (!email.isNullOrBlank()) {-                when (pickTarget) {-                    0 -> to = appendRecipient(to, email)-                    1 -> cc = appendRecipient(cc, email)-                    2 -> bcc = appendRecipient(bcc, email)-                }-                pickTick++-            }-        }-    }-    LaunchedEffect(pickTick) {-        if (pickTick > 0) {-            contactPicker.launch(-                Intent(Intent.ACTION_PICK, android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI)-            )-        }-    }-    val pickContact = { target: Int ->-        pickTarget = target-        pickTick++-    }--    var currentDraftId by remember { mutableStateOf(draftId) }-    var draftLoaded by remember { mutableStateOf(draftId == null) }-    LaunchedEffect(draftId) {-        if (draftId != null) {-            viewModel.loadDraft(draftId)?.let { d ->-                to = d.to; cc = d.cc; bcc = d.bcc-                if (d.cc.isNotBlank() || d.bcc.isNotBlank()) showCcBcc = true-                subject = d.subject; bodyController.setHtml(d.body)-                accounts.firstOrNull { it.email == d.accountEmail }?.let { fromAccount = it }-            }-            draftLoaded = true-        }-    }--    var appliedSignature by remember { mutableStateOf("") }-    LaunchedEffect(fromAccount) {-        if (draftId != null) return@LaunchedEffect-        val block = signatureBlockHtml(fromAccount)-        if (block != appliedSignature) {-            val t = bodyController.html-            val newText = when {-                appliedSignature.isEmpty() -> t + block-                t.endsWith(appliedSignature) -> t.removeSuffix(appliedSignature) + block-                else -> t + block-            }-            bodyController.setHtml(newText)-            appliedSignature = block-        }-    }--    LaunchedEffect(fromAccount, draftLoaded) {-        val acc = fromAccount-        if (!draftLoaded || acc == null) return@LaunchedEffect-        snapshotFlow { listOf(to, cc, bcc, subject, bodyController.html) }-            .debounce(800)-            .collect {-                val hasContent = to.isNotBlank() || cc.isNotBlank() || bcc.isNotBlank() ||-                    subject.isNotBlank() || bodyController.html.isNotBlank()-                if (hasContent) {-                    viewModel.saveDraft(currentDraftId, acc.email, to, cc, bcc, subject, bodyController.html) { id ->-                        currentDraftId = id-                    }-                }-            }-    }--    // Clean up orphan inline images on text change-    LaunchedEffect(bodyController.html) {-        bodyController.inlineCleanupOrphans()-    }--    // Attachment launchers-    val attachmentLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) { uris ->-        if (uris.isNotEmpty()) attachments = attachments + uris-    }--    // Inline image pickers: use PickVisualMedia + fallback GetContent image/*-    val inlineFallbackLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->-        if (uri != null) {-            val mime = context.contentResolver.getType(uri) ?: "image/jpeg"-            val name = uriName(context, uri)-            // Copy to cache/inline for stable uri-            val cached = copyInlineToCache(context, uri, name)-            val finalUri = cached ?: uri-            bodyController.insertInlineImage(context, finalUri, mime, name)-        }-    }--    // Try to use PickVisualMedia if available-    var visualMediaLauncher: Any? = null-    val pickVisualLauncher = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->-        if (uri != null) {-            val mime = context.contentResolver.getType(uri) ?: "image/jpeg"-            val name = uriName(context, uri)-            val cached = copyInlineToCache(context, uri, name)-            val finalUri = cached ?: uri-            bodyController.insertInlineImage(context, finalUri, mime, name)-        }-    }--    val pickMultipleVisualLauncher = rememberLauncherForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(5)) { uris ->-        if (uris.isNotEmpty()) {-            uris.forEach { uri ->-                val mime = context.contentResolver.getType(uri) ?: "image/jpeg"-                val name = uriName(context, uri)-                val cached = copyInlineToCache(context, uri, name)-                val finalUri = cached ?: uri-                bodyController.insertInlineImage(context, finalUri, mime, name)-            }-        }-    }--    Scaffold(-        topBar = {-            TopAppBar(-                title = { Text(stringResource(R.string.compose)) },-                navigationIcon = { IconNavigation(onBack) },-                actions = {-                    IconButton(onClick = { attachmentLauncher.launch("*/*") }) {-                        IconAttachment()-                    }-                    IconButton(onClick = {-                        // Prefer new photo picker-                        try {-                            pickVisualLauncher.launch(androidx.activity.result.PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))-                        } catch (_: Exception) {-                            inlineFallbackLauncher.launch("image/*")-                        }-                    }) {-                        com.vayunmathur.library.ui.IconImage()-                    }-                    Box {-                        TextButton(onClick = { showSchedule = true }, enabled = fromAccount != null) {-                            Text(stringResource(R.string.later))-                        }-                        DropdownMenu(expanded = showSchedule, onDismissRequest = { showSchedule = false }) {-                            val schedule = { at: Long ->-                                showSchedule = false-                                fromAccount?.let { acc ->-                                    viewModel.scheduleSend(-                                        account = acc, to = to, subject = subject,-                                        body = bodyController.html,-                                        asHtml = true,-                                        cc = cc.ifBlank { null }, bcc = bcc.ifBlank { null },-                                        attachments = attachments,-                                        inlineImages = bodyController.toInlineAttachments(),-                                        inReplyTo = inReplyTo,-                                        references = references, scheduledAt = at,-                                    ) { currentDraftId?.let { viewModel.deleteDraft(it) } }-                                    AppMessages.show(resources.getString(R.string.scheduled))-                                    onBack()-                                }-                            }-                            DropdownMenuItem(text = { Text(stringResource(R.string.in_1_hour)) }, onClick = { schedule(System.currentTimeMillis() + 3_600_000L) })-                            DropdownMenuItem(text = { Text(stringResource(R.string.this_evening_6_pm)) }, onClick = { schedule(scheduleTime(18, sameDay = true)) })-                            DropdownMenuItem(text = { Text(stringResource(R.string.tomorrow_8_am)) }, onClick = { schedule(scheduleTime(8, sameDay = false)) })-                        }-                    }-                    IconButton(onClick = {-                        val acc = fromAccount ?: return@IconButton-                        sending = true-                        viewModel.sendEmailFrom(-                            account = acc,-                            to = to,-                            subject = subject,-                            body = bodyController.html,-                            asHtml = true,-                            cc = cc.ifBlank { null },-                            bcc = bcc.ifBlank { null },-                            attachments = attachments,-                            inlineImages = bodyController.toInlineAttachments(),-                            inReplyTo = inReplyTo,-                            references = references,-                            onSuccess = {-                                sending = false-                                currentDraftId?.let { viewModel.deleteDraft(it) }-                                AppMessages.show(resources.getString(R.string.message_sent))-                                onBack()-                            },-                            onError = { err ->-                                sending = false-                                AppMessages.show(resources.getString(R.string.saved_to_outbox, err))-                                onBack()-                            }-                        )-                    }, enabled = !sending && fromAccount != null) {-                        if (sending) CircularProgressIndicator(modifier = Modifier.size(24.dp))-                        else IconSend()-                    }-                }-            )-        },-        bottomBar = {-            // Always show formatting toolbar? Per plan, only when focused, but now scrollable and more buttons-            if (bodyController.focused) {-                EmailComposerFormatToolbar(controller = bodyController,-                    onInsertImage = {-                        try {-                            pickVisualLauncher.launch(androidx.activity.result.PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))-                        } catch (_: Exception) {-                            inlineFallbackLauncher.launch("image/*")-                        }-                    })-            }-        }-    ) { padding ->-        Column(modifier = Modifier.padding(padding).padding(horizontal = 16.dp, vertical = 8.dp).fillMaxSize(), verticalArrangement = Arrangement.spacedBy(4.dp)) {-            Surface(-                onClick = { showAccountPicker = true },-                shape = androidx.compose.foundation.shape.RoundedCornerShape(8.dp),-                tonalElevation = 1.dp,-                modifier = Modifier.fillMaxWidth(),-            ) {-                Row(-                    modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp),-                    verticalAlignment = Alignment.CenterVertically,-                ) {-                    Text(-                        "${stringResource(R.string.from_label)}: ",-                        style = MaterialTheme.typography.labelMedium,-                        color = MaterialTheme.colorScheme.onSurfaceVariant,-                    )-                    Text(-                        fromAccount?.email ?: stringResource(R.string.select_account),-                        style = MaterialTheme.typography.bodyMedium,-                        maxLines = 1,-                        overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis,-                        modifier = Modifier.weight(1f),-                    )-                    IconChevronRight()-                }-            }--            if (showAccountPicker) {-                AlertDialog(-                    onDismissRequest = { showAccountPicker = false },-                    confirmButton = {},-                    title = { Text(stringResource(R.string.select_sender)) },-                    text = {-                        Column {-                            accounts.forEach { acc ->-                                ListItem(-                                    content = { Text(acc.email) },-                                    modifier = Modifier.clickable {-                                        fromAccount = acc-                                        showAccountPicker = false-                                    }-                                )-                            }-                        }-                    }-                )-            }--            Row(verticalAlignment = Alignment.CenterVertically) {-                OutlinedTextField(-                    value = to, onValueChange = { to = it },-                    label = { Text(stringResource(R.string.to_label)) },-                    trailingIcon = { IconButton(onClick = { pickContact(0) }) { com.vayunmathur.library.ui.IconAdd() } },-                    singleLine = true,-                    textStyle = MaterialTheme.typography.bodyMedium,-                    modifier = Modifier.weight(1f),-                )-                TextButton(onClick = { showCcBcc = !showCcBcc }) { Text(stringResource(R.string.cc_bcc)) }-            }-            if (showCcBcc) {-                OutlinedTextField(-                    value = cc, onValueChange = { cc = it }, label = { Text(stringResource(R.string.cc)) },-                    trailingIcon = { IconButton(onClick = { pickContact(1) }) { com.vayunmathur.library.ui.IconAdd() } },-                    singleLine = true,-                    textStyle = MaterialTheme.typography.bodyMedium,-                    modifier = Modifier.fillMaxWidth(),-                )-                OutlinedTextField(-                    value = bcc, onValueChange = { bcc = it }, label = { Text(stringResource(R.string.bcc)) },-                    trailingIcon = { IconButton(onClick = { pickContact(2) }) { com.vayunmathur.library.ui.IconAdd() } },-                    singleLine = true,-                    textStyle = MaterialTheme.typography.bodyMedium,-                    modifier = Modifier.fillMaxWidth(),-                )-            }-            OutlinedTextField(-                value = subject, onValueChange = { subject = it },-                label = { Text(stringResource(R.string.subject_label)) },-                singleLine = true,-                textStyle = MaterialTheme.typography.bodyMedium,-                modifier = Modifier.fillMaxWidth(),-            )--            com.vayunmathur.email.composer.EmailHtmlEditor(-                controller = bodyController,-                placeholder = stringResource(R.string.body_label),-                modifier = Modifier.fillMaxWidth().weight(1f),-            )--            // Inline images thumbnail row (text-based to avoid heavy deps; WYSIWYG preview already in editor)-            if (bodyController.inlineImages.isNotEmpty()) {-                val inlineTotal = bodyController.inlineImages.sumOf { uriSize(context, it.localUri) }-                Text(stringResource(R.string.inline_images, bodyController.inlineImages.size, android.text.format.Formatter.formatShortFileSize(context, inlineTotal)), style = MaterialTheme.typography.labelSmall)-                androidx.compose.foundation.lazy.LazyRow(-                    horizontalArrangement = Arrangement.spacedBy(8.dp),-                    modifier = Modifier.fillMaxWidth()-                ) {-                    items(bodyController.inlineImages.size) { idx ->-                        val img = bodyController.inlineImages[idx]-                        Card(modifier = Modifier.size(96.dp)) {-                            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {-                                Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {-                                    com.vayunmathur.library.ui.IconImage(modifier = Modifier.size(24.dp))-                                    Text(img.fileName.take(16), style = MaterialTheme.typography.labelSmall, maxLines = 1, overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis)-                                }-                                IconButton(onClick = { bodyController.removeInlineImage(img.cid) }, modifier = Modifier.align(Alignment.TopEnd).size(20.dp)) {-                                    com.vayunmathur.library.ui.IconClose(modifier = Modifier.size(12.dp))-                                }-                            }-                        }-                    }-                }-            }--            if (attachments.isNotEmpty() || bodyController.inlineImages.isNotEmpty()) {-                val totalBytes = attachments.sumOf { uriSize(context, it) } + bodyController.inlineImages.sumOf { uriSize(context, it.localUri) }-                Text(stringResource(R.string.attachments_1, android.text.format.Formatter.formatShortFileSize(context, totalBytes)), style = MaterialTheme.typography.labelLarge)-                if (totalBytes > 25L * 1024 * 1024) {-                    Text(-                        stringResource(R.string.total_attachment_size_exceeds_25_mb_many),-                        style = MaterialTheme.typography.bodySmall,-                        color = MaterialTheme.colorScheme.error,-                    )-                }-            }--            if (attachments.isNotEmpty()) {-                attachments.forEach { uri ->-                    val attachmentLabel = remember(uri) {-                        "${uriName(context, uri)} · " + android.text.format.Formatter.formatShortFileSize(context, uriSize(context, uri))-                    }-                    Row(-                        modifier = Modifier.fillMaxWidth(),-                        verticalAlignment = Alignment.CenterVertically,-                    ) {-                        IconAttachment(modifier = Modifier.size(16.dp))-                        Spacer(Modifier.width(8.dp))-                        Text(-                            attachmentLabel,-                            style = MaterialTheme.typography.bodySmall,-                            maxLines = 1,-                            overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis,-                            modifier = Modifier.weight(1f),-                        )-                        IconButton(onClick = { attachments = attachments - uri }) {-                            com.vayunmathur.library.ui.IconClose(modifier = Modifier.size(16.dp))-                        }-                    }-                }-            }-        }-    }-}--@Composable-private fun EmailComposerFormatToolbar(-    controller: com.vayunmathur.email.composer.EmailHtmlEditorController,-    onInsertImage: () -> Unit,-) {-    var headingMenu by remember { mutableStateOf(false) }-    var alignMenu by remember { mutableStateOf(false) }-    var colorDialog by remember { mutableStateOf(0) } // 0 none, 1 fg, 2 bg-    var sizeMenu by remember { mutableStateOf(false) }-    var fontMenu by remember { mutableStateOf(false) }--    val headingLevel = controller.getCurrentHeadingLevel()-    val headingLabel = when (headingLevel) {-        1 -> "H1"; 2 -> "H2"; 3 -> "H3"; else -> "Normal"-    }-    val alignCss = controller.getCurrentAlignment()-    val blockquoteActive = controller.isBlockquoteActive()-    val codeActive = controller.isInlineCodeActive()--    com.vayunmathur.library.ui.EditorBottomBar(modifier = Modifier, scrollable = true) {-        // Heading dropdown-        Box {-            TextButton(onClick = { headingMenu = true }) {-                Text(headingLabel)-                IconArrowDropDown()-            }-            DropdownMenu(expanded = headingMenu, onDismissRequest = { headingMenu = false }) {-                DropdownMenuItem(text = { Text(stringResource(R.string.normal)) }, onClick = { headingMenu = false; controller.toggleHeading(null) })-                DropdownMenuItem(text = { Text(stringResource(R.string.heading_1)) }, onClick = { headingMenu = false; controller.toggleHeading(1) })-                DropdownMenuItem(text = { Text(stringResource(R.string.heading_2)) }, onClick = { headingMenu = false; controller.toggleHeading(2) })-                DropdownMenuItem(text = { Text(stringResource(R.string.heading_3)) }, onClick = { headingMenu = false; controller.toggleHeading(3) })-            }-        }--        com.vayunmathur.library.ui.EditorBaseButtons(formatter = controller)--        HorizontalDivider(modifier = Modifier.height(24.dp).width(1.dp))--        // Alignment dropdown-        Box {-            val alignIcon: @Composable () -> Unit = when (alignCss) {-                "center" -> { { IconFormatAlignCenter() } }-                "right" -> { { IconFormatAlignRight() } }-                "justify" -> { { IconFormatAlignJustify() } }-                else -> { { IconFormatAlignLeft() } }-            }-            com.vayunmathur.library.ui.FormatIconButton(-                contentDescription = stringResource(R.string.alignment),-                active = alignCss != null,-                onClick = { alignMenu = true },-                icon = alignIcon,-            )-            DropdownMenu(expanded = alignMenu, onDismissRequest = { alignMenu = false }) {-                DropdownMenuItem(-                    text = { Text(stringResource(R.string.left)) }, leadingIcon = { IconFormatAlignLeft() },-                    onClick = { alignMenu = false; controller.setAlignment(null) }-                )-                DropdownMenuItem(-                    text = { Text(stringResource(R.string.center)) }, leadingIcon = { IconFormatAlignCenter() },-                    onClick = { alignMenu = false; controller.setAlignment("center") }-                )-                DropdownMenuItem(-                    text = { Text(stringResource(R.string.right)) }, leadingIcon = { IconFormatAlignRight() },-                    onClick = { alignMenu = false; controller.setAlignment("right") }-                )-                DropdownMenuItem(-                    text = { Text(stringResource(R.string.justify)) }, leadingIcon = { IconFormatAlignJustify() },-                    onClick = { alignMenu = false; controller.setAlignment("justify") }-                )-            }-        }--        // Blockquote, code, hr, clear-        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.blockquote),-            active = blockquoteActive,-            onClick = { controller.toggleBlockquote() },-        ) { IconFormatQuote() }--        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.inline_code),-            active = codeActive,-            onClick = { controller.toggleInlineCode() },-        ) { IconFormatCode() }--        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.horizontal_rule),-            onClick = { controller.insertHorizontalRule() },-        ) { IconFormatHorizontalRule() }--        HorizontalDivider(modifier = Modifier.height(24.dp).width(1.dp))--        // Text color-        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.text_color),-            active = colorDialog == 1,-            onClick = { colorDialog = 1 },-        ) { IconFormatColorText() }--        // Highlight / background-        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.highlight),-            active = colorDialog == 2,-            onClick = { colorDialog = 2 },-        ) { IconFormatColorFill() }--        // Font size dropdown-        Box {-            com.vayunmathur.library.ui.FormatIconButton(-                contentDescription = stringResource(R.string.font_size),-                onClick = { sizeMenu = true },-            ) { IconFormatSize() }-            DropdownMenu(expanded = sizeMenu, onDismissRequest = { sizeMenu = false }) {-                DropdownMenuItem(text = { Text(stringResource(R.string.small)) }, onClick = { sizeMenu = false; controller.setFontSizeFactor(0.9f) })-                DropdownMenuItem(text = { Text(stringResource(R.string.normal)) }, onClick = { sizeMenu = false; controller.setFontSizeFactor(null) })-                DropdownMenuItem(text = { Text(stringResource(R.string.large)) }, onClick = { sizeMenu = false; controller.setFontSizeFactor(1.2f) })-                DropdownMenuItem(text = { Text(stringResource(R.string.extra_large)) }, onClick = { sizeMenu = false; controller.setFontSizeFactor(1.4f) })-            }-        }--        // Font family dropdown-        Box {-            com.vayunmathur.library.ui.FormatIconButton(-                contentDescription = stringResource(R.string.font_family),-                onClick = { fontMenu = true },-            ) { IconFormatTitle() }-            DropdownMenu(expanded = fontMenu, onDismissRequest = { fontMenu = false }) {-                DropdownMenuItem(text = { Text(stringResource(R.string.font_default)) }, onClick = { fontMenu = false; controller.setFontFamily(null) })-                DropdownMenuItem(text = { Text(stringResource(R.string.sans)) }, onClick = { fontMenu = false; controller.setFontFamily("sans-serif") })-                DropdownMenuItem(text = { Text(stringResource(R.string.serif)) }, onClick = { fontMenu = false; controller.setFontFamily("serif") })-                DropdownMenuItem(text = { Text(stringResource(R.string.monospace)) }, onClick = { fontMenu = false; controller.setFontFamily("monospace") })-            }-        }--        HorizontalDivider(modifier = Modifier.height(24.dp).width(1.dp))--        // Clear formatting-        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.clear_formatting),-            onClick = { controller.clearFormatting() },-        ) { IconFormatClear() }--        // Image button-        com.vayunmathur.library.ui.FormatIconButton(-            contentDescription = stringResource(R.string.cd_insert_image),-            onClick = onInsertImage,-        ) { com.vayunmathur.library.ui.IconImage() }-    }--    // Color picker dialog-    if (colorDialog != 0) {-        val isForeground = colorDialog == 1-        EmailColorPickerDialog(-            title = if (isForeground) "Text color" else "Highlight",-            onDismiss = { colorDialog = 0 },-            onColorSelected = { colorInt ->-                if (isForeground) {-                    if (colorInt == null) controller.setTextColor(null) else controller.setTextColor(colorInt)-                } else {-                    if (colorInt == null) controller.setHighlight(null) else controller.setHighlight(colorInt)-                }-                colorDialog = 0-            }-        )-    }-}--@Composable-private fun EmailColorPickerDialog(-    title: String,-    onDismiss: () -> Unit,-    onColorSelected: (Int?) -> Unit,-) {-    val palette = remember {-        listOf(-            null to "Default",-            android.graphics.Color.BLACK to "Black",-            "#D32F2F".toColorInt() to "Red",-            "#1976D2".toColorInt() to "Blue",-            "#388E3C".toColorInt() to "Green",-            "#F57C00".toColorInt() to "Orange",-            "#7B1FA2".toColorInt() to "Purple",-            "#00796B".toColorInt() to "Teal",-            "#455A64".toColorInt() to "Gray",-            "#FFEB3B".toColorInt() to "Yellow",-            "#FFCDD2".toColorInt() to "Light Red",-            "#BBDEFB".toColorInt() to "Light Blue",-            "#C8E6C9".toColorInt() to "Light Green",-            "#FFF9C4".toColorInt() to "Light Yellow",-        )-    }-    AlertDialog(-        onDismissRequest = onDismiss,-        title = { Text(title) },-        text = {-            Column(-                modifier = Modifier.verticalScroll(rememberScrollState()),-                verticalArrangement = Arrangement.spacedBy(4.dp),-            ) {-                // chunk into rows of 4-                palette.chunked(4).forEach { rowItems ->-                    Row(-                        horizontalArrangement = Arrangement.spacedBy(8.dp),-                        modifier = Modifier.fillMaxWidth(),-                    ) {-                        rowItems.forEach { (colorInt, label) ->-                            Box(-                                modifier = Modifier-                                    .size(48.dp)-                                    .background(-                                        color = if (colorInt == null) MaterialTheme.colorScheme.surfaceVariant-                                        else Color(colorInt),-                                        shape = androidx.compose.foundation.shape.CircleShape,-                                    )-                                    .clickable { onColorSelected(colorInt) },-                                contentAlignment = Alignment.Center,-                            ) {-                                if (colorInt == null) {-                                    IconClose(modifier = Modifier.size(20.dp))-                                }-                            }-                        }-                    }-                }-            }-        },-        confirmButton = {},-        dismissButton = {-            TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.cancel)) }-        }-    )-}--private fun copyInlineToCache(context: android.content.Context, uri: Uri, name: String): Uri? {-    return try {-        val dir = java.io.File(context.cacheDir, "inline").also { it.mkdirs() }-        val safeName = name.replace(Regex("[/\\\\]"), "_").ifBlank { "image_${System.currentTimeMillis()}.jpg" }-        val outFile = java.io.File(dir, "${System.currentTimeMillis()}-$safeName")-        context.contentResolver.openInputStream(uri)?.use { input ->-            outFile.outputStream().use { out -> input.copyTo(out) }-        } ?: return null-        androidx.core.content.FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", outFile)-    } catch (_: Exception) { null }-}----@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun OutboxScreen(-    viewModel: EmailViewModel,-    onBack: () -> Unit,-) {-    val outbox by viewModel.outbox.collectAsStateWithLifecycle(emptyList())-    val context = LocalContext.current-    val resources = LocalResources.current--    Scaffold(-        topBar = {-            TopAppBar(-                title = { Text(stringResource(R.string.outbox)) },-                navigationIcon = { IconNavigation(onBack) },-                actions = {-                    if (outbox.isNotEmpty()) {-                        TextButton(onClick = {-                            viewModel.sendOutboxNow(context)-                            AppMessages.show(resources.getQuantityString(R.plurals.retrying_pending_messages, outbox.size, outbox.size))-                        }) { Text(stringResource(R.string.send_now)) }-                    }-                },-            )-        },-    ) { padding ->-        if (outbox.isEmpty()) {-            EmptyState(-                title = stringResource(R.string.outbox_is_empty),-                modifier = Modifier.padding(padding).fillMaxSize(),-            )-        } else {-            LazyColumn(-                modifier = Modifier.padding(padding).fillMaxSize(),-            ) {-                items(outbox, key = { it.id }) { entry ->-                    OutboxRow(-                        entry = entry,-                        onDelete = {-                            viewModel.deleteOutboxEntry(entry)-                            AppMessages.show(resources.getString(R.string.deleted_from_outbox))-                        },-                    )-                    HorizontalDivider()-                }-            }-        }-    }-}--@Composable-private fun OutboxRow(entry: OutboxEntry, onDelete: () -> Unit) {-    Column(-        modifier = Modifier-            .fillMaxWidth()-            .padding(horizontal = 16.dp, vertical = 12.dp),-    ) {-        Row(verticalAlignment = Alignment.CenterVertically) {-            Column(modifier = Modifier.weight(1f)) {-                Text(-                    entry.subject.ifBlank { stringResource(R.string.no_subject) },-                    style = MaterialTheme.typography.titleMedium,-                    fontWeight = FontWeight.SemiBold,-                )-                Text(-                    stringResource(R.string.from, entry.accountEmail),-                    style = MaterialTheme.typography.bodySmall,-                    color = MaterialTheme.colorScheme.onSurfaceVariant,-                )-                Text(-                    stringResource(R.string.to_1, entry.to),-                    style = MaterialTheme.typography.bodySmall,-                    color = MaterialTheme.colorScheme.onSurfaceVariant,-                )-            }-            IconButton(onClick = onDelete) { IconDelete() }-        }-        if (entry.body.isNotBlank()) {-            Spacer(Modifier.height(4.dp))-            Text(-                entry.body.lineSequence().firstOrNull().orEmpty(),-                style = MaterialTheme.typography.bodyMedium,-                maxLines = 2,-            )-        }-        if (entry.lastError != null || entry.attemptCount > 0) {-            Spacer(Modifier.height(8.dp))-            val statusColor =-                if (entry.lastError != null) MaterialTheme.colorScheme.error-                else MaterialTheme.colorScheme.primary-            val failedText = if (entry.lastError != null) stringResource(R.string.failed, entry.lastError) else ""-            val attemptText = if (entry.attemptCount > 0) pluralStringResource(R.plurals.attempts, entry.attemptCount, entry.attemptCount) else ""-            Text(-                buildString {-                    if (failedText.isNotEmpty()) append(failedText)-                    if (attemptText.isNotEmpty()) {-                        if (isNotEmpty()) append(" · ")-                        append(attemptText)-                    }-                },-                style = MaterialTheme.typography.bodySmall,-                color = statusColor,-            )-        }-    }-}--/** The signature block (HTML) appended to an outgoing message body, or "" if none. */-private fun signatureBlockHtml(acc: com.vayunmathur.email.EmailAccount?): String {-    val s = acc?.signature?.trim().orEmpty()-    if (s.isEmpty()) return ""-    val escaped = s.htmlEncode().replace("\n", "<br>")-    return "<br><br>-- <br>$escaped"-}--@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun SettingsScreen(viewModel: EmailViewModel, onBack: () -> Unit) {-    val accounts by viewModel.accounts.collectAsStateWithLifecycle(emptyList())-    val context = LocalContext.current-    val resources = LocalResources.current-    Scaffold(-        topBar = {-            TopAppBar(-                title = { Text(stringResource(UiR.string.settings)) },-                navigationIcon = { IconNavigation(onBack) },-            )-        }-    ) { padding ->-        Column(-            modifier = Modifier-                .padding(padding)-                .padding(16.dp)-                .fillMaxSize()-                .verticalScroll(rememberScrollState()),-            verticalArrangement = Arrangement.spacedBy(16.dp),-        ) {-            val settings = remember(context) { com.vayunmathur.email.data.EmailSettings.get(context) }-            val loadRemoteImages by settings.loadRemoteImages.collectAsStateWithLifecycle()-            Text(stringResource(R.string.reading), style = MaterialTheme.typography.titleMedium)-            com.vayunmathur.library.ui.SettingsSwitchRow(-                title = stringResource(R.string.load_remote_images),-                supportingText = stringResource(R.string.load_remote_images_summary),-                checked = loadRemoteImages,-                onCheckedChange = { settings.setLoadRemoteImages(it) },-            )-            HorizontalDivider()--            Text(stringResource(R.string.signatures), style = MaterialTheme.typography.titleMedium)-            if (accounts.isEmpty()) {-                Text(stringResource(R.string.select_account))-            }-            accounts.forEach { acc ->-                var sig by remember(acc.email) { mutableStateOf(acc.signature) }-                Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {-                    Text(acc.email, style = MaterialTheme.typography.labelLarge)-                    OutlinedTextField(-                        value = sig,-                        onValueChange = { sig = it },-                        modifier = Modifier.fillMaxWidth(),-                        minLines = 3,-                        placeholder = { Text(stringResource(R.string.your_signature)) },-                    )-                    Button(-                        onClick = {-                            viewModel.setSignature(acc.email, sig)-                            AppMessages.show(resources.getString(R.string.signature_saved))-                        },-                        modifier = Modifier.align(Alignment.End),-                    ) {-                        Text(stringResource(UiR.string.save))-                    }-                }-                HorizontalDivider()-            }--            val blocked by viewModel.blockedSenders.collectAsStateWithLifecycle(emptyList())-            Text(stringResource(R.string.blocked_senders), style = MaterialTheme.typography.titleMedium)-            if (blocked.isEmpty()) {-                Text(stringResource(R.string.none), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)-            }-            blocked.forEach { b ->-                ListItem(-                    content = { Text(b.address) },-                    trailingContent = {-                        TextButton(onClick = { viewModel.unblockSender(b.address) }) { Text(stringResource(R.string.unblock)) }-                    },-                )-                HorizontalDivider()-            }-        }-    }-}--@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun DraftsScreen(-    viewModel: EmailViewModel,-    onBack: () -> Unit,-    onOpenDraft: (Long) -> Unit,-) {-    val drafts by viewModel.drafts.collectAsStateWithLifecycle(emptyList())-    Scaffold(-        topBar = {-            TopAppBar(-                title = { Text(stringResource(R.string.drafts)) },-                navigationIcon = { IconNavigation(onBack) },-            )-        }-    ) { padding ->-        if (drafts.isEmpty()) {-            EmptyState(-                title = stringResource(R.string.no_drafts),-                modifier = Modifier.fillMaxSize().padding(padding),-            )-        } else {-            LazyColumn(modifier = Modifier.fillMaxSize().padding(padding)) {-                items(drafts, key = { it.id }) { d ->-                    ListItem(-                        content = { Text(d.subject.ifBlank { stringResource(R.string.no_subject) }) },-                        supportingContent = {-                            val prefix = if (d.to.isNotBlank()) "To: ${d.to}  " else ""-                            Text(prefix + d.body.replace("\n", " ").take(80), maxLines = 2)-                        },-                        modifier = Modifier.clickable { onOpenDraft(d.id) },-                        trailingContent = {-                            IconButton(onClick = { viewModel.deleteDraft(d.id) }) {-                                com.vayunmathur.library.ui.IconDelete()-                            }-                        },-                    )-                    HorizontalDivider()-                }-            }-        }-    }-}--private fun uriName(context: android.content.Context, uri: android.net.Uri): String =-    runCatching {-        context.contentResolver.query(uri, arrayOf(android.provider.OpenableColumns.DISPLAY_NAME), null, null, null)?.use {-            if (it.moveToFirst()) it.getString(0) else null-        }-    }.getOrNull() ?: uri.lastPathSegment ?: "attachment"--private fun uriSize(context: android.content.Context, uri: android.net.Uri): Long =-    runCatching {-        context.contentResolver.query(uri, arrayOf(android.provider.OpenableColumns.SIZE), null, null, null)?.use {-            if (it.moveToFirst() && !it.isNull(0)) it.getLong(0) else 0L-        }-    }.getOrNull() ?: 0L--/** Epoch millis for [hour]:00 today (or tomorrow if that time already passed, or sameDay=false forces next day). */-private fun scheduleTime(hour: Int, sameDay: Boolean): Long {-    val c = java.util.Calendar.getInstance()-    c.set(java.util.Calendar.HOUR_OF_DAY, hour)-    c.set(java.util.Calendar.MINUTE, 0)-    c.set(java.util.Calendar.SECOND, 0)-    c.set(java.util.Calendar.MILLISECOND, 0)-    if (!sameDay || c.timeInMillis <= System.currentTimeMillis()) {-        c.add(java.util.Calendar.DAY_OF_YEAR, 1)-    }-    return c.timeInMillis-}--/** Append an email to a comma-separated recipient field, avoiding duplicates. */-private fun appendRecipient(field: String, email: String): String {-    val existing = field.split(",").map { it.trim() }.filter { it.isNotEmpty() }-    if (existing.any { it.equals(email, ignoreCase = true) }) return field-    return if (existing.isEmpty()) email else existing.joinToString(", ") + ", " + email-}--/** Read the email address from a contact-picker result URI (granted per-item, no permission needed). */-private fun contactEmail(context: android.content.Context, uri: android.net.Uri): String? =-    runCatching {-        context.contentResolver.query(-            uri,-            arrayOf(android.provider.ContactsContract.CommonDataKinds.Email.ADDRESS),-            null, null, null,-        )?.use { if (it.moveToFirst()) it.getString(0) else null }-    }.getOrNull()--/**- * Split a plain-text body into (visibleText, quotedText). Quoted text starts at- * the first reply boundary ("On … wrote:", "-----Original Message-----") or a- * run of '>'-prefixed lines. Returns empty quotedText when nothing is quoted.- */-private fun splitQuotedText(body: String): Pair<String, String> {-    val lines = body.split("\n")-    val onWrote = Regex("^On .+ wrote:\\s*$")-    val origMsg = Regex("^-{2,}\\s*Original Message\\s*-{2,}\\s*$", RegexOption.IGNORE_CASE)-    for (i in lines.indices) {-        val line = lines[i].trim()-        val isBoundary = onWrote.matches(line) || origMsg.matches(line) || line.startsWith(">")-        if (isBoundary && i > 0) {-            val main = lines.subList(0, i).joinToString("\n").trimEnd()-            val quoted = lines.subList(i, lines.size).joinToString("\n")-            return main to quoted-        }-    }-    return body to ""-}--/** Confirmation dialog shown before acting on a detected unsubscribe option. */-@Composable-private fun UnsubscribeDialog(-    method: UnsubscribeMethod,-    onDismiss: () -> Unit,-    onConfirm: () -> Unit,-) {-    val (message, confirmLabel) = when (method) {-        is UnsubscribeMethod.OneClickPost ->-            "Send an unsubscribe request to the sender?" to "Unsubscribe"-        is UnsubscribeMethod.OpenWeb ->-            "Open the unsubscribe page in your browser?" to "Open"-        is UnsubscribeMethod.SendMail ->-            "Compose an unsubscribe email to ${method.address}?" to "Compose"-    }-    AlertDialog(-        onDismissRequest = onDismiss,-        title = { Text(stringResource(R.string.unsubscribe)) },-        text = { Text(message) },-        confirmButton = { TextButton(onClick = onConfirm) { Text(confirmLabel) } },-        dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.cancel)) } },-    )-}--/** Act on a confirmed unsubscribe option. */-private fun performUnsubscribe(-    method: UnsubscribeMethod,-    context: android.content.Context,-    actions: MessageThreadActions,-    onCompose: (String, String) -> Unit,-) {-    when (method) {-        is UnsubscribeMethod.OneClickPost -> {-            AppMessages.show(context.getString(R.string.unsubscribing))-            actions.oneClickUnsubscribe(method.url) { ok ->-                val text = if (ok) "Unsubscribed" else "Unsubscribe failed"-                AppMessages.show(text)-            }-        }-        is UnsubscribeMethod.OpenWeb -> {-            val opened = runCatching {-                context.startActivity(Intent(Intent.ACTION_VIEW, method.url.toUri()))-            }.isSuccess-            if (!opened) {-                AppMessages.show(context.getString(R.string.couldn_t_open_unsubscribe_page))-            }-        }-        is UnsubscribeMethod.SendMail -> onCompose(method.address, "Unsubscribe")-    }-}
email/src/main/java/com/vayunmathur/email/Navigation.kt
diff --git a/email/src/main/java/com/vayunmathur/email/Navigation.kt b/email/src/main/java/com/vayunmathur/email/Navigation.ktnew file mode 100644index 0000000000..eb2a5a173f--- /dev/null+++ b/email/src/main/java/com/vayunmathur/email/Navigation.kt@@ -0,0 +1,206 @@+package com.vayunmathur.email++import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.fillMaxHeight+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.foundation.verticalScroll+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.rememberCoroutineScope+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.email.data.accountColor+import com.vayunmathur.email.platform.EmailViewModel+import com.vayunmathur.email.platform.IntentState+import com.vayunmathur.email.ui.AddAccountScreen+import com.vayunmathur.email.ui.ComposerScreen+import com.vayunmathur.email.ui.DraftsScreen+import com.vayunmathur.email.ui.EmlViewerScreen+import com.vayunmathur.email.ui.FolderList+import com.vayunmathur.email.ui.MessageListPage+import com.vayunmathur.email.ui.MessageThreadPage+import com.vayunmathur.email.ui.OutboxScreen+import com.vayunmathur.email.ui.SettingsScreen+import com.vayunmathur.library.ui.DrawerValue+import com.vayunmathur.library.ui.HorizontalDivider+import com.vayunmathur.library.ui.IconAdd+import com.vayunmathur.library.ui.IconEdit+import com.vayunmathur.library.ui.IconInbox+import com.vayunmathur.library.ui.IconMail+import com.vayunmathur.library.ui.IconSend+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.ModalDrawerSheet+import com.vayunmathur.library.ui.ModalNavigationDrawer+import com.vayunmathur.library.ui.NavigationDrawerItem+import com.vayunmathur.library.ui.NavigationDrawerItemDefaults+import com.vayunmathur.library.ui.Surface+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.contentColorOn+import com.vayunmathur.library.ui.rememberDrawerState+import com.vayunmathur.library.util.ListDetailPage+import com.vayunmathur.library.util.ListPage+import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.library.util.openSettingsIfRequested+import com.vayunmathur.library.util.rememberNavBackStack+import kotlinx.coroutines.launch+import com.vayunmathur.library.ui.R as UiR++@Composable+fun Navigation(viewModel: EmailViewModel) {+    val scope = rememberCoroutineScope()+    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)+    val context = LocalContext.current++    val accounts by viewModel.accounts.collectAsStateWithLifecycle(emptyList())+    val selectedAccountEmail by viewModel.selectedAccountEmail.collectAsStateWithLifecycle()+    val folders by viewModel.folders.collectAsStateWithLifecycle(emptyList())+    val selectedFolderName by viewModel.selectedFolderName.collectAsStateWithLifecycle()+    val outbox by viewModel.outbox.collectAsStateWithLifecycle(emptyList())++    val backStack = rememberNavBackStack<Route>(Route.MessageList)+    backStack.openSettingsIfRequested(Route.Settings)++    val navigationRoute = IntentState.navigationRoute+    LaunchedEffect(navigationRoute) {+        if (navigationRoute != null) {+            backStack.add(navigationRoute)+            IntentState.navigationRoute = null+        }+    }++    ModalNavigationDrawer(+        drawerState = drawerState,+        drawerContent = {+            ModalDrawerSheet {+                Column(Modifier.fillMaxHeight()) {+                    Column(+                        Modifier+                            .weight(1f)+                            .verticalScroll(rememberScrollState())+                    ) {+                        Text(stringResource(R.string.unified_inbox), modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)+                        NavigationDrawerItem(+                            label = { Text(stringResource(R.string.all_accounts)) },+                            selected = selectedAccountEmail == null,+                            onClick = {+                                viewModel.selectAccount("")+                                backStack.reset(Route.MessageList)+                                scope.launch { drawerState.close() }+                            },+                            icon = { IconInbox() },+                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)+                        )+                        HorizontalDivider(Modifier.padding(vertical = 8.dp))+                        Text(stringResource(R.string.accounts), modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)+                        accounts.forEach { account ->+                            NavigationDrawerItem(+                                label = { Text(account.email) },+                                selected = account.email == selectedAccountEmail,+                                onClick = {+                                    viewModel.selectAccount(account.email)+                                    backStack.reset(Route.MessageList)+                                    scope.launch { drawerState.close() }+                                },+                                icon = {+                                    val accountColor = Color(accountColor(account.email))+                                    Surface(shape = CircleShape, color = accountColor, modifier = Modifier.size(24.dp)) {+                                        Box(contentAlignment = Alignment.Center) { com.vayunmathur.library.ui.IconMail(modifier = Modifier.size(16.dp), tint = contentColorOn(accountColor)) }+                                    }+                                },+                                modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)+                            )+                        }+                        NavigationDrawerItem(+                            label = { Text(stringResource(R.string.add_account)) },+                            selected = false,+                            onClick = { backStack.add(Route.AddAccount); scope.launch { drawerState.close() } },+                            icon = { IconAdd() },+                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)+                        )+                        if (selectedAccountEmail != null) {+                            HorizontalDivider(Modifier.padding(vertical = 8.dp))+                            Text(stringResource(R.string.folders), modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)+                            FolderList(folders, selectedFolderName) { folderName ->+                                viewModel.selectFolder(folderName)+                                backStack.reset(Route.MessageList)+                                scope.launch { drawerState.close() }+                            }+                        }+                        HorizontalDivider(Modifier.padding(vertical = 8.dp))+                        NavigationDrawerItem(+                            label = { Text(if (outbox.isEmpty()) stringResource(R.string.outbox) else stringResource(R.string.outbox_with_count, outbox.size)) },+                            selected = false,+                            onClick = { backStack.add(Route.Outbox); scope.launch { drawerState.close() } },+                            icon = { IconSend() },+                            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)+                        )+                        NavigationDrawerItem(+                            label = { Text(stringResource(UiR.string.settings)) },+                            selected = false,+                            onClick = { b

Diff truncated.