180 lines
8.0 KiB
Kotlin
180 lines
8.0 KiB
Kotlin
package com.androidsmsgateway.ui.screens
|
|
|
|
import androidx.compose.foundation.background
|
|
import androidx.compose.foundation.clickable
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.Column
|
|
import androidx.compose.foundation.layout.PaddingValues
|
|
import androidx.compose.foundation.layout.Row
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.foundation.layout.size
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
import androidx.compose.foundation.lazy.items
|
|
import androidx.compose.foundation.shape.CircleShape
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
import androidx.compose.material.icons.Icons
|
|
import androidx.compose.material.icons.outlined.Inbox
|
|
import androidx.compose.material.icons.outlined.MarkEmailRead
|
|
import androidx.compose.material.icons.outlined.Outbox
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableIntStateOf
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.saveable.rememberSaveable
|
|
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.text.font.FontWeight
|
|
import androidx.compose.ui.text.style.TextOverflow
|
|
import androidx.compose.ui.unit.dp
|
|
import com.androidsmsgateway.data.local.IncomingSmsEntity
|
|
import com.androidsmsgateway.data.local.OutgoingSmsEntity
|
|
import com.androidsmsgateway.ui.GatewayUiState
|
|
import com.androidsmsgateway.ui.components.BadgeTone
|
|
import com.androidsmsgateway.ui.components.EmptyState
|
|
import com.androidsmsgateway.ui.components.GatewayCard
|
|
import com.androidsmsgateway.ui.components.GatewaySearchField
|
|
import com.androidsmsgateway.ui.components.LiveBadge
|
|
import com.androidsmsgateway.ui.components.ScreenHeader
|
|
import com.androidsmsgateway.ui.components.StatusBadge
|
|
import com.androidsmsgateway.ui.theme.GatewayInfoBackground
|
|
import com.androidsmsgateway.ui.theme.GatewayMint
|
|
import com.androidsmsgateway.ui.theme.GatewayPrimary
|
|
|
|
@Composable
|
|
fun MessagesScreen(state: GatewayUiState) {
|
|
var tab by rememberSaveable { mutableIntStateOf(0) }
|
|
var query by rememberSaveable { mutableStateOf("") }
|
|
val incoming = remember(state.incoming, query) {
|
|
state.incoming.filter { query.isBlank() || it.sender.contains(query, true) || it.message.contains(query, true) }
|
|
}
|
|
val outgoing = remember(state.outgoing, query) {
|
|
state.outgoing.filter {
|
|
query.isBlank() || it.recipient.contains(query, true) || it.message.contains(query, true) || it.status.contains(query, true)
|
|
}
|
|
}
|
|
|
|
Column(Modifier.fillMaxSize()) {
|
|
Column(
|
|
Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 18.dp),
|
|
verticalArrangement = Arrangement.spacedBy(14.dp),
|
|
) {
|
|
ScreenHeader("Messages", "Live SMS activity", trailing = { LiveBadge() })
|
|
MessageTabs(tab) { tab = it }
|
|
GatewaySearchField(
|
|
value = query,
|
|
onValueChange = { query = it.take(100) },
|
|
placeholder = "Search sender or message",
|
|
)
|
|
}
|
|
when {
|
|
tab == 0 && incoming.isEmpty() -> EmptyState(
|
|
icon = Icons.Outlined.Inbox,
|
|
title = if (query.isBlank()) "No incoming messages" else "No matching messages",
|
|
body = if (query.isBlank()) "Messages received by this device will appear here." else "Try another sender, number, or phrase.",
|
|
modifier = Modifier.fillMaxSize(),
|
|
)
|
|
tab == 1 && outgoing.isEmpty() -> EmptyState(
|
|
icon = Icons.Outlined.Outbox,
|
|
title = if (query.isBlank()) "No outgoing messages" else "No matching messages",
|
|
body = if (query.isBlank()) "SMS requests submitted through the API will appear here." else "Try another recipient, status, or phrase.",
|
|
modifier = Modifier.fillMaxSize(),
|
|
)
|
|
tab == 0 -> IncomingList(incoming)
|
|
else -> OutgoingList(outgoing)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
private fun MessageTabs(selected: Int, onSelected: (Int) -> Unit) {
|
|
Row(
|
|
Modifier.fillMaxWidth().clip(RoundedCornerShape(14.dp)).background(MaterialTheme.colorScheme.surfaceVariant).padding(4.dp),
|
|
) {
|
|
listOf("Incoming", "Outgoing").forEachIndexed { index, label ->
|
|
Box(
|
|
Modifier.weight(1f).clip(RoundedCornerShape(11.dp))
|
|
.background(if (selected == index) MaterialTheme.colorScheme.surface else MaterialTheme.colorScheme.surfaceVariant)
|
|
.clickable { onSelected(index) }
|
|
.padding(vertical = 10.dp),
|
|
contentAlignment = Alignment.Center,
|
|
) {
|
|
Text(
|
|
label,
|
|
style = MaterialTheme.typography.labelLarge,
|
|
color = if (selected == index) GatewayPrimary else MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
private fun IncomingList(messages: List<IncomingSmsEntity>) {
|
|
LazyColumn(
|
|
Modifier.fillMaxSize(),
|
|
contentPadding = PaddingValues(start = 16.dp, end = 16.dp, bottom = 20.dp),
|
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
|
) {
|
|
items(messages, key = { it.id }) { item ->
|
|
MessageCard(item.sender, item.message, item.status, item.receivedAt, incoming = true)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
private fun OutgoingList(messages: List<OutgoingSmsEntity>) {
|
|
LazyColumn(
|
|
Modifier.fillMaxSize(),
|
|
contentPadding = PaddingValues(start = 16.dp, end = 16.dp, bottom = 20.dp),
|
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
|
) {
|
|
items(messages, key = { it.id }) { item ->
|
|
MessageCard(item.recipient, item.message, item.status, item.requestedAt, incoming = false)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
private fun MessageCard(address: String, body: String, status: String, timestamp: Long, incoming: Boolean) {
|
|
val normalized = status.lowercase()
|
|
val tone = when (normalized) {
|
|
"received", "sent" -> BadgeTone.SUCCESS
|
|
"pending" -> BadgeTone.INFO
|
|
"failed" -> BadgeTone.ERROR
|
|
else -> BadgeTone.NEUTRAL
|
|
}
|
|
val avatarColor = if (incoming) GatewayMint else GatewayInfoBackground
|
|
GatewayCard(Modifier.fillMaxWidth()) {
|
|
Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(11.dp)) {
|
|
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(11.dp)) {
|
|
Box(Modifier.size(38.dp).clip(CircleShape).background(avatarColor), contentAlignment = Alignment.Center) {
|
|
Text(initials(address), style = MaterialTheme.typography.labelMedium, color = GatewayPrimary, fontWeight = FontWeight.Bold)
|
|
}
|
|
Column(Modifier.weight(1f)) {
|
|
Text(address, style = MaterialTheme.typography.titleMedium, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
|
StatusBadge(normalized, tone)
|
|
}
|
|
Text(formatTime(timestamp), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
|
}
|
|
Text(body, style = MaterialTheme.typography.bodyMedium, maxLines = 4, overflow = TextOverflow.Ellipsis)
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun initials(value: String): String {
|
|
val letters = value.trim().split(Regex("\\s+")).mapNotNull { it.firstOrNull()?.takeIf(Char::isLetterOrDigit) }
|
|
return when {
|
|
letters.size >= 2 -> "${letters[0]}${letters[1]}".uppercase()
|
|
letters.size == 1 -> letters[0].uppercase()
|
|
else -> "SMS"
|
|
}
|
|
}
|