// SEARCH IS A DECK (coordinator's ruling, 23 Oct): the hits are cards, each // drawn with its own table's face, under one chip per table that has hits. // A swipe moves card; past a chip's last card the deck goes on to the next // chip's first, as a tool's deck rolls over its views. A tap opens the record // by the same grammar as its tool's deck. A hit whose table has no card face yet is drawn as // the search's own hit card on the deck's paper. import CottonKit import CottonPaper import CottonValues import SwiftUI /// One hit on the search deck: the record its card is read from (the core's /// `record`), with the hit and its section's words. struct SearchDeckHit: Identifiable, Equatable { let table: String let record: String let hit: FindHit let section: String let words: String var id: String { "\(table)|\(record)" } /// The hits of a search answer, in its order, one card per record; a hit /// that stands for no record has no card. static func of(_ items: [SearchItem]) -> [SearchDeckHit] { var seen = Set() return items.compactMap { item -> SearchDeckHit? in guard case .hit(_, let hit, let section, let words) = item, let card = hit.card else { return nil } return SearchDeckHit(table: card.table, record: card.id, hit: hit, section: section, words: words) } .filter { seen.insert($0.id).inserted } } } struct SearchDeck: View { let hits: [SearchDeckHit] /// The SETTLED query, for a hit card's underline. let query: String let open: (SearchDeckHit) -> Void @Environment(Navigator.self) private var navigator: Navigator? @Environment(\.recordsRevision) private var revision @Environment(\.records) private var store @Environment(\.tableDefinitions) private var definitions @Environment(\.theme) private var theme @Environment(\.cottonHaptics) private var haptics @Environment(\.viewerPresenter) private var viewerPresenter /// The hit opened in place (`OpenedInPlace`): while it stands the deck /// holds every move. @State private var opened: String? @State private var reads = false @State private var chip: String? @State private var at = 0 @State private var drag: CGFloat = 0 /// Each hit's record as read, with its card; nil once read without one. @State private var read: [String: TableRecord?] = [:] /// The tables with hits, in the order their first hit came. private var tables: [String] { var seen = Set() return hits.map(\.table).filter { seen.insert($0).inserted } } private var shown: String? { chip.flatMap { tables.contains($0) ? $0 : nil } ?? tables.first } private var cards: [SearchDeckHit] { hits.filter { $0.table == shown } } var body: some View { let cards = cards let at = min(self.at, max(0, cards.count - 1)) VStack(spacing: 0) { chips .padding(.bottom, 14) if cards.indices.contains(at) { GeometryReader { box in let width = max(0, min(box.size.width, box.size.height * CardPaperKnobs.aspect)) card(cards[at], zones: DeckZones(box.frame(in: .global).insetBy(dx: -DeckKnobs.sideInset, dy: 0))) .frame(width: width, height: width / CardPaperKnobs.aspect) .id(cards[at].id) .transition(.opacity) .offset(x: drag) .rotationEffect(.degrees(Double(drag) / 40)) .frame(width: box.size.width, height: box.size.height, alignment: .top) } .padding(.horizontal, DeckKnobs.sideInset) .task(id: cards.map(\.id)[max(0, at - 1)...min(cards.count - 1, at + 2)].joined(separator: ",")) { for hit in cards[max(0, at - 1)...min(cards.count - 1, at + 2)] where read[hit.id] == nil { read[hit.id] = .some(try? await store?.get(table: hit.table, id: hit.record)) } } } Text(ShellLines.line("cards", "deck.position", ["at": String(at + 1), "of": String(cards.count)])) .font(CottonFonts.type(DeckKnobs.positionSize)) .foregroundStyle(theme.print(.pencil)) .padding(.vertical, 10) .accessibilityIdentifier("search-deck-position") } .gesture( DragGesture(minimumDistance: 14) .onChanged { value in guard opened == nil, abs(value.translation.width) > abs(value.translation.height) else { return } drag = value.translation.width } .onEnded { value in guard opened == nil else { return } let width = value.translation.width if width < -DeckKnobs.swipe { move(1) } else if width > DeckKnobs.swipe { move(-1) } withAnimation(.snappy) { drag = 0 } }) .onChange(of: hits.map(\.id)) { _, _ in self.at = 0 } .accessibilityIdentifier("search-deck") } // MARK: The chips private var chips: some View { ScrollView(.horizontal) { HStack(spacing: 8) { ForEach(tables, id: \.self) { table in let on = table == shown Button { withAnimation(.snappy(duration: 0.24)) { chip = table at = 0 } } label: { Text(definitions[table]?.plural ?? label(of: table)) .font(CottonFonts.type(15)) .foregroundStyle(on ? theme.sheet(.cream) : theme.print(.ink)) .padding(.horizontal, 16) .padding(.vertical, 8) .background(Capsule().fill(on ? theme.print(.ink) : theme.sheet(.cream))) .softShadow() } .buttonStyle(.plain) .accessibilityAddTraits(on ? .isSelected : []) .accessibilityIdentifier("search-chip-\(table)") } } .padding(.horizontal, DeckKnobs.sideInset) .padding(.vertical, 4) } .scrollIndicators(.hidden) } /// A table the app holds no definition of: its hits' section's words. private func label(of table: String) -> String { hits.first { $0.table == table }?.words ?? table } // MARK: A card @ViewBuilder private func card(_ hit: SearchDeckHit, zones: DeckZones) -> some View { if case .some(.some(let record)) = read[hit.id], let card = DeckCard(record) { // What the card holds opens as the owner's deck opens it, the // viewer walking this table's cards read so far (`FullScreenRoute`). let definition = definitions[hit.table] let opening = DeckOpening.of(record, card: card, in: cards.compactMap { read[$0.id] ?? nil }, definition: definition, presenter: viewerPresenter) let isOpened = opened == hit.id DeckFace.view(for: card, record: record) {} .modifier(SearchOpensInPlace(opened: isOpened, record: record, card: card, definition: definition) { if opened == hit.id { opened = nil } }) .environment(\.faceChoiceTable, hit.table) .contentShape(Rectangle()) .deckZoned(zones, move: move) { // Opened, the card's taps are its parts' (`deckEditable`). guard opened == nil else { return } if let definition, definition.card?.opens?.opens(record) == true, let navigator { navigator.enter(WholeLevel(table: definition.id, id: record.id, title: WholePath.title(of: record, in: definition))) } else if definition?.drawsConversation == true, let definition { navigator?.push(BoardRecord.route(table: definition.id, id: record.id, definition: definition)) } else if opening.viewer != nil { opening.viewer?() } else if case .reading = opening { opening.open($reads) } else if definition != nil { opened = hit.id } } .deckMoveActions(move) .onLongPressGesture(minimumDuration: DeckEditableKnobs.hold) { if definition != nil { opened = hit.id } } .task(id: revision) { if let fresh = try? await store?.get(table: hit.table, id: hit.record) { read[hit.id] = fresh } } .viewerSource(definition.map { DeckViewer.itemID($0, record.id) } ?? hit.id) .pinchOpens(opening, opened: $reads) } else { // A hit not read as its record: the found words on the cream stock. SearchHitCard(hit: hit.hit, section: hit.section, words: hit.words, query: query) { open(hit) } .onCardPaper(.cream) } } // MARK: Moving /// The next or previous card; past a chip's ends, on to the neighbouring /// chip's first (or last) card. private func move(_ step: Int) { // An opened card holds its deck: nothing moves it. guard opened == nil else { return } let cards = cards let next = at + step if cards.indices.contains(next) { withAnimation(.snappy(duration: 0.28)) { at = next } return } let tables = tables guard let here = shown.flatMap(tables.firstIndex), tables.indices.contains(here + step) else { return } haptics.select() let into = tables[here + step] withAnimation(.snappy(duration: 0.28)) { chip = into at = step > 0 ? 0 : max(0, hits.filter { $0.table == into }.count - 1) } } } /// A hit opened in place, where its table's definition is held; with none /// there is nothing to open, and no editing target rides down. private struct SearchOpensInPlace: ViewModifier { let opened: Bool let record: TableRecord let card: DeckCard let definition: TableDefinition? let close: () -> Void func body(content: Content) -> some View { if let definition { content.opensInPlace(opened, definition: definition, record: record, worn: card.face, close: close) } else { content.environment(\.deckCardTarget, nil) } } }