Cotton code
159 lines · 7.2 KB · SwiftRaw
// A PERSON (assets/design/contacts/SCREENS.md "The person page"; `contacts.person {id}`): Edit at the top right; the// mark, the name and how they stand; Between you (each row opens that app on them), Ways to reach (always; Call or// Email, then the row's own acts), Groups, At, Same person?, From; every other act at the foot as a full-width line,// asked first when it carries `confirm`. A merged-away id opens the one it was merged into.#if os(iOS)import CottonKitimport SwiftUIstruct ContactsPersonScreen: View {    let model: ContactsModel    @State var id: String    @State private var read: ContactsRead<ContactsPerson>?    @Environment(\.openURL) private var openURL    var body: some View {        ContactsPage(tag: "contacts-person") {            switch read {            case .read(let person)?: page(person)            case .refused(let line)?: ContactsRefused(line: line) { model.changed() }            case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40)            }        }        .toolbar {            if let edit = read?.answer?.acts?.first(where: { $0.id == "edit" }) {                ToolbarItem(placement: .topBarTrailing) {                    Button(edit.words) { model.tap(edit) }                        .foregroundStyle(ContactsInk.accent)                        .accessibilityIdentifier("contacts-person-edit")                }            }        }        .task(id: "\(id)|\(model.revision)") {            let answer = await model.calls.person(id)            if let merged = answer.answer?.mergedInto, merged != id { id = merged; return }            read = answer        }    }    @ViewBuilder private func page(_ person: ContactsPerson) -> some View {        VStack(spacing: 8) {            ContactsRing(initials: person.initials ?? ContactsStandIn.initials(person.title), size: 64)            Text(verbatim: person.title).font(.system(size: 26, weight: .bold)).foregroundStyle(ContactsInk.ink).multilineTextAlignment(.center)                .accessibilityAddTraits(.isHeader)                .accessibilityIdentifier("contacts-person-title")            if let subtitle = person.subtitle {                Text(verbatim: subtitle).font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet).multilineTextAlignment(.center)            }        }        .frame(maxWidth: .infinity)        .padding(.top, 4)        if let between = person.between, !between.isEmpty {            heading("person.between")            ForEach(Array(between.enumerated()), id: \.element.id) { at, one in                Button { open(one, person: person.id) } label: { ContactsRowView(title: one.words, detail: one.detail, last: at == between.count - 1) }                    .buttonStyle(ContactsPress())            }        }        heading("person.reach")        let reach = person.reach ?? []        if reach.isEmpty { ContactsNote(text: ContactsWords.line("person.no_reach")).padding(.vertical, 8) }        ForEach(Array(reach.enumerated()), id: \.element.id) { at, one in            Button { dial(one) } label: {                ContactsRowView(title: one.words, detail: one.detail, last: at == reach.count - 1) {                    HStack(spacing: 12) {                        if one.kind == "phone" || one.kind == "email" {                            Text(verbatim: ContactsWords.line(one.kind == "phone" ? "person.call" : "person.email"))                                .font(.system(.subheadline).weight(.semibold)).foregroundStyle(ContactsInk.accent)                        }                        ForEach(one.acts ?? []) { act in                            Button(act.words) { model.tap(act) }                                .font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet)                                .frame(minHeight: 44)                        }                    }                }            }            .buttonStyle(ContactsPress())            .accessibilityIdentifier("contacts-reach-\(one.id)")        }        if let groups = person.groups, !groups.isEmpty {            heading("person.groups")            links(groups)        }        if let organisation = person.organisation {            heading("person.organisation")            links([organisation])        }        if let same = person.same, !same.isEmpty {            heading("person.same")            links(same)        }        if let sources = person.sources, !sources.isEmpty {            heading("person.sources")            ForEach(Array(sources.enumerated()), id: \.offset) { at, one in                ContactsRowView(title: one.words, detail: one.detail, last: at == sources.count - 1) { EmptyView() }            }        }        let foot = (person.acts ?? []).filter { $0.id != "edit" }        if !foot.isEmpty {            VStack(spacing: 0) {                ForEach(foot) { act in                    Button { model.tap(act) } label: {                        Text(verbatim: act.words)                            .font(.system(.callout).weight(act.red == true ? .semibold : .regular))                            .foregroundStyle(act.red == true ? ContactsInk.ink : ContactsInk.accent)                            .frame(maxWidth: .infinity, minHeight: 48, alignment: .leading)                            .contentShape(Rectangle())                    }                    .buttonStyle(ContactsPress())                    .accessibilityIdentifier("contacts-person-act-\(act.id)")                    ContactsRule()                }            }            .padding(.top, 24)        }    }    private func heading(_ key: String) -> some View {        VStack(spacing: 0) {            ContactsHeading(text: ContactsWords.line(key))            ContactsRule()        }    }    private func links(_ items: [ContactsLink]) -> some View {        ForEach(Array(items.enumerated()), id: \.offset) { at, one in            Button { Task { await model.take(one.call) } } label: {                if one.call == nil {                    ContactsRowView(title: one.words, detail: one.detail, last: at == items.count - 1) { EmptyView() }                } else {                    ContactsRowView(title: one.words, detail: one.detail, last: at == items.count - 1)                }            }            .buttonStyle(ContactsPress())        }    }    /// Tapping a way to reach dials it, or opens mail.    private func dial(_ one: ContactsPerson.Reach) {        let value = one.words.filter { !$0.isWhitespace }        let url: URL? = switch one.kind {        case "phone": URL(string: "tel:" + value.filter { $0.isNumber || $0 == "+" })        case "email": URL(string: "mailto:" + value)        default: nil        }        if let url { openURL(url) }    }    /// A row of Between you opens `cotton<app>://person/<person>`; nothing happens when that app is not here.    private func open(_ one: ContactsPerson.Between, person: String) {        let fields = (one.open ?? .null).contactsFields        let app = fields["app"]?.string ?? one.app        let who = fields["person"]?.string ?? person        guard let url = URL(string: "cotton\(app)://person/\(who.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? who)") else { return }        openURL(url)    }}#endif