package com.cottondb.android.ui.photos 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.Spacer import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawingPadding 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.grid.GridItemSpan import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.runtime.remember import androidx.compose.runtime.mutableStateOf import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier 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.unit.dp import androidx.compose.ui.unit.sp import com.cottondb.android.design.CottonFonts import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import com.cottondb.android.media.LocalMediaSource import com.cottondb.android.navigation.Route import com.cottondb.android.ui.media.MediaImage import com.cottondb.android.traffic.photos.LibraryMedia import com.cottondb.android.traffic.photos.LibraryMediaKind import com.cottondb.android.traffic.photos.PhotosFilter import com.cottondb.android.traffic.photos.PhotosLibraryPresentation import com.cottondb.android.traffic.photos.PhotosMonth /** Fully controlled presentation state; reads, folds, selection and navigation stay above this leaf. */ data class PhotosScreenState( val months: List, val show: PhotosFilter = PhotosFilter.All, val query: String = "", val searching: Boolean = false, val firstRead: Boolean = true, val loading: Boolean = false, val unreadable: Boolean = false, val readProblem: String? = null, val waiting: Long = 0, val suggestedAlbums: Int = 0, val duplicates: Long = 0, val faceSuggestions: Long = 0, val hasPhotos: Boolean = false, val hasVideos: Boolean = false, val selecting: Boolean = false, val selectionEnabled: Boolean = true, val deleting: Boolean = false, val deletionMessage: String? = null, val deletionReadRequired: Boolean = false, val selectedIds: Set = emptySet(), val prints: Boolean = true, val layoutProblem: String? = null, ) /** * Swift's `/photos` library leaf: one read's already-folded months, local thumbnails only, * and controlled callbacks for search, filtering, picking and opening a typed route. */ @Composable fun PhotosScreen( state: PhotosScreenState, media: LocalMediaSource, dataRevision: Long, onBack: () -> Unit, onShowChange: (PhotosFilter) -> Unit, onPrintsChange: (Boolean) -> Unit, onSearchingChange: (Boolean) -> Unit, onQueryChange: (String) -> Unit, onRetry: () -> Unit, onStartSelecting: (() -> Unit)? = null, onAskDelete: (() -> Unit)? = null, onStopSelecting: () -> Unit, onToggleSelected: (String) -> Unit, onOpen: (Route) -> Unit, modifier: Modifier = Modifier, ) { val allItems = state.months.flatMap(PhotosMonth::items) val queryBites = state.searching && state.query.trim().isNotEmpty() val showFilters = !state.selecting && !state.firstRead && (state.hasPhotos && state.hasVideos || state.show != PhotosFilter.All || state.waiting > 0) Column(modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding()) { PhotosHeader(state, onBack, onSearchingChange, onQueryChange, onPrintsChange, onStartSelecting, onAskDelete, onStopSelecting, onOpen) state.deletionMessage?.let { Text(it, Modifier.padding(horizontal = 20.dp), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } if (state.deletionReadRequired && state.readProblem == null) TextButton(onClick = onRetry, enabled = !state.loading && !state.deleting) { Text("Refresh photos") } state.layoutProblem?.let { Text(it, Modifier.padding(horizontal = 20.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError) } if (showFilters) PhotosFilterRow(state.show, state.waiting, onShowChange) if (state.readProblem != null && !state.unreadable) { Text("Your photos could not be refreshed.", Modifier.padding(horizontal = 20.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError) TextButton(onClick = onRetry, enabled = !state.loading) { Text("Try again") } } when { state.unreadable -> PhotosUnreadable(onRetry) state.firstRead -> PhotosSkeleton(state.prints) queryBites && allItems.isEmpty() -> PhotosNoMatches() allItems.isEmpty() -> PhotosEmpty(state.show) else -> PhotosMonths(state, media, dataRevision, onToggleSelected, onOpen) } } } @Composable private fun PhotosHeader( state: PhotosScreenState, onBack: () -> Unit, onSearchingChange: (Boolean) -> Unit, onQueryChange: (String) -> Unit, onPrintsChange: (Boolean) -> Unit, onStartSelecting: (() -> Unit)?, onAskDelete: (() -> Unit)?, onStopSelecting: () -> Unit, onOpen: (Route) -> Unit, ) = Column(Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 8.dp)) { Row(verticalAlignment = Alignment.CenterVertically) { TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") } Spacer(Modifier.weight(1f)) if (state.selecting) { onAskDelete?.let { ask -> TextButton(onClick = ask, enabled = state.selectionEnabled && state.selectedIds.isNotEmpty(), modifier = Modifier.semantics { contentDescription = "Delete selected" }) { Text(if (state.deleting) "Deleting…" else "Delete") } } TextButton(onClick = onStopSelecting, enabled = !state.deleting, modifier = Modifier.semantics { contentDescription = "Done selecting" }) { Text("Done") } } else if (!state.searching) { TextButton(onClick = { onSearchingChange(true) }, modifier = Modifier.semantics { contentDescription = "Search your photos" }) { Text("Search") } Box { var menu by remember { mutableStateOf(false) } TextButton(onClick = { menu = true }, modifier = Modifier.semantics { contentDescription = "More actions" }) { Text("More") } DropdownMenu(expanded = menu, onDismissRequest = { menu = false }) { onStartSelecting?.let { select -> DropdownMenuItem(text = { Text("Select") }, enabled = state.selectionEnabled && (state.hasPhotos || state.hasVideos), onClick = { menu = false; select() }) } DropdownMenuItem(text = { Text(if (state.suggestedAlbums > 0) "Albums · ${state.suggestedAlbums} suggested" else "Albums") }, onClick = { menu = false; onOpen(Route.PhotosAlbums) }) if (state.duplicates > 0) DropdownMenuItem(text = { Text("Duplicates · ${state.duplicates}") }, onClick = { menu = false; onOpen(Route.PhotosDuplicates) }) if (state.faceSuggestions > 0) DropdownMenuItem(text = { Text("People to confirm · ${state.faceSuggestions}") }, onClick = { menu = false; onOpen(Route.PhotosPeople) }) } } } } Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text( if (state.selecting) selectedTitle(state.selectedIds.size) else "Photos", Modifier.weight(1f), style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, ) if (!state.selecting && !state.searching) { TextButton(onClick = { onPrintsChange(!state.prints) }, modifier = Modifier.semantics { contentDescription = "Change photo layout" }) { Text(if (state.prints) "Contact sheet" else "Prints") } } } if (state.searching) { OutlinedTextField( value = state.query, onValueChange = onQueryChange, modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Search your photos" }, placeholder = { Text("What's in your photos") }, singleLine = true, textStyle = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), trailingIcon = { TextButton(onClick = { onSearchingChange(false) }) { Text("Close") } }, ) } } @Composable private fun PhotosFilterRow(show: PhotosFilter, waiting: Long, onShowChange: (PhotosFilter) -> Unit) = Row( Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 4.dp), verticalAlignment = Alignment.CenterVertically, ) { PhotosFilter.entries.forEachIndexed { index, option -> if (index > 0) Text("·", Modifier.padding(horizontal = 5.dp), color = CottonPalette.inkTertiary) Text( option.name.lowercase(), Modifier.clickable(role = Role.Button) { onShowChange(option) } .semantics { contentDescription = "Show ${option.name.lowercase()}" }, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), color = if (show == option) CottonPalette.ink else CottonPalette.inkTertiary, ) } Spacer(Modifier.weight(1f)) if (waiting > 0) Text( "$waiting to review", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 16.sp), color = CottonPalette.statusError, ) } @Composable private fun PhotosMonths( state: PhotosScreenState, media: LocalMediaSource, dataRevision: Long, onToggleSelected: (String) -> Unit, onOpen: (Route) -> Unit, ) = LazyVerticalGrid( columns = GridCells.Fixed(if (state.prints) 3 else 4), modifier = Modifier.fillMaxSize().semantics { contentDescription = "Photos library" }, contentPadding = PaddingValues(20.dp), horizontalArrangement = Arrangement.spacedBy(if (state.prints) 16.dp else 2.dp), verticalArrangement = Arrangement.spacedBy(if (state.prints) 14.dp else 2.dp), ) { state.months.forEach { month -> item("month-${month.id}", span = { GridItemSpan(maxLineSpan) }) { Row(Modifier.fillMaxWidth().padding(top = 12.dp), verticalAlignment = Alignment.Bottom) { Text(month.title, style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink) Text(" ${PhotosLibraryPresentation.countLine(month.items)}", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) } } items(month.items, key = LibraryMedia::id) { item -> PhotosTile(item, state.prints, state.selecting, item.id in state.selectedIds, media, dataRevision, onToggleSelected, onOpen, enabled = !state.selecting || state.selectionEnabled) } } } @Composable internal fun PhotosTile( item: LibraryMedia, prints: Boolean, selecting: Boolean, selected: Boolean, media: LocalMediaSource, dataRevision: Long, onToggleSelected: (String) -> Unit, onOpen: (Route) -> Unit, enabled: Boolean = true, ) = Box( Modifier.fillMaxWidth().aspectRatio(if (prints) .72f else 1f) .clickable(enabled = enabled, role = Role.Button) { if (selecting) onToggleSelected(item.id) else onOpen(if (item.kind == LibraryMediaKind.Photo) Route.PhotosPhoto(item.id) else Route.VideosVideo(item.id)) } .semantics { contentDescription = "Photo tile ${item.id}: ${item.caption ?: if (item.kind == LibraryMediaKind.Video) "Video" else "Photo"}" }, ) { val key = if (item.kind == LibraryMediaKind.Photo) item.key else item.posterKey val label = item.caption ?: if (item.kind == LibraryMediaKind.Video) "Video" else "Photo" if (prints) Column(Modifier.fillMaxSize().background(CottonPalette.ground).padding(7.dp)) { MediaImage(key, media, dataRevision, 120.dp, Modifier.fillMaxWidth().aspectRatio(1f), label) if (item.kind == LibraryMediaKind.Video) Text( PhotosLibraryPresentation.durationLabel(item.durationMs)?.let { "▶ $it" } ?: "▶", Modifier.padding(top = 3.dp), style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.hand), color = CottonPalette.ink, ) } else { MediaImage(key, media, dataRevision, 120.dp, Modifier.fillMaxSize(), label) if (item.kind == LibraryMediaKind.Video) Text( "▶", Modifier.align(Alignment.BottomEnd).padding(3.dp), style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.hand), color = CottonPalette.ink, ) } if (selecting) Text( if (selected) "✓" else "○", Modifier.align(Alignment.TopEnd).padding(5.dp), style = CottonTextStyles.rowTitle.copy(fontFamily = CottonFonts.type), color = if (selected) CottonPalette.statusSuccess else CottonPalette.inkSecondary, ) } @Composable private fun PhotosSkeleton(prints: Boolean) = LazyVerticalGrid( GridCells.Fixed(if (prints) 3 else 4), Modifier.fillMaxSize().semantics { contentDescription = "Photos loading" }, contentPadding = PaddingValues(20.dp), horizontalArrangement = Arrangement.spacedBy(if (prints) 16.dp else 2.dp), verticalArrangement = Arrangement.spacedBy(if (prints) 16.dp else 2.dp), ) { items(9) { Box(Modifier.fillMaxWidth().aspectRatio(if (prints) .72f else 1f).background(CottonPalette.ground)) } } @Composable private fun PhotosUnreadable(onRetry: () -> Unit) = PhotosNotice( "Your photos could not be read", "Your pictures are on this phone, and Cotton could not open them.", retry = onRetry, ) @Composable private fun PhotosNoMatches() = PhotosNotice( "Nothing matches.", "This searches what your pictures say — your captions, and the descriptions Cotton wrote.", ) @Composable private fun PhotosEmpty(show: PhotosFilter) = PhotosNotice( if (show == PhotosFilter.Videos) "No videos yet." else "No photos yet.", "Pictures and videos from your chats land here on their own.", ) @Composable private fun PhotosNotice(first: String, second: String, retry: (() -> Unit)? = null) = Column( Modifier.fillMaxSize().padding(32.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { Text(first, style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink, textAlign = TextAlign.Center) Text(second, Modifier.padding(top = 8.dp), style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary, textAlign = TextAlign.Center) retry?.let { TextButton(it, Modifier.semantics { contentDescription = "Try again" }) { Text("Try again") } } } private fun selectedTitle(count: Int): String = if (count == 0) "Select" else if (count == 1) "1 selected" else "$count selected"