import CottonValues import SwiftUI // A Stories face over the same records and polaroids as the shared list. // Opening a person narrows that list; opening a print opens its record page. struct StoriesPictureGrid: View { let definition: TableDefinition let records: [TableRecord] let personNames: [String: String] let strip: Bool let hasMore: Bool let onLoadMore: () -> Void let onOpenGroup: (StoriesPresentation.Group) -> Void let onOpenRecord: (TableRecord) -> Void private var groups: [StoriesPresentation.Group] { let personColumn = definition.group?.column ?? "person_id" return StoriesPresentation.groups(records, personColumn: personColumn, personNames: personNames) } var body: some View { if strip { ScrollView(.horizontal) { LazyHStack(alignment: .top, spacing: 16) { ForEach(groups) { group in ForEach(group.records) { record in print(record, fills: false) } } nextPage } .padding(.vertical, 4) } .scrollIndicators(.hidden) } else { LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: max(1, definition.grid?.columns ?? 3)), alignment: .leading, spacing: 16) { ForEach(groups) { group in Section { ForEach(group.records) { record in print(record, fills: true) } } header: { Button { onOpenGroup(group) } label: { HStack { Text(group.title).paperTitle(20) Spacer() Text("›").paperBody(18, ink: .pencil) } .frame(minHeight: 44) .contentShape(Rectangle()) } .buttonStyle(OpacityButtonStyle()) .accessibilityIdentifier("stories-person-\(group.records.first?.id ?? "")") } } nextPage } } } @ViewBuilder private var nextPage: some View { if hasMore { Color.clear.frame(width: 1, height: 1) .id(records.count) .onAppear(perform: onLoadMore) } } private func print(_ record: TableRecord, fills: Bool) -> some View { let shown = StoriesPresentation.displayed(record, mediaColumns: [definition.slip.photo].compactMap { $0 }) let printDefinition = StoriesPresentation.pictureDefinition(definition) return Button { onOpenRecord(record) } label: { PolaroidFace(definition: printDefinition, record: shown, width: 160, fills: fills) .overlay(alignment: .topTrailing) { if let stories = definition.stories, StoriesPresentation.isVideo(record, stories), StoriesPresentation.mediaKey(of: record) != nil { Image(systemName: "play.fill").padding(8).accessibilityHidden(true) } } .contentShape(Rectangle()) } .buttonStyle(MotionButtonStyle()) .accessibilityLabel((record.said?.slip.spoken ?? definition.name)) .accessibilityIdentifier("story-\(record.id)") } }