package com.cottondb.android.ui.photos.review 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.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.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items 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.decide.DecideDuplicate import com.cottondb.android.decide.DecideDuplicates import com.cottondb.android.decide.DecideFace import com.cottondb.android.decide.DecideFaces import com.cottondb.android.decide.DecidePhoto import com.cottondb.android.decide.DecidePhotoPresentationFacts import com.cottondb.android.decide.DecisionSlip 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.traffic.photos.review.PhotoReviewContent import com.cottondb.android.traffic.photos.review.PhotoReviewKind import com.cottondb.android.traffic.photos.review.PhotoReviewState import com.cottondb.android.traffic.photos.details.MediaDetailFacts import com.cottondb.android.ui.media.MediaImageCrop import com.cottondb.android.ui.media.MediaImage import com.cottondb.android.ui.paper.MemoryPaperDye import com.cottondb.android.ui.paper.PaperEdge import com.cottondb.android.ui.paper.TornPaper import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.longOrNull /** * Controlled direct review leaf for `/photos/duplicates` and `/photos/people`. * * Its host owns the retained direct-read model and the one existing Decide write * boundary. Media images only resolve bytes already held by this phone. */ @Composable fun PhotoReviewScreen( kind: PhotoReviewKind, state: PhotoReviewState, media: LocalMediaSource, dataRevision: Long, prints: Boolean, onPrints: (Boolean) -> Unit, onBack: () -> Unit, onRefresh: () -> Unit, onDecide: (DecisionSlip, Boolean) -> Unit, onPhoto: (String) -> Unit, modifier: Modifier = Modifier, ) { val slips = state.content?.slips.orEmpty() Column( modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding(), ) { ReviewHeader(kind, slips.isNotEmpty(), prints, onPrints, onBack) ReviewNotices(state, onRefresh) when { state.content == null && (state.loading || state.readProblem == null) -> ReviewSkeleton(kind) slips.isEmpty() && state.content != null -> ReviewEmpty(kind) kind == PhotoReviewKind.Duplicates -> DuplicateList( state = state, media = media, dataRevision = dataRevision, onDecide = onDecide, onPhoto = onPhoto, ) else -> FaceList( state = state, media = media, dataRevision = dataRevision, prints = prints, onDecide = onDecide, onPhoto = onPhoto, ) } } } @Composable private fun ReviewHeader( kind: PhotoReviewKind, hasRows: Boolean, prints: Boolean, onPrints: (Boolean) -> Unit, onBack: () -> Unit, ) { Row( Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") } Spacer(Modifier.weight(1f)) if (kind == PhotoReviewKind.Faces && hasRows) { TextButton( onClick = { onPrints(!prints) }, modifier = Modifier.semantics { contentDescription = "Change people layout" }, ) { Text(if (prints) "List" else "Prints") } } } Text( text = if (kind == PhotoReviewKind.Duplicates) "Duplicates" else "People to confirm", modifier = Modifier.padding(horizontal = 20.dp, vertical = 4.dp), style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, ) } @Composable private fun ReviewNotices(state: PhotoReviewState, onRefresh: () -> Unit) { state.readProblem?.let { problem -> Text( problem, Modifier.padding(horizontal = 20.dp, vertical = 4.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError, ) } state.writeProblem?.let { problem -> Text( problem, Modifier.padding(horizontal = 20.dp, vertical = 4.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError, ) } if (state.outcomeUnknown || state.needsRefresh || state.readProblem != null) { TextButton( onClick = onRefresh, enabled = !state.loading && !state.writing, modifier = Modifier.padding(horizontal = 12.dp).semantics { contentDescription = "Try again" }, ) { Text("Try again") } } state.receipt?.let { receipt -> Text( receipt, Modifier.padding(horizontal = 20.dp, vertical = 4.dp), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 16.sp), color = CottonPalette.statusSuccess, ) } } @Composable private fun ReviewSkeleton(kind: PhotoReviewKind) { val id = if (kind == PhotoReviewKind.Duplicates) "duplicates" else "people-to-confirm" Column( Modifier.fillMaxWidth().padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp), ) { repeat(3) { index -> TornPaper( "$id-skeleton-$index", MemoryPaperDye.Cream, Modifier.fillMaxWidth().height(96.dp), edge = PaperEdge.Cut, ) {} } } } @Composable private fun ReviewEmpty(kind: PhotoReviewKind) { val (title, body) = when (kind) { PhotoReviewKind.Duplicates -> "Nothing to review." to "When the Mac finds pictures that look like copies of each other, they are listed here for you to decide. Nothing is ever deleted for you." PhotoReviewKind.Faces -> "Nobody to confirm" to "When the Mac thinks it recognises someone in a picture, it asks you here. It never decides on its own." } Column( Modifier.fillMaxWidth().padding(28.dp), verticalArrangement = Arrangement.spacedBy(8.dp), ) { Text(title, style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink) Text(body, style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary) } } @Composable private fun DuplicateList( state: PhotoReviewState, media: LocalMediaSource, dataRevision: Long, onDecide: (DecisionSlip, Boolean) -> Unit, onPhoto: (String) -> Unit, ) { val content = state.content as? PhotoReviewContent.Duplicates ?: return val pairs = duplicatePairs(content.read, content.slips) LazyColumn( Modifier.fillMaxSize(), contentPadding = androidx.compose.foundation.layout.PaddingValues(20.dp), verticalArrangement = Arrangement.spacedBy(18.dp), ) { items(pairs, key = { it.slip.id }) { pair -> DuplicatePair(pair, state.canAct, media, dataRevision, onDecide, onPhoto) } } } private data class DuplicatePair( val slip: DecisionSlip, val copy: DecideDuplicate, val keep: DecidePhoto?, val duplicate: DecidePhoto?, ) private fun duplicatePairs(read: DecideDuplicates, slips: List): List = read.groups.flatMap { group -> group.copies.mapNotNull { copy -> val proposalId = copy.id ?: return@mapNotNull null val slip = slips.firstOrNull { it.id == "duplicate:$proposalId" } ?: return@mapNotNull null DuplicatePair( slip = slip, copy = copy, keep = read.photos.firstOrNull { it.id == group.keepId }, duplicate = copy.copyId?.let { id -> read.photos.firstOrNull { it.id == id } }, ) } } @Composable private fun DuplicatePair( pair: DuplicatePair, canAct: Boolean, media: LocalMediaSource, dataRevision: Long, onDecide: (DecisionSlip, Boolean) -> Unit, onPhoto: (String) -> Unit, ) { Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) { DuplicateSide("Keep", pair.keep, media, dataRevision, onPhoto, Modifier.weight(1f)) DuplicateSide("Copy", pair.duplicate, media, dataRevision, onPhoto, Modifier.weight(1f)) } Text( DecidePhotoPresentationFacts.distanceLine(pair.copy.distance, pair.copy.method), style = CottonTextStyles.caption, color = CottonPalette.inkTertiary, ) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { ReviewAction( "Keep this one", "Keep this one — the other is a copy", canAct, Modifier.weight(1f), ) { onDecide(pair.slip, true) } ReviewAction( "They are different", "They are different pictures", canAct, Modifier.weight(1f), ) { onDecide(pair.slip, false) } } } } @Composable private fun DuplicateSide( label: String, photo: DecidePhoto?, media: LocalMediaSource, dataRevision: Long, onPhoto: (String) -> Unit, modifier: Modifier, ) { Column(modifier, verticalArrangement = Arrangement.spacedBy(4.dp)) { Text(label, style = CottonTextStyles.caption, color = CottonPalette.inkTertiary) if (photo == null) { Box(Modifier.fillMaxWidth().aspectRatio(1f).background(CottonPalette.ground)) } else { MediaImage( photo.key, media, dataRevision, 150.dp, Modifier.fillMaxWidth().aspectRatio(1f).clickable(role = Role.Button) { onPhoto(photo.id) }, "Open the picture", ) } Text( duplicateFacts(photo), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary, ) } } private fun duplicateFacts(photo: DecidePhoto?): String { if (photo == null) return "This picture is gone" val parts = buildList { val width = photo.raw.longOrNull("width") val height = photo.raw.longOrNull("height") if (width != null && height != null) add("$width × $height") MediaDetailFacts.size(photo.raw.longOrNull("sizeBytes"))?.let(::add) if (photo.raw.booleanOrFalse("favourite")) add("Favourite") } return parts.joinToString(" · ") } @Composable private fun FaceList( state: PhotoReviewState, media: LocalMediaSource, dataRevision: Long, prints: Boolean, onDecide: (DecisionSlip, Boolean) -> Unit, onPhoto: (String) -> Unit, ) { val content = state.content as? PhotoReviewContent.Faces ?: return val rows = faceRows(content.read, content.slips) if (prints) { LazyVerticalGrid( columns = GridCells.Fixed(2), contentPadding = androidx.compose.foundation.layout.PaddingValues(20.dp), horizontalArrangement = Arrangement.spacedBy(18.dp), verticalArrangement = Arrangement.spacedBy(18.dp), ) { items(rows, key = { it.slip.id }) { row -> FaceCard(row, state.canAct, media, dataRevision, onDecide, onPhoto, true) } } } else { LazyColumn( Modifier.fillMaxSize(), contentPadding = androidx.compose.foundation.layout.PaddingValues(20.dp), verticalArrangement = Arrangement.spacedBy(14.dp), ) { items(rows, key = { it.slip.id }) { row -> FaceCard(row, state.canAct, media, dataRevision, onDecide, onPhoto, false) } } } } private data class FaceRow(val slip: DecisionSlip, val face: DecideFace, val photo: DecidePhoto?) private fun faceRows(read: DecideFaces, slips: List): List = read.faces.mapNotNull { face -> val slip = slips.firstOrNull { it.id == "face:${face.id}" } ?: return@mapNotNull null FaceRow(slip, face, read.photos.firstOrNull { it.id == face.recordId }) } @Composable private fun FaceCard( row: FaceRow, canAct: Boolean, media: LocalMediaSource, dataRevision: Long, onDecide: (DecisionSlip, Boolean) -> Unit, onPhoto: (String) -> Unit, print: Boolean, ) { TornPaper( row.slip.id, MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut, ) { if (print) { Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { FacePicture(row, media, dataRevision, onPhoto, Modifier.fillMaxWidth().aspectRatio(1f)) FaceWords(row, canAct, onDecide) } } else { Row(Modifier.padding(12.dp), horizontalArrangement = Arrangement.spacedBy(14.dp)) { FacePicture(row, media, dataRevision, onPhoto, Modifier.size(64.dp)) Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) { FaceWords(row, canAct, onDecide) } } } } } @Composable private fun FacePicture( row: FaceRow, media: LocalMediaSource, dataRevision: Long, onPhoto: (String) -> Unit, modifier: Modifier, ) { MediaImage( row.photo?.key, media, dataRevision, 160.dp, modifier.clickable(role = Role.Button) { onPhoto(row.face.recordId) }, "Open the picture", crop = MediaImageCrop(row.face.x, row.face.y, row.face.w.coerceAtLeast(.01), row.face.h.coerceAtLeast(.01)), ) } @Composable private fun FaceWords(row: FaceRow, canAct: Boolean, onDecide: (DecisionSlip, Boolean) -> Unit) { val name = row.face.suggestedName?.takeIf(String::isNotBlank) ?: "someone you have named" Text( "Is this $name?", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif, fontSize = 18.sp), color = CottonPalette.ink, ) Text( DecidePhotoPresentationFacts.faceHedge(row.face.distance), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 15.sp), color = CottonPalette.inkSecondary, ) Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { ReviewAction("Confirm", "Yes, this is $name", canAct) { onDecide(row.slip, true) } ReviewAction("Not them", "No, not $name", canAct) { onDecide(row.slip, false) } } } @Composable private fun ReviewAction( label: String, description: String, enabled: Boolean, modifier: Modifier = Modifier, onClick: () -> Unit, ) { Text( label, modifier.padding(vertical = 10.dp).semantics { contentDescription = description } .clickable(enabled = enabled, role = Role.Button, onClick = onClick), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), color = if (enabled) CottonPalette.ink else CottonPalette.inkTertiary, textAlign = TextAlign.Center, ) } private fun kotlinx.serialization.json.JsonObject.longOrNull(key: String): Long? = (this[key] as? JsonPrimitive)?.takeIf { !it.isString }?.longOrNull private fun kotlinx.serialization.json.JsonObject.booleanOrFalse(key: String): Boolean = (this[key] as? JsonPrimitive)?.takeIf { !it.isString }?.content == "true"