From 7e921f9cff89f0ce50c8139fb6e8847207a1661f Mon Sep 17 00:00:00 2001 From: krinry Date: Mon, 23 Nov 2026 14:39:04 +0530 Subject: [PATCH] fix: address UI padding, meetings tab rename, add account sheet, swipe action, rich email rendering, and domain logos --- .../com/bansify/mail/data/model/MailModels.kt | 5 +- .../mail/data/repository/MailMappers.kt | 27 ++- .../ui/components/AccountSwitcherDialog.kt | 3 + .../mail/ui/components/AvatarAndBadges.kt | 134 ++++++++++-- .../mail/ui/components/EmailThreadRow.kt | 3 +- .../mail/ui/navigation/MailNavGraph.kt | 16 ++ .../bansify/mail/ui/screens/InboxScreen.kt | 18 +- .../com/bansify/mail/ui/screens/MeetScreen.kt | 4 +- .../bansify/mail/ui/screens/SettingsScreen.kt | 1 + .../mail/ui/screens/SignedOutScreen.kt | 1 + .../mail/ui/screens/ThreadDetailScreen.kt | 196 +++++++++++++++++- app/src/main/res/values/strings.xml | 2 +- 12 files changed, 375 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/bansify/mail/data/model/MailModels.kt b/app/src/main/java/com/bansify/mail/data/model/MailModels.kt index 84ee5ec..240bc0d 100644 --- a/app/src/main/java/com/bansify/mail/data/model/MailModels.kt +++ b/app/src/main/java/com/bansify/mail/data/model/MailModels.kt @@ -109,7 +109,8 @@ data class ThreadMessage( val timestampMillis: Long, val isStarred: Boolean = false, val attachments: List = emptyList(), - val isEncryptedTls: Boolean = true + val isEncryptedTls: Boolean = true, + val bodyHtml: String? = null ) /** @@ -216,7 +217,7 @@ data class AppSettingsEntity( @PrimaryKey val id: Int = 1, val themePreference: ThemePreference = ThemePreference.SYSTEM, val rightSwipeAction: SwipeAction = SwipeAction.ARCHIVE, - val leftSwipeAction: SwipeAction = SwipeAction.DELETE, + val leftSwipeAction: SwipeAction = SwipeAction.ARCHIVE, val compactDensity: Boolean = false, val conversationView: Boolean = true, val confirmBeforeDelete: Boolean = false diff --git a/app/src/main/java/com/bansify/mail/data/repository/MailMappers.kt b/app/src/main/java/com/bansify/mail/data/repository/MailMappers.kt index 03013de..197726b 100644 --- a/app/src/main/java/com/bansify/mail/data/repository/MailMappers.kt +++ b/app/src/main/java/com/bansify/mail/data/repository/MailMappers.kt @@ -96,6 +96,7 @@ object MailMappers { timestampMillis = timestamp, isStarred = message.isStarred, attachments = attachments, + bodyHtml = message.bodyHtml ?: existing?.bodyHtml, ) return EmailThreadEntity( id = EmailThreadEntity.cacheKeyFor(accountEmail, message.id), @@ -134,8 +135,10 @@ object MailMappers { } else { bodyText } - val updatedMessage = existing.messages.firstOrNull()?.copy( + val existingFirst = existing.messages.firstOrNull() + val updatedMessage = existingFirst?.copy( body = body, + bodyHtml = message.bodyHtml ?: existing.bodyHtml, attachments = if (message.attachments.isNotEmpty()) { message.attachments.mapIndexed { index, attachment -> AttachmentItem( @@ -146,8 +149,26 @@ object MailMappers { ) } } else { - existing.messages.first().attachments + existingFirst.attachments }, + ) ?: ThreadMessage( + id = message.id, + senderName = existing.primarySenderName, + senderEmail = existing.primarySenderEmail, + senderColorHex = existing.primarySenderColorHex, + recipientsTo = listOf(existing.accountEmail), + body = body, + bodyHtml = message.bodyHtml ?: existing.bodyHtml, + timestampMillis = existing.updatedAtMillis, + isStarred = message.isStarred, + attachments = message.attachments.mapIndexed { index, attachment -> + AttachmentItem( + id = "${message.id}-att-$index", + name = attachment.name.ifBlank { "attachment" }, + sizeLabel = formatSize(attachment.size), + mimeType = attachment.type.ifBlank { "application/octet-stream" }, + ) + } ) return existing.copy( subject = if (existing.subject == "(no subject)" && message.subject.isNotBlank()) { @@ -158,7 +179,7 @@ object MailMappers { snippet = existing.snippet.ifBlank { message.snippet }, messages = listOfNotNull(updatedMessage), bodyText = body, - bodyHtml = message.bodyHtml, + bodyHtml = message.bodyHtml ?: existing.bodyHtml, etag = etag, bodyLoaded = true, isRead = message.isRead || existing.isRead, diff --git a/app/src/main/java/com/bansify/mail/ui/components/AccountSwitcherDialog.kt b/app/src/main/java/com/bansify/mail/ui/components/AccountSwitcherDialog.kt index 67db96e..84800f3 100644 --- a/app/src/main/java/com/bansify/mail/ui/components/AccountSwitcherDialog.kt +++ b/app/src/main/java/com/bansify/mail/ui/components/AccountSwitcherDialog.kt @@ -97,6 +97,7 @@ fun AccountSwitcherDialog( SenderAvatar( name = activeAccount.displayName, colorHex = activeAccount.avatarColorHex, + email = activeAccount.email, size = 44.dp ) Spacer(modifier = Modifier.width(12.dp)) @@ -186,6 +187,7 @@ fun AccountSwitcherDialog( SenderAvatar( name = account.displayName, colorHex = account.avatarColorHex, + email = account.email, size = 36.dp ) Spacer(modifier = Modifier.width(12.dp)) @@ -240,6 +242,7 @@ fun AccountSwitcherDialog( colorHex = com.bansify.mail.data.repository.MailMappers.avatarColor( discovered.mailboxAddress ), + email = discovered.mailboxAddress, size = 32.dp ) Spacer(modifier = Modifier.width(12.dp)) diff --git a/app/src/main/java/com/bansify/mail/ui/components/AvatarAndBadges.kt b/app/src/main/java/com/bansify/mail/ui/components/AvatarAndBadges.kt index 40a1c57..9590177 100644 --- a/app/src/main/java/com/bansify/mail/ui/components/AvatarAndBadges.kt +++ b/app/src/main/java/com/bansify/mail/ui/components/AvatarAndBadges.kt @@ -1,5 +1,8 @@ package com.bansify.mail.ui.components +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import androidx.collection.LruCache import androidx.compose.animation.AnimatedContent import androidx.compose.animation.core.Spring import androidx.compose.animation.core.spring @@ -8,6 +11,7 @@ import androidx.compose.animation.fadeOut import androidx.compose.animation.scaleIn import androidx.compose.animation.scaleOut import androidx.compose.animation.togetherWith +import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable @@ -36,11 +40,18 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.minimumInteractiveComponentSize import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow @@ -51,11 +62,69 @@ import com.bansify.mail.data.model.MailFolder import com.bansify.mail.ui.theme.PromotionsGreen import com.bansify.mail.ui.theme.SocialBlue import com.bansify.mail.ui.theme.UpdatesAmber +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import okhttp3.OkHttpClient +import okhttp3.Request +import java.util.concurrent.TimeUnit + +/** + * In-memory cache for domain and brand logos (e.g. Google, Instagram, Bansify, GitHub, Amazon). + * Uses the Google Favicons service which resolves 128x128 brand icons for any corporate domain. + */ +object DomainLogoCache { + private val memoryCache = object : LruCache(100) {} + private val client by lazy { + OkHttpClient.Builder() + .connectTimeout(4, TimeUnit.SECONDS) + .readTimeout(4, TimeUnit.SECONDS) + .followRedirects(true) + .followSslRedirects(true) + .build() + } + + private val personalDomains = setOf( + "gmail.com", "yahoo.com", "hotmail.com", "outlook.com", + "icloud.com", "live.com", "aol.com", "proton.me", "protonmail.com" + ) + + fun isPersonalDomain(domain: String): Boolean = domain in personalDomains + + fun get(domain: String): Bitmap? = memoryCache.get(domain) + + suspend fun load(domain: String): Bitmap? = withContext(Dispatchers.IO) { + if (domain.isBlank() || isPersonalDomain(domain)) return@withContext null + memoryCache.get(domain)?.let { return@withContext it } + val url = "https://www.google.com/s2/favicons?domain=$domain&sz=128" + try { + val request = Request.Builder() + .url(url) + .header("User-Agent", "Mozilla/5.0 (Android; Mobile)") + .build() + client.newCall(request).execute().use { response -> + if (response.isSuccessful) { + val bytes = response.body?.bytes() + if (bytes != null && bytes.isNotEmpty()) { + val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + if (bitmap != null) { + memoryCache.put(domain, bitmap) + return@withContext bitmap + } + } + } + } + } catch (_: Throwable) { + // Ignore network or test environment errors gracefully + } + null + } +} @Composable fun SenderAvatar( name: String, colorHex: Long, + email: String? = null, isSelected: Boolean = false, size: Dp = 40.dp, onClick: (() -> Unit)? = null, @@ -73,6 +142,24 @@ fun SenderAvatar( } ) + val domain = remember(email) { + val d = email?.substringAfter('@', "")?.trim()?.lowercase().orEmpty() + if (d.isNotEmpty() && !DomainLogoCache.isPersonalDomain(d)) d else null + } + + var logoBitmap by remember(domain) { + mutableStateOf(domain?.let { DomainLogoCache.get(it) }) + } + + LaunchedEffect(domain) { + if (domain != null && logoBitmap == null) { + val loaded = DomainLogoCache.load(domain) + if (loaded != null) { + logoBitmap = loaded + } + } + } + Box( modifier = baseModifier, contentAlignment = Alignment.Center @@ -101,19 +188,40 @@ fun SenderAvatar( ) } } else { - Box( - modifier = Modifier - .size(size) - .clip(CircleShape) - .background(Color(colorHex)), - contentAlignment = Alignment.Center - ) { - Text( - text = MailFormatters.extractInitials(name), - style = MaterialTheme.typography.titleMedium, - color = Color.White, - fontWeight = FontWeight.SemiBold - ) + val currentLogo = logoBitmap + if (currentLogo != null) { + Box( + modifier = Modifier + .size(size) + .clip(CircleShape) + .background(Color.White) + .border(1.dp, MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f), CircleShape), + contentAlignment = Alignment.Center + ) { + Image( + bitmap = currentLogo.asImageBitmap(), + contentDescription = name, + contentScale = ContentScale.Fit, + modifier = Modifier + .size(size * 0.72f) + .clip(CircleShape) + ) + } + } else { + Box( + modifier = Modifier + .size(size) + .clip(CircleShape) + .background(Color(colorHex)), + contentAlignment = Alignment.Center + ) { + Text( + text = MailFormatters.extractInitials(name), + style = MaterialTheme.typography.titleMedium, + color = Color.White, + fontWeight = FontWeight.SemiBold + ) + } } } } diff --git a/app/src/main/java/com/bansify/mail/ui/components/EmailThreadRow.kt b/app/src/main/java/com/bansify/mail/ui/components/EmailThreadRow.kt index 062c815..f69c9f7 100644 --- a/app/src/main/java/com/bansify/mail/ui/components/EmailThreadRow.kt +++ b/app/src/main/java/com/bansify/mail/ui/components/EmailThreadRow.kt @@ -64,7 +64,7 @@ fun EmailThreadRow( isTabletActive: Boolean = false, compactDensity: Boolean = false, rightSwipeAction: SwipeAction = SwipeAction.ARCHIVE, - leftSwipeAction: SwipeAction = SwipeAction.DELETE, + leftSwipeAction: SwipeAction = SwipeAction.ARCHIVE, onClick: () -> Unit, onLongClick: () -> Unit, onAvatarClick: () -> Unit, @@ -159,6 +159,7 @@ fun EmailThreadRow( SenderAvatar( name = thread.primarySenderName, colorHex = thread.primarySenderColorHex, + email = thread.primarySenderEmail, isSelected = isSelected, size = 40.dp, onClick = onAvatarClick, diff --git a/app/src/main/java/com/bansify/mail/ui/navigation/MailNavGraph.kt b/app/src/main/java/com/bansify/mail/ui/navigation/MailNavGraph.kt index ffe06ad..4cdf3af 100644 --- a/app/src/main/java/com/bansify/mail/ui/navigation/MailNavGraph.kt +++ b/app/src/main/java/com/bansify/mail/ui/navigation/MailNavGraph.kt @@ -73,6 +73,7 @@ fun MailNavGraph( uiState: MailUiState ) { val navController = rememberNavController() + val nativeAuth by viewModel.nativeAuth.collectAsStateWithLifecycle() val context = LocalContext.current val activity = context as? android.app.Activity @@ -327,4 +328,19 @@ fun MailNavGraph( } } } + + NativeAuthSheet( + state = nativeAuth, + onDismiss = viewModel::dismissNativeAuth, + onSubmitIdentifier = viewModel::nativeSubmitIdentifier, + onSubmitPassword = viewModel::nativeSubmitPassword, + onSubmitMfa = viewModel::nativeSubmitMfa, + onSubmitSignUp = viewModel::nativeSubmitSignUp, + onForgotPassword = viewModel::nativeForgotPassword, + onResetPassword = viewModel::nativeResetPassword, + onSwitchToSignUp = viewModel::openNativeSignUp, + onSwitchToSignIn = viewModel::openNativeSignIn, + onUpdate = viewModel::nativeAuthUpdate, + onBrowserSignIn = { activity?.let { viewModel.signIn(it, chooseAccount = true) } } + ) } diff --git a/app/src/main/java/com/bansify/mail/ui/screens/InboxScreen.kt b/app/src/main/java/com/bansify/mail/ui/screens/InboxScreen.kt index ac64d20..a015ede 100644 --- a/app/src/main/java/com/bansify/mail/ui/screens/InboxScreen.kt +++ b/app/src/main/java/com/bansify/mail/ui/screens/InboxScreen.kt @@ -246,7 +246,8 @@ fun InboxScreen( bottomBar = { if (!isWideScreen && !uiState.isSelectionMode) { NavigationBar( - containerColor = MaterialTheme.colorScheme.surfaceContainerLow + containerColor = MaterialTheme.colorScheme.surfaceContainerLow, + modifier = Modifier.height(64.dp) ) { NavigationBarItem( selected = uiState.activeBottomTab == BottomNavTab.MAIL, @@ -325,8 +326,8 @@ fun InboxScreen( NavigationRailItem( selected = uiState.activeBottomTab == BottomNavTab.MEET, onClick = { onSelectBottomTab(BottomNavTab.MEET) }, - icon = { Icon(Icons.Rounded.Videocam, contentDescription = "Meet") }, - label = { Text("Meet") } + icon = { Icon(Icons.Rounded.Videocam, contentDescription = "Meetings") }, + label = { Text("Meetings") } ) } } @@ -408,10 +409,11 @@ fun InboxScreen( PillSearchTopBar( activeAccountName = uiState.activeAccount?.displayName ?: "Bansify User", activeAccountColor = uiState.activeAccount?.avatarColorHex ?: 0xFF0B57D0, + activeAccountEmail = uiState.activeAccount?.email, onOpenDrawer = { scope.launch { drawerState.open() } }, onOpenSearch = onOpenSearch, onOpenAccountSwitcher = { showAccountDialog = true }, - modifier = if (isWideScreen) Modifier else Modifier.statusBarsPadding() + modifier = Modifier ) } } @@ -424,7 +426,7 @@ fun InboxScreen( LazyColumn( state = listState, modifier = Modifier.fillMaxSize(), - contentPadding = PaddingValues(bottom = 96.dp) + contentPadding = PaddingValues(bottom = 80.dp) ) { // Current Folder Label + Quick Filters item(key = "folder_header") { @@ -664,7 +666,8 @@ private fun PillSearchTopBar( onOpenDrawer: () -> Unit, onOpenSearch: () -> Unit, onOpenAccountSwitcher: () -> Unit, - modifier: Modifier = Modifier + modifier: Modifier = Modifier, + activeAccountEmail: String? = null ) { Surface( shape = RoundedCornerShape(28.dp), @@ -672,7 +675,7 @@ private fun PillSearchTopBar( tonalElevation = 2.dp, modifier = modifier .fillMaxWidth() - .padding(horizontal = 16.dp, vertical = 10.dp) + .padding(horizontal = 16.dp, vertical = 6.dp) .height(52.dp) .clip(RoundedCornerShape(28.dp)) .clickable(onClick = onOpenSearch) @@ -707,6 +710,7 @@ private fun PillSearchTopBar( SenderAvatar( name = activeAccountName, colorHex = activeAccountColor, + email = activeAccountEmail, size = 32.dp, onClick = onOpenAccountSwitcher, modifier = Modifier.testTag("account_avatar_button") diff --git a/app/src/main/java/com/bansify/mail/ui/screens/MeetScreen.kt b/app/src/main/java/com/bansify/mail/ui/screens/MeetScreen.kt index c0bed4b..0d247e0 100644 --- a/app/src/main/java/com/bansify/mail/ui/screens/MeetScreen.kt +++ b/app/src/main/java/com/bansify/mail/ui/screens/MeetScreen.kt @@ -96,7 +96,7 @@ fun MeetTabContent( } Spacer(modifier = Modifier.weight(1f)) Text( - text = "Meet", + text = "Meetings", style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.SemiBold ) @@ -105,6 +105,7 @@ fun MeetTabContent( SenderAvatar( name = activeAccount.displayName, colorHex = activeAccount.avatarColorHex, + email = activeAccount.email, size = 34.dp, onClick = onOpenAccountDialog ) @@ -316,6 +317,7 @@ fun MeetTabContent( SenderAvatar( name = activeAccount.displayName, colorHex = activeAccount.avatarColorHex, + email = activeAccount.email, size = 56.dp ) } diff --git a/app/src/main/java/com/bansify/mail/ui/screens/SettingsScreen.kt b/app/src/main/java/com/bansify/mail/ui/screens/SettingsScreen.kt index b7d3cfd..37c315a 100644 --- a/app/src/main/java/com/bansify/mail/ui/screens/SettingsScreen.kt +++ b/app/src/main/java/com/bansify/mail/ui/screens/SettingsScreen.kt @@ -176,6 +176,7 @@ fun SettingsScreen( SenderAvatar( name = activeAccount.displayName, colorHex = activeAccount.avatarColorHex, + email = activeAccount.email, size = 32.dp ) Spacer(modifier = Modifier.width(12.dp)) diff --git a/app/src/main/java/com/bansify/mail/ui/screens/SignedOutScreen.kt b/app/src/main/java/com/bansify/mail/ui/screens/SignedOutScreen.kt index a76bda9..abe1390 100644 --- a/app/src/main/java/com/bansify/mail/ui/screens/SignedOutScreen.kt +++ b/app/src/main/java/com/bansify/mail/ui/screens/SignedOutScreen.kt @@ -245,6 +245,7 @@ private fun DiscoveredAccountCard( SenderAvatar( name = discovered.displayName, colorHex = com.bansify.mail.data.repository.MailMappers.avatarColor(discovered.mailboxAddress), + email = discovered.mailboxAddress, size = 40.dp ) Spacer(modifier = Modifier.width(12.dp)) diff --git a/app/src/main/java/com/bansify/mail/ui/screens/ThreadDetailScreen.kt b/app/src/main/java/com/bansify/mail/ui/screens/ThreadDetailScreen.kt index 5b0e4a2..406e78e 100644 --- a/app/src/main/java/com/bansify/mail/ui/screens/ThreadDetailScreen.kt +++ b/app/src/main/java/com/bansify/mail/ui/screens/ThreadDetailScreen.kt @@ -1,10 +1,16 @@ package com.bansify.mail.ui.screens +import android.content.Intent +import android.view.ViewGroup +import android.webkit.WebResourceRequest +import android.webkit.WebView +import android.webkit.WebViewClient import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -42,12 +48,14 @@ import androidx.compose.material.icons.rounded.Star import androidx.compose.material.icons.rounded.StarOutline import androidx.compose.material3.AlertDialog import androidx.compose.material3.Checkbox +import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.IconButton +import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton import androidx.compose.material3.Scaffold @@ -65,10 +73,12 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView import com.bansify.mail.data.model.AttachmentItem import com.bansify.mail.data.model.EmailThreadEntity import com.bansify.mail.data.model.MailFolder @@ -335,6 +345,17 @@ fun ThreadDetailScreen( .fillMaxSize() .padding(innerPadding) ) { + if (!thread.bodyLoaded) { + item(key = "thread_loading_indicator") { + LinearProgressIndicator( + modifier = Modifier + .fillMaxWidth() + .height(2.dp), + color = MaterialTheme.colorScheme.primary + ) + } + } + // Subject + Star + Label Pills Header item(key = "subject_header") { Column( @@ -401,7 +422,9 @@ fun ThreadDetailScreen( onReply = { onReply(thread) }, onReplyAll = { onReplyAll(thread) }, onForward = { onForward(thread) }, - onAttachmentClick = { previewAttachment = it } + onAttachmentClick = { previewAttachment = it }, + fallbackBodyHtml = if (index == thread.messages.lastIndex) thread.bodyHtml else null, + isBodyLoaded = thread.bodyLoaded ) if (index < thread.messages.lastIndex) { HorizontalDivider( @@ -511,7 +534,9 @@ private fun ThreadMessageCard( onReply: () -> Unit, onReplyAll: () -> Unit, onForward: () -> Unit, - onAttachmentClick: (AttachmentItem) -> Unit + onAttachmentClick: (AttachmentItem) -> Unit, + fallbackBodyHtml: String? = null, + isBodyLoaded: Boolean = true ) { var showMetadataDetails by remember { mutableStateOf(false) } var showMessageMenu by remember { mutableStateOf(false) } @@ -531,6 +556,7 @@ private fun ThreadMessageCard( SenderAvatar( name = message.senderName, colorHex = message.senderColorHex, + email = message.senderEmail, size = 40.dp ) Spacer(modifier = Modifier.width(12.dp)) @@ -669,11 +695,65 @@ private fun ThreadMessageCard( // Expanded Body + Attachments AnimatedVisibility(visible = isExpanded) { Column(modifier = Modifier.padding(top = 14.dp)) { - Text( - text = message.body, - style = MaterialTheme.typography.bodyLarge, - color = MaterialTheme.colorScheme.onSurface - ) + val effectiveHtml = message.bodyHtml?.takeIf { it.isNotBlank() } ?: fallbackBodyHtml?.takeIf { it.isNotBlank() } + var showPlainText by remember { mutableStateOf(false) } + + if (!isBodyLoaded && message.body.isBlank() && effectiveHtml.isNullOrBlank()) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 24.dp), + horizontalArrangement = Arrangement.Center, + verticalAlignment = Alignment.CenterVertically + ) { + CircularProgressIndicator( + modifier = Modifier.size(24.dp), + strokeWidth = 2.dp, + color = MaterialTheme.colorScheme.primary + ) + Spacer(modifier = Modifier.width(12.dp)) + Text( + text = "Loading message...", + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant + ) + } + } else if (!effectiveHtml.isNullOrBlank() && !showPlainText) { + HtmlEmailView( + html = effectiveHtml, + modifier = Modifier.fillMaxWidth() + ) + if (message.body.isNotBlank()) { + TextButton( + onClick = { showPlainText = true }, + modifier = Modifier.padding(top = 4.dp) + ) { + Text( + text = "View plain text", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.primary + ) + } + } + } else { + Text( + text = message.body, + style = MaterialTheme.typography.bodyLarge, + color = MaterialTheme.colorScheme.onSurface + ) + if (!effectiveHtml.isNullOrBlank()) { + TextButton( + onClick = { showPlainText = false }, + modifier = Modifier.padding(top = 4.dp) + ) { + Text( + text = "View formatted email", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.primary + ) + } + } + } if (message.attachments.isNotEmpty()) { Spacer(modifier = Modifier.height(16.dp)) @@ -735,3 +815,105 @@ private fun MetadataLine(label: String, value: String) { ) } } + +/** + * Renders rich HTML email bodies cleanly and safely using an isolated WebView. + * Handles viewport scaling, dark mode styling, and intercepts links to open in the system browser. + */ +@Composable +fun HtmlEmailView( + html: String, + modifier: Modifier = Modifier +) { + val context = LocalContext.current + val isDark = isSystemInDarkTheme() + + val formattedHtml = remember(html, isDark) { + val textColor = if (isDark) "#E2E8F0" else "#1E293B" + val linkColor = if (isDark) "#60A5FA" else "#2563EB" + """ + + + + + + + + $html + + + """.trimIndent() + } + + AndroidView( + modifier = modifier.fillMaxWidth(), + factory = { ctx -> + WebView(ctx).apply { + layoutParams = ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + setBackgroundColor(android.graphics.Color.TRANSPARENT) + isNestedScrollingEnabled = false + settings.apply { + javaScriptEnabled = false + loadWithOverviewMode = true + useWideViewPort = false + domStorageEnabled = false + builtInZoomControls = false + displayZoomControls = false + defaultTextEncodingName = "utf-8" + } + webViewClient = object : WebViewClient() { + override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { + val uri = request?.url ?: return false + return try { + val intent = Intent(Intent.ACTION_VIEW, uri) + context.startActivity(intent) + true + } catch (_: Exception) { + false + } + } + + override fun onPageFinished(view: WebView?, url: String?) { + super.onPageFinished(view, url) + view?.requestLayout() + } + } + } + }, + update = { webView -> + webView.loadDataWithBaseURL(null, formattedHtml, "text/html", "utf-8", null) + } + ) +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a863285..7f1f914 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,7 +3,7 @@ Search in mail Compose Mail - Meet + Meetings Open navigation drawer Switch account Toggle star