package com.cottondb.android.ui.photos import androidx.compose.foundation.background 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.GridItemSpan import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items 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 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.traffic.photos.LibraryMedia import com.cottondb.android.traffic.photos.LibraryMediaKind import com.cottondb.android.traffic.photos.albums.AlbumDecision import com.cottondb.android.traffic.photos.albums.AlbumDetail import com.cottondb.android.traffic.photos.albums.AlbumDetailState import com.cottondb.android.traffic.photos.albums.AlbumFacts import com.cottondb.android.traffic.photos.albums.AlbumItem import com.cottondb.android.ui.media.MediaImage import java.time.ZoneId /** Controlled commands for one retained `/photos/album/[id]` visit. */ data class AlbumDetailCallbacks( val retry: () -> Unit, val editTitle: (String) -> Unit, val saveTitle: () -> Unit, val resolveTitle: (Boolean) -> Unit, val decide: (AlbumDecision) -> Unit, val startSelecting: () -> Unit, val stopSelecting: () -> Unit, val toggle: (String) -> Unit, val removeSelected: () -> Unit, ) @Composable fun AlbumDetailScreen( state: AlbumDetailState, media: LocalMediaSource, eventRevision: Long, zone: ZoneId, prints: Boolean, onPrintsChange: (Boolean) -> Unit, callbacks: AlbumDetailCallbacks, onBack: () -> Unit, onOpen: (Route) -> Unit, modifier: Modifier = Modifier, ) { val detail = state.album val selected = state.selected val selecting = selected != null Column(modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding()) { AlbumBar(detail, state, selecting, callbacks, onBack, onOpen) state.readProblem?.let { AlbumProblem(it, callbacks.retry, !state.loading && !state.writing) } state.writeProblem?.let { Text(it, Modifier.padding(horizontal = 20.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError) } state.receipt?.let { Text(it, Modifier.padding(horizontal = 20.dp), style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) } if (state.outcomeUnknown || state.needsRefresh) { TextButton(onClick = callbacks.retry, enabled = !state.loading && !state.writing) { Text("Refresh") } } when { detail == null && state.readProblem == null -> AlbumLoading() detail == null -> AlbumMissing() else -> AlbumBody(detail, state, media, eventRevision, zone, prints, onPrintsChange, callbacks, onOpen) } } } @Composable private fun AlbumBar( detail: AlbumDetail?, state: AlbumDetailState, selecting: Boolean, callbacks: AlbumDetailCallbacks, onBack: () -> Unit, onOpen: (Route) -> Unit, ) = Row( Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 6.dp), verticalAlignment = Alignment.CenterVertically, ) { TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") } Spacer(Modifier.weight(1f)) if (selecting) { Text(if (state.selected.orEmpty().isEmpty()) "Select" else "${state.selected!!.size} selected", style = CottonTextStyles.preview) TextButton( onClick = callbacks.removeSelected, enabled = state.canEdit && state.selected.orEmpty().isNotEmpty(), modifier = Modifier.semantics { contentDescription = "Remove from album" }, ) { Text("Remove") } TextButton(onClick = callbacks.stopSelecting, enabled = !state.writing && !state.outcomeUnknown, modifier = Modifier.semantics { contentDescription = "Done selecting" }) { Text("Done") } } else if (detail != null) { TextButton( onClick = callbacks.startSelecting, enabled = state.canEdit && detail.items.isNotEmpty(), modifier = Modifier.semantics { contentDescription = "Select" }, ) { Text("Select") } TextButton( onClick = { onOpen(Route.PhotosAlbumAdd(detail.card.id)) }, enabled = state.canEdit, modifier = Modifier.semantics { contentDescription = "Add photos" }, ) { Text("Add") } } } @Composable private fun AlbumBody( detail: AlbumDetail, state: AlbumDetailState, media: LocalMediaSource, eventRevision: Long, zone: ZoneId, prints: Boolean, onPrintsChange: (Boolean) -> Unit, callbacks: AlbumDetailCallbacks, onOpen: (Route) -> Unit, ) = LazyVerticalGrid( columns = GridCells.Fixed(if (prints) 3 else 4), modifier = Modifier.fillMaxSize().semantics { contentDescription = "Album pictures" }, contentPadding = PaddingValues(20.dp), horizontalArrangement = Arrangement.spacedBy(if (prints) 16.dp else 2.dp), verticalArrangement = Arrangement.spacedBy(if (prints) 16.dp else 2.dp), ) { if (state.selected == null) { item("album-header", span = { GridItemSpan(maxLineSpan) }) { AlbumHeader(detail, state, media, eventRevision, zone, callbacks) } } if (detail.items.isEmpty()) { item("album-empty", span = { GridItemSpan(maxLineSpan) }) { AlbumEmpty() } } else { item("album-pictures", span = { GridItemSpan(maxLineSpan) }) { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text("Pictures", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif)) Text(" ${detail.items.size}", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) Spacer(Modifier.weight(1f)) TextButton(onClick = { onPrintsChange(!prints) }, enabled = !state.writing) { Text(if (prints) "Contact sheet" else "Prints") } } } items(detail.items, key = AlbumItem::id) { item -> AlbumMemberTile(item, state, prints, media, eventRevision, callbacks, onOpen) } } } @Composable private fun AlbumHeader( detail: AlbumDetail, state: AlbumDetailState, media: LocalMediaSource, eventRevision: Long, zone: ZoneId, callbacks: AlbumDetailCallbacks, ) = Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { val card = detail.card Column(Modifier.fillMaxWidth().background(CottonPalette.ground).padding(8.dp)) { MediaImage( key = AlbumFacts.coverKey(card.cover), source = media, eventRevision = eventRevision, edge = 360.dp, modifier = Modifier.fillMaxWidth().aspectRatio(1f), description = card.title?.trim()?.takeIf(String::isNotEmpty) ?: "Album cover", ) } OutlinedTextField( value = state.titleDraft, onValueChange = callbacks.editTitle, readOnly = !state.canEdit, label = { Text("Name this album") }, modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Album title" }, supportingText = { when { state.titleTooLong -> Text("Album titles can be at most 120 characters.", color = CottonPalette.statusError) state.titleConflict -> Text("This title changed elsewhere.", color = CottonPalette.statusError) else -> Text("Edit the title") } }, ) if (state.titleConflict) Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { TextButton(onClick = { callbacks.resolveTitle(true) }, enabled = state.canEdit) { Text("Keep my title") } TextButton(onClick = { callbacks.resolveTitle(false) }, enabled = state.canEdit) { Text("Use latest title") } } else { TextButton( onClick = callbacks.saveTitle, enabled = state.canEdit && state.titleDirty && !state.titleTooLong, modifier = Modifier.semantics { contentDescription = "Save album title" }, ) { Text(if (state.writing) "Saving…" else "Save") } } Text(AlbumFacts.detailFacts(card, zone), style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary) AlbumDecisions(card.state, state.canAct, callbacks) } @Composable private fun AlbumDecisions(state: String?, canAct: Boolean, callbacks: AlbumDetailCallbacks) { when (state?.trim()) { "open" -> Column { Text("Cotton put these together — keep the album?", style = CottonTextStyles.body.copy(fontFamily = CottonFonts.hand)) Row { TextButton(onClick = { callbacks.decide(AlbumDecision.Keep) }, enabled = canAct) { Text("Keep") } TextButton(onClick = { callbacks.decide(AlbumDecision.Dismiss) }, enabled = canAct) { Text("Not this") } } } "dismissed" -> Column { Text("You said not this one.", style = CottonTextStyles.body.copy(fontFamily = CottonFonts.hand), color = CottonPalette.statusError) TextButton(onClick = { callbacks.decide(AlbumDecision.Open) }, enabled = canAct) { Text("Bring it back") } } } } @Composable private fun AlbumMemberTile( item: AlbumItem, state: AlbumDetailState, prints: Boolean, media: LocalMediaSource, eventRevision: Long, callbacks: AlbumDetailCallbacks, onOpen: (Route) -> Unit, ) { val library = item.asLibraryMedia() PhotosTile( item = library, prints = prints, selecting = state.selected != null, selected = item.id in state.selected.orEmpty(), media = media, dataRevision = eventRevision, onToggleSelected = { if (state.canEdit) callbacks.toggle(it) }, onOpen = onOpen, ) } private fun AlbumItem.asLibraryMedia(): LibraryMedia = LibraryMedia( id = id, kind = if (kind == com.cottondb.android.traffic.photos.albums.AlbumItemKind.Video) LibraryMediaKind.Video else LibraryMediaKind.Photo, key = drawing?.key, preview = drawing?.preview, posterKey = drawing?.posterKey, caption = null, description = null, takenAt = sortAt, sizeBytes = 0, contentType = null, favourite = false, people = emptyList(), projects = emptyList(), durationMs = null, ) @Composable private fun AlbumLoading() = Box(Modifier.fillMaxSize().padding(20.dp), contentAlignment = Alignment.TopCenter) { Box(Modifier.fillMaxWidth().aspectRatio(1f).background(CottonPalette.ground)) } @Composable private fun AlbumMissing() = Column(Modifier.fillMaxSize().padding(32.dp), verticalArrangement = Arrangement.Center) { Text("This album is not here", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), textAlign = TextAlign.Center) Text("It may have been dismissed or removed from another device.", style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary, textAlign = TextAlign.Center) } @Composable private fun AlbumEmpty() = Column(Modifier.fillMaxWidth().padding(top = 24.dp)) { Text("No pictures in this album", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif)) Text("Add some from your photos with the + circle.", style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary) } @Composable private fun AlbumProblem(message: String, retry: () -> Unit, enabled: Boolean) = Row(Modifier.fillMaxWidth().padding(horizontal = 20.dp)) { Text(message, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError) TextButton(onClick = retry, enabled = enabled) { Text("Try again") } }