310 lines · 15.6 KB · KotlinRaw
package com.cottondb.android.ui.photosimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.PaddingValuesimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.Spacerimport androidx.compose.foundation.layout.aspectRatioimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.safeDrawingPaddingimport androidx.compose.foundation.lazy.grid.GridCellsimport androidx.compose.foundation.lazy.grid.LazyVerticalGridimport androidx.compose.foundation.lazy.grid.itemsimport androidx.compose.foundation.lazy.grid.GridItemSpanimport androidx.compose.material3.DropdownMenuimport androidx.compose.material3.DropdownMenuItemimport androidx.compose.runtime.getValueimport androidx.compose.runtime.setValueimport androidx.compose.runtime.rememberimport androidx.compose.runtime.mutableStateOfimport androidx.compose.material3.OutlinedTextFieldimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.semantics.Roleimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.media.LocalMediaSourceimport com.cottondb.android.navigation.Routeimport com.cottondb.android.ui.media.MediaImageimport com.cottondb.android.traffic.photos.LibraryMediaimport com.cottondb.android.traffic.photos.LibraryMediaKindimport com.cottondb.android.traffic.photos.PhotosFilterimport com.cottondb.android.traffic.photos.PhotosLibraryPresentationimport 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<PhotosMonth>, 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<String> = 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. */@Composablefun 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) } }}@Composableprivate 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") } }, ) }}@Composableprivate 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, )}@Composableprivate 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) } }}@Composableinternal 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, )}@Composableprivate 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)) } }@Composableprivate 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,)@Composableprivate fun PhotosNoMatches() = PhotosNotice( "Nothing matches.", "This searches what your pictures say — your captions, and the descriptions Cotton wrote.",)@Composableprivate 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.",)@Composableprivate 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"