Cotton code
168 lines · 7.7 KB · SwiftRaw
// SEARCH (DECISIONS, 21 Nov 2026): the magnifier beside "more" opens a page// in the pen look with the keyboard up. Results come as the person types,// from the core's `days.find` over this phone's copy (contract §12): Coming// (from today on, soonest first, then Sometime), then Before (newest first),// each a normal Days row under its day, a done one struck through, a dropped// one marked Dropped. A tap closes search and lands on the item, marked a// moment; a tick follows the nothing-moves rule (`keep`). Nothing found says// the core's own line.#if os(iOS)import SwiftUIstruct DaysFindPage: View {    @Bindable var model: DaysModel    @State private var words = ""    @State private var found = DaysFound.none    @FocusState private var focused: Bool    var body: some View {        VStack(alignment: .leading, spacing: 0) {            HStack(spacing: 10) {                PenMagnifier().frame(width: 18, height: 18).foregroundStyle(Pen.quiet)                TextField("", text: $words, prompt: Text(DaysWords.line("search.hint")).foregroundColor(Pen.faint))                    .font(PenType.words).foregroundStyle(Pen.ink).tint(Pen.pen)                    .focused($focused)                    .submitLabel(.search)                    .autocorrectionDisabled()                    .frame(minHeight: 44)                    .accessibilityLabel(DaysWords.line("search.hint"))                Button { model.finding = false } label: {                    Text(DaysWords.line("search.close")).font(PenType.small).foregroundStyle(Pen.quiet)                        .padding(.horizontal, 4).frame(minHeight: 44).contentShape(Rectangle())                }                .buttonStyle(PenPress())            }            .padding(.horizontal, Pen.side)            DottedRule().padding(.horizontal, Pen.side)            ScrollView {                LazyVStack(alignment: .leading, spacing: 0) {                    if let empty = found.empty {                        Text(empty).font(PenType.words).foregroundStyle(Pen.quiet).padding(.top, 22)                    }                    part(DaysWords.line("search.coming"), found.coming, first: true)                    part(DaysWords.line("search.before"), found.before, first: found.coming.isEmpty)                }                .padding(.horizontal, Pen.side)                .padding(.bottom, 40)            }            .scrollIndicators(.hidden)        }        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)        .background(Pen.sheet.ignoresSafeArea())        .keyboardStaysOnTap()        .onAppear { focused = true }        // As the person types: the newest words' answer alone lands (a        // task asked for older words is cancelled and its answer dropped).        .task(id: words) {            let asked = words            guard !asked.trimmingCharacters(in: .whitespaces).isEmpty else { found = .none; return }            try? await Task.sleep(for: .milliseconds(120))            guard !Task.isCancelled else { return }            let answer = try? await model.book.find(asked, keep: model.kept)            guard !Task.isCancelled, asked == words, let answer else { return }            found = answer        }    }    /// One part (Coming or Before): its heading, then its rows under a quiet    /// day line wherever the day changes.    @ViewBuilder    private func part(_ name: String, _ rows: [DaysListed], first: Bool) -> some View {        if !rows.isEmpty {            PenGroupHeading(name: name, first: first)                .padding(.top, first ? 14 : DaysSpace.betweenDays - 16)            ForEach(Array(rows.enumerated()), id: \.element.id) { at, listed in                if at == 0 || rows[at - 1].dayWords != listed.dayWords {                    Text(listed.dayWords).font(PenType.suffix).foregroundStyle(Pen.quiet)                        .padding(.top, at == 0 ? 6 : 12)                        .accessibilityAddTraits(.isHeader)                }                DaysRowView(row: dropped(model.drawn(listed.row), listed.dropped), acts: acts(listed))            }        }    }    /// A dropped one is marked Dropped, after its words.    private func dropped(_ row: DaysRow, _ dropped: Bool) -> DaysRow {        guard dropped else { return row }        var out = row        let mark = DaysWords.line("row.dropped_mark")        out.suffix = row.suffix.map { "\($0) · \(mark)" } ?? mark        return out    }    private func acts(_ listed: DaysListed) -> DaysRowActs {        let model = model        return DaysRowActs(tick: { model.tick($0, standing: listed.day) }, open: { _ in model.land(listed) })    }}/// One of the four views chosen on Show (contract §11), in place of the/// days: its rows under a quiet day line wherever the day changes (Late's/// due days, Repeating's next days with the rhythm, Dropped with Bring back,/// Done by the day each was done, read a page at a time as the list nears/// its end). Its empty line is the core's.struct DaysViewList: View {    @Bindable var model: DaysModel    var body: some View {        ScrollView {            LazyVStack(alignment: .leading, spacing: 0) {                if let viewed = model.viewed {                    if viewed.rows.isEmpty, let empty = viewed.empty {                        Text(empty).font(PenType.words).foregroundStyle(Pen.quiet).padding(.top, 22)                    }                    ForEach(Array(viewed.rows.enumerated()), id: \.element.id) { at, listed in                        if at == 0 || viewed.rows[at - 1].dayWords != listed.dayWords {                            Text(listed.dayWords).font(PenType.suffix).foregroundStyle(Pen.quiet)                                .padding(.top, at == 0 ? 6 : 14)                                .accessibilityAddTraits(.isHeader)                        }                        HStack(alignment: .firstTextBaseline, spacing: 8) {                            DaysRowView(row: rhythm(model.drawn(listed.row), listed.rhythm),                                        acts: DaysRowActs(tick: { model.tick($0, standing: listed.day) },                                                          open: { row in if row.recordId != nil { model.sheet = .change(row) } }))                            if listed.dropped {                                PenChip(words: DaysWords.line("change.bring_back")) { model.bringBack(listed.row) }                            }                        }                        .onAppear {                            // Near the end of what is read: the next page (Done).                            if at >= viewed.rows.count - 20, viewed.next != nil { Task { await model.moreOfView() } }                        }                    }                }            }            .padding(.horizontal, Pen.side)            .padding(.top, 4)            .padding(.bottom, 110)        }        .scrollIndicators(.hidden)    }    /// Repeating's rhythm in words, as the row's quiet suffix.    private func rhythm(_ row: DaysRow, _ rhythm: String?) -> DaysRow {        guard let rhythm else { return row }        var out = row        out.suffix = rhythm        return out    }}/// The magnifier in the top bar: a ring and its handle, drawn in one pen line.struct PenMagnifier: View {    var body: some View {        Canvas { g, size in            let s = min(size.width, size.height)            var p = Path()            p.addEllipse(in: CGRect(x: s * 0.08, y: s * 0.08, width: s * 0.6, height: s * 0.6))            p.move(to: CGPoint(x: s * 0.6, y: s * 0.6))            p.addLine(to: CGPoint(x: s * 0.92, y: s * 0.92))            g.stroke(p, with: .foreground, style: StrokeStyle(lineWidth: 1.6, lineCap: .round))        }        .accessibilityHidden(true)    }}#endif