@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class) package com.cottondb.android.ui.day.people 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.Spacer import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.material3.pulltorefresh.PullToRefreshBox import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.shadow import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import com.cottondb.android.day.people.PeopleRead import com.cottondb.android.day.people.PeopleState import com.cottondb.android.day.people.PersonListing import com.cottondb.android.day.people.filterPeople import com.cottondb.android.design.CottonFonts import com.cottondb.android.design.CottonMetrics import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import java.time.Clock import java.time.ZoneId import java.time.format.DateTimeFormatter /** The host provides CottonAvatar so this directory never fetches media itself. */ typealias PersonAvatar = @Composable (id: String, name: String, avatarKey: String?, size: Dp) -> Unit /** This device-local presentation choice is deliberately separate from the full directory read. */ enum class PeopleLayout { Grid, List } /** * `/people`: the complete directory from `read.people`, filtered locally by the screen-owned query. * The importer is a route action because contacts are curated there, never typed into this directory. */ @Composable fun PeopleDirectoryScreen( state: PeopleState, query: String, layout: PeopleLayout, onBack: () -> Unit = {}, onImport: () -> Unit = {}, onQueryChange: (String) -> Unit, onLayoutChange: (PeopleLayout) -> Unit, onRefresh: () -> Unit, onOpenPerson: (String) -> Unit, avatar: PersonAvatar = { id, name, _, size -> FallbackPersonAvatar(id, name, size) }, clock: Clock = Clock.systemDefaultZone(), zoneId: ZoneId = ZoneId.systemDefault(), modifier: Modifier = Modifier, ) { val retained = when (state) { is PeopleState.Data -> state.read is PeopleState.Error -> state.last PeopleState.Loading -> null } val rows = retained?.people?.let { filterPeople(it, query) }.orEmpty() Column( modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding(), ) { PeopleTopBar( onBack = onBack, onImport = onImport, layout = layout, onLayoutChange = onLayoutChange, ) OutlinedTextField( value = query, onValueChange = onQueryChange, placeholder = { Text("Search name, org, city") }, singleLine = true, modifier = Modifier.fillMaxWidth().padding( start = CottonMetrics.contentMargin, end = CottonMetrics.contentMargin, bottom = 10.dp, ).semantics { contentDescription = "Search people" }, ) PullToRefreshBox( isRefreshing = (state as? PeopleState.Data)?.refreshing == true, onRefresh = onRefresh, modifier = Modifier.weight(1f), ) { when { retained != null -> { Column(Modifier.fillMaxSize()) { (state as? PeopleState.Error)?.let { PeopleErrorNotice(it.message, onRefresh) } if (retained.people.isEmpty()) { PeopleEmpty(onImport) } else if (rows.isEmpty()) { PeopleNoMatches(onClear = { onQueryChange("") }) } else { DirectoryRows(rows, layout, avatar, clock, zoneId, onOpenPerson) } } } state is PeopleState.Loading -> PeopleDirectorySkeleton() state is PeopleState.Error -> PeopleReadFailure((state as PeopleState.Error).message, onRefresh) } } } } @Composable private fun PeopleTopBar( onBack: () -> Unit, onImport: () -> Unit, layout: PeopleLayout, onLayoutChange: (PeopleLayout) -> Unit, ) = Column(Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin, vertical = 8.dp)) { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text( "Back", Modifier.clickable(role = Role.Button, onClick = onBack).padding(8.dp) .semantics { contentDescription = "Back" }, style = CottonTextStyles.preview, color = CottonPalette.inkSecondary, ) Spacer(Modifier.weight(1f)) Text( if (layout == PeopleLayout.Grid) "List" else "Grid", Modifier.clickable(role = Role.Button) { onLayoutChange(if (layout == PeopleLayout.Grid) PeopleLayout.List else PeopleLayout.Grid) }.padding(8.dp).semantics { contentDescription = "Change people layout" }, style = CottonTextStyles.preview, color = CottonPalette.inkSecondary, ) Text( "Import", Modifier.clickable(role = Role.Button, onClick = onImport).padding(8.dp) .semantics { contentDescription = "Import contacts" }, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), color = CottonPalette.accent, ) } Text( "People", Modifier.padding(start = 8.dp, top = 2.dp, bottom = 5.dp), style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, ) } @Composable private fun DirectoryRows( people: List, layout: PeopleLayout, avatar: PersonAvatar, clock: Clock, zoneId: ZoneId, onOpen: (String) -> Unit, ) { when (layout) { PeopleLayout.Grid -> LazyVerticalGrid( columns = GridCells.Fixed(4), modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(horizontal = CottonMetrics.contentMargin, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(10.dp), verticalArrangement = Arrangement.spacedBy(14.dp), ) { items(people, key = { it.id }) { person -> PersonGridCard(person, avatar, onOpen) } } PeopleLayout.List -> LazyColumn( modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(horizontal = CottonMetrics.contentMargin, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), ) { items(people, key = { it.id }) { person -> PersonListRow(person, avatar, clock, zoneId, onOpen) } } } } @Composable private fun PersonGridCard(person: PersonListing, avatar: PersonAvatar, onOpen: (String) -> Unit) = Column( Modifier.fillMaxWidth().clickable(role = Role.Button) { onOpen(person.id) } .semantics { contentDescription = person.name.orEmpty().ifBlank { "Person" } }, horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(6.dp), ) { avatar(person.id, person.name.orEmpty(), person.avatarKey, 68.dp) Text( person.name.orEmpty().ifBlank { "Unnamed" }, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, maxLines = 2, overflow = TextOverflow.Ellipsis, textAlign = TextAlign.Center, ) PersonCardDetail(person)?.let { detail -> Text(detail, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary, maxLines = 1, overflow = TextOverflow.Ellipsis) } } @Composable private fun PersonListRow( person: PersonListing, avatar: PersonAvatar, clock: Clock, zoneId: ZoneId, onOpen: (String) -> Unit, ) = Row( Modifier.fillMaxWidth().shadow(2.dp, RoundedCornerShape(CottonMetrics.cardRadius)) .background(CottonPalette.groundElevated, RoundedCornerShape(CottonMetrics.cardRadius)) .clickable(role = Role.Button) { onOpen(person.id) } .semantics { contentDescription = person.name.orEmpty().ifBlank { "Person" } } .padding(12.dp), verticalAlignment = Alignment.CenterVertically, ) { avatar(person.id, person.name.orEmpty(), person.avatarKey, 46.dp) Column(Modifier.weight(1f).padding(horizontal = 12.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) { Text(person.name.orEmpty().ifBlank { "Unnamed" }, style = CottonTextStyles.body.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, maxLines = 1, overflow = TextOverflow.Ellipsis) PersonListDetail(person)?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary, maxLines = 1, overflow = TextOverflow.Ellipsis) } } PersonZoneTime(person.timezone, clock, zoneId)?.let { time -> Text(time, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) } } private fun PersonCardDetail(person: PersonListing): String? = listOfNotNull(person.org, person.roleTitle).firstOrNull() private fun PersonListDetail(person: PersonListing): String? = listOfNotNull(person.roleTitle, person.org, person.city).filter(String::isNotBlank).joinToString(" · ").ifBlank { null } internal fun PersonZoneTime(timezone: String?, clock: Clock, deviceZone: ZoneId): String? { val zone = timezone?.trim()?.takeIf(String::isNotEmpty)?.let { runCatching { ZoneId.of(it) }.getOrNull() } ?: return null if (zone == deviceZone) return null return DateTimeFormatter.ofPattern("h:mm a").withZone(zone).format(clock.instant()) } @Composable private fun PeopleDirectorySkeleton() = LazyColumn( Modifier.fillMaxSize().padding(horizontal = CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(10.dp), ) { items(8) { Box(Modifier.fillMaxWidth().height(70.dp).background(CottonPalette.groundElevated, RoundedCornerShape(CottonMetrics.cardRadius))) } } @Composable private fun PeopleEmpty(onImport: () -> Unit) = PeopleMessage( title = "No people yet", body = "Bring in the contacts that matter from this phone.", action = "Import contacts", onAction = onImport, ) @Composable private fun PeopleNoMatches(onClear: () -> Unit) = PeopleMessage( title = "No one matches that.", body = "Search name, organisation, or city.", action = "Clear search", onAction = onClear, ) @Composable private fun PeopleReadFailure(message: String, retry: () -> Unit) = PeopleMessage( title = "People could not be read", body = message, action = "Try again", onAction = retry, ) @Composable private fun PeopleErrorNotice(message: String, retry: () -> Unit) = Row( Modifier.fillMaxWidth().background(CottonPalette.groundElevated) .padding(horizontal = CottonMetrics.contentMargin, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { Text(message, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError) Text("Try again", Modifier.clickable(role = Role.Button, onClick = retry).padding(8.dp), style = CottonTextStyles.preview, color = CottonPalette.accent) } @Composable private fun PeopleMessage(title: String, body: String, action: String, onAction: () -> Unit) = Column( Modifier.fillMaxWidth().padding(top = 42.dp, start = CottonMetrics.contentMargin, end = CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(8.dp), ) { Text(title, style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink) Text(body, style = CottonTextStyles.body, color = CottonPalette.inkSecondary) Text(action, Modifier.clickable(role = Role.Button, onClick = onAction).padding(vertical = 8.dp), style = CottonTextStyles.preview, color = CottonPalette.accent) } @Composable private fun FallbackPersonAvatar(seed: String, name: String, size: Dp) = Box( Modifier.size(size).background(CottonPalette.groundElevated, RoundedCornerShape(3.dp)), contentAlignment = Alignment.Center, ) { Text(name.trim().firstOrNull()?.uppercaseChar()?.toString() ?: "–", style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type), color = CottonPalette.ink) }