Cotton code
215 lines · 11.4 KB · SwiftRaw
// SEARCH AND THE CHOOSER (sites/DECISIONS.md "Search", "A link from another app"; sites.html): nothing is sent while// he types, and nothing is suggested from the internet: only his own sites are matched, on the phone. Results open in// a throwaway place that keeps nothing. A result or a link opens only where he chooses: his sites at that address// first, then Quick visit, then a new site, then another of his sites. Nothing loads until he picks.#if os(iOS)import SwiftUIimport UIKitstruct SitesSearchPage: View {    @Bindable var model: SitesModel    @State private var q = ""    @State private var found: SitesFound?    @FocusState private var focused: Bool    var body: some View {        VStack(alignment: .leading, spacing: 0) {            HStack {                Spacer()                Button(SitesWords.line("add.cancel")) { model.quickNext = false; model.place = nil }                    .font(SitesType.sans(16, .semibold)).foregroundStyle(Cranberry.ink).buttonStyle(.plain)                    .accessibilityIdentifier("sites-search-cancel")            }            .frame(minHeight: 40)            HStack(spacing: 8) {                SitesGlyph(icon: .search, size: 20).foregroundStyle(Cranberry.ink)                TextField(SitesWords.line("search.line"), text: $q)                    .font(SitesType.bodySmall)                    .foregroundStyle(Cranberry.ink)                    .tint(Cranberry.accent)                    .focused($focused)                    .keyboardType(.webSearch)                    .textInputAutocapitalization(.never)                    .autocorrectionDisabled()                    .submitLabel(.search)                    .onSubmit { if let first = found?.first, !q.isEmpty { Task { await model.go(first) } } }                    .accessibilityIdentifier("sites-search-field")                if !q.isEmpty {                    Button { q = "" } label: { SitesGlyph(icon: .close, size: 18).foregroundStyle(Cranberry.quiet) }                        .buttonStyle(.plain)                        .accessibilityLabel(SitesWords.line("size.reset"))                }            }            .padding(.horizontal, 12).padding(.vertical, 11)            .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Cranberry.ink, lineWidth: 1))            ScrollView {                VStack(alignment: .leading, spacing: 0) {                    if let found {                        if !q.isEmpty || UIPasteboard.general.hasURLs { firstLine(found.first) }                        if !found.matches.isEmpty {                            SitesGroupHead(words: found.inYours)                            ForEach(Array(found.matches.enumerated()), id: \.element.id) { at, row in                                Button { Task { await model.open(row) } } label: {                                    HStack(spacing: 12) {                                        SitesLetter(letter: row.letter, kind: row.hidden ? .hidden : .plain)                                        VStack(alignment: .leading, spacing: 2) {                                            Text(row.label).font(SitesType.name).foregroundStyle(Cranberry.ink)                                            Text(row.line).font(SitesType.line).foregroundStyle(Cranberry.quiet)                                        }                                        .frame(maxWidth: .infinity, alignment: .leading)                                        SitesGlyph(icon: .next, size: 18).foregroundStyle(Cranberry.quiet)                                    }                                    .padding(.vertical, 11).contentShape(Rectangle())                                }                                .buttonStyle(.plain)                                .accessibilityElement(children: .combine)                                .accessibilityIdentifier("sites-match-\(row.id)")                                if at < found.matches.count - 1 { SitesRule() }                            }                        } else if let none = found.none {                            Text(none).font(SitesType.line).foregroundStyle(Cranberry.quiet).padding(.top, 18)                                .accessibilityIdentifier("sites-search-none")                        }                    }                }                .padding(.top, 10)            }            .scrollDismissesKeyboard(.interactively)        }        .padding(.horizontal, Cranberry.side)        .onAppear { focused = true }        .onChange(of: model.sheet) { _, raised in if raised != nil { focused = false } }        .task(id: q) { found = try? await model.book.search(q, unlocked: model.unlocked) }    }    /// "Search for '…'" with the engine and the place that keeps nothing; "Go to ledgerbank.example"; or "Open the copied link".    private func firstLine(_ first: SitesFound.First) -> some View {        Button { Task { await model.go(first) } } label: {            HStack(spacing: 12) {                SitesGlyph(icon: first.kind == "search" ? .search : (first.kind == "address" ? .open : .copy)).frame(minWidth: 32)                VStack(alignment: .leading, spacing: 2) {                    Text(first.label).font(SitesType.name).foregroundStyle(Cranberry.ink)                    if let line = first.line { Text(line).font(SitesType.line).foregroundStyle(Cranberry.quiet) }                }                .frame(maxWidth: .infinity, alignment: .leading)            }            .foregroundStyle(Cranberry.ink)            .padding(.vertical, 11).contentShape(Rectangle())        }        .buttonStyle(.plain)        .accessibilityElement(children: .combine)        .accessibilityIdentifier("sites-search-first")    }}/// "Open this in": the whole link with any one-time code masked; his sites at that address first, each with the/// account it holds; Hidden behind Face ID; Quick visit; a new site; another of his sites. Nothing loads until he picks.struct SitesChooser: View {    @Bindable var model: SitesModel    let choice: SitesChoice    let moving: Bool    let from: String?    @State private var others = false    var body: some View {        VStack(alignment: .leading, spacing: 0) {            Text(choice.title).font(SitesType.sheetTitle).foregroundStyle(Cranberry.ink)                .padding(.top, 4)                .accessibilityAddTraits(.isHeader)                .accessibilityIdentifier("sites-choose-title")            linkBox            if let once = choice.once {                Text(once).font(SitesType.line).foregroundStyle(Cranberry.quiet)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.top, 10)                    .accessibilityIdentifier("sites-choose-once")            }            if !choice.atSite.rows.isEmpty {                SitesGroupHead(words: choice.atSite.label)                rows(choice.atSite.rows)            }            if choice.hidden.locked {                SitesRule().padding(.top, choice.atSite.rows.isEmpty ? 14 : 0)                Button { Task { if await model.unlock() { await model.choose(choice.url, moving: moving, from: from) } } } label: {                    SitesPlainRow(icon: .lock, label: choice.hidden.label, line: choice.hidden.line)                }                .buttonStyle(.plain)                .accessibilityIdentifier("sites-choose-hidden")            } else if let hidden = choice.hidden.rows, !hidden.isEmpty {                SitesGroupHead(words: SitesWords.line("hidden.showing"))                rows(hidden, hidden: true)            }            SitesRule().padding(.top, 14)            Button { Task { model.sheet = nil; await model.openQuick(choice.url) } } label: {                SitesPlainRow(icon: .quick, label: choice.quick.label, line: choice.quick.line)            }            .buttonStyle(.plain)            .accessibilityIdentifier("sites-choose-quick")            SitesRule()            Button { model.sheet = .add(home: choice.url, thenOpen: choice.url) } label: {                SitesPlainRow(icon: .plus, label: choice.newSite.label, line: choice.newSite.line)            }            .buttonStyle(.plain)            .accessibilityIdentifier("sites-choose-new")            if !choice.others.rows.isEmpty {                SitesRule()                Button { withAnimation(.easeOut(duration: 0.2)) { others.toggle() } } label: {                    HStack(spacing: 12) {                        Color.clear.frame(width: 32, height: 1)                        Text(choice.others.label).font(SitesType.sans(16, .medium)).foregroundStyle(Cranberry.ink)                            .frame(maxWidth: .infinity, alignment: .leading)                        SitesGlyph(icon: .next, size: 18).foregroundStyle(Cranberry.quiet).rotationEffect(.degrees(others ? 90 : 0))                    }                    .padding(.vertical, 12).contentShape(Rectangle())                }                .buttonStyle(.plain)                .accessibilityIdentifier("sites-choose-others")                if others { rows(choice.others.rows) }            }            Text(choice.nothingYet).font(SitesType.line).foregroundStyle(Cranberry.quiet)                .padding(.top, 14)        }    }    /// `.linkbox`: the address in bold, the rest quiet, the one-time code already masked by the core.    private var linkBox: some View {        let parts = URLComponents(string: choice.shown)        let host = parts?.host.map { $0.hasPrefix("www.") ? String($0.dropFirst(4)) : $0 } ?? choice.shown        let rest = parts.map { c in            (c.port.map { ":\($0)" } ?? "") + c.percentEncodedPath + (c.percentEncodedQuery.map { "?\($0)" } ?? "")        } ?? ""        return (Text(host).font(SitesType.sans(14.5, .bold)).foregroundColor(Cranberry.ink)                + Text(rest.removingPercentEncoding ?? rest).font(SitesType.sans(14.5)).foregroundColor(Cranberry.quiet))            .fixedSize(horizontal: false, vertical: true)            .frame(maxWidth: .infinity, alignment: .leading)            .padding(.horizontal, 13).padding(.vertical, 11)            .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Cranberry.rule, style: StrokeStyle(lineWidth: 1, dash: [4, 3])))            .padding(.top, 12)            .accessibilityIdentifier("sites-choose-link")    }    private func rows(_ rows: [SitesRow], hidden: Bool = false) -> some View {        VStack(spacing: 0) {            ForEach(Array(rows.enumerated()), id: \.element.id) { at, row in                Button { Task { await model.picked(row, in: choice, from: from) } } label: {                    HStack(spacing: 12) {                        SitesLetter(letter: row.letter, kind: hidden ? .hidden : .plain)                        VStack(alignment: .leading, spacing: 2) {                            Text(row.label).font(SitesType.name).foregroundStyle(Cranberry.ink)                            Text(row.login ?? row.line).font(SitesType.line).foregroundStyle(Cranberry.quiet).lineLimit(1)                        }                        .frame(maxWidth: .infinity, alignment: .leading)                        SitesGlyph(icon: .next, size: 18).foregroundStyle(Cranberry.quiet)                    }                    .padding(.vertical, 11).contentShape(Rectangle())                }                .buttonStyle(.plain)                .accessibilityElement(children: .combine)                .accessibilityIdentifier("sites-choose-\(row.id)")                if at < rows.count - 1 { SitesRule() }            }        }    }}#endif