// How to reach someone — and it is always somebody else's app that does it.//// Cotton sends nothing. "Call" hands the number to the Phone app (`tel:`),// "Message" hands it to Messages (`sms:`) or, when the conversation Cotton// holds with them came over WhatsApp, to WhatsApp — its URL scheme first and// Messages behind it when WhatsApp is unavailable. Contact actions always// hand off to a phone app; they never open a web page.//// Every URL a page opens is built HERE, as pure functions, so the whole table// is tested rather than spread across buttons. A column marked `reach` draws// its values through `ReachLines`: a line each that opens the sheet of what// can be done with it (`ContactSheetContent`), a long press copying it, and —// under the definition's first phone column — the quiet "Call" and "Message".// The call log rings back through `ContactHandoff.callBack`. Moved here from// the old person page, which drew them first.importCottonValuesimportSwiftUI/// What can be done with a contact point.enumContactAction: Equatable, Sendable {case call, message, whatsApp, whatsAppCall, copy, mail, instagram, faceTime}/// A link to open, and the one to try when it will not.structHandoff: Equatable, Sendable {let url: URLlet fallback: URL?}enumContactHandoff {/// The fewest digits that can plausibly address a real subscriber.staticlet minAddressableDigits = 10/// The number as it is dialled: digits only, with the leading `+` kept/// when the number was written with one — `tel:+919876543210` reaches the/// same phone from any country, `tel:919876543210` does not.staticfunc dialable(_ phone: String) -> String {let digits = phone.filter(\.isNumber)let international = phone.trimmingCharacters(in: .whitespaces).hasPrefix("+")return international && !digits.isEmpty ? "+\(digits)" : digits }/// Digits only — what WhatsApp's links take, which have no `+`.staticfunc digits(_ phone: String) -> String { phone.filter(\.isNumber) }/// An Instagram handle as its links take it: no "@", no profile URL/// round it, only the characters a handle can hold.staticfunc instagramHandle(_ value: String) -> String {var handle = value.trimmingCharacters(in: .whitespacesAndNewlines)for prefix in ["https://www.instagram.com/", "https://instagram.com/", "instagram.com/", "@"] where handle.lowercased().hasPrefix(prefix) { handle = String(handle.dropFirst(prefix.count)) } handle = handle.trimmingCharacters(in: CharacterSet(charactersIn: "/"))return handle.allSatisfy({ $0.isLetter || $0.isNumber || $0 == "." || $0 == "_" }) ? handle : "" }/// WhatsApp is offered only for a number long enough to address: a short/// entry (an extension, a half-typed number) would open a dead chat.staticfunc canWhatsApp(_ phone: String) -> Bool { digits(phone).count >= minAddressableDigits }/// Which app "Message" goes to. WhatsApp when Cotton's conversation with/// them is a WhatsApp one AND the number can address a chat; Messages/// otherwise — Messages takes iMessage and SMS alike.staticfunc messagesInWhatsApp(channel: String?, phone: String) -> Bool { channel == "whatsapp" && canWhatsApp(phone) }/// The link for one action on one contact point. `nil` when the point/// cannot take the action (an email cannot be called, a number with no/// digits cannot be dialled, a short number has no WhatsApp) — the button/// is then absent, never offered and dead. `.copy` is not a link.staticfunc link(_ action: ContactAction, _ target: ContactTarget) -> Handoff? {switch (target.kind, action) {case (_, .copy):returnnilcase (.email, .mail):let address = target.value.trimmingCharacters(in: .whitespaces)guard address.contains("@"), let url = URL(string: "mailto:\(address)") else { returnnil }returnHandoff(url: url, fallback: nil)case (.instagram, .instagram):// The app's own link first; the profile on the web behind it.let handle = instagramHandle(target.value)guard !handle.isEmpty, let url = URL(string: "instagram://user?username=\(handle)"),let web = URL(string: "https://www.instagram.com/\(handle)/")else { returnnil }returnHandoff(url: url, fallback: web)case (.phone, .faceTime), (.email, .faceTime):// FaceTime reaches a number or an Apple ID's address alike.let point = target.kind == .phone ? dialable(target.value) : target.value.trimmingCharacters(in: .whitespaces)guard !point.isEmpty, let url = URL(string: "facetime:\(point)") else { returnnil }returnHandoff(url: url, fallback: nil)case (.instagram, _), (_, .instagram), (.email, _), (.phone, .mail):returnnilcase (.phone, .call):let number = dialable(target.value)guard !digits(number).isEmpty, let url = URL(string: "tel:\(number)") else { returnnil }returnHandoff(url: url, fallback: nil)case (.phone, .message):let number = dialable(target.value)guard !digits(number).isEmpty, let url = URL(string: "sms:\(number)") else { returnnil }returnHandoff(url: url, fallback: nil)case (.phone, .whatsApp), (.phone, .whatsAppCall):// WhatsApp publishes no link that starts a CALL; the chat is as// close as a link can land, with the call button one tap away.guard canWhatsApp(target.value) else { returnnil }let number = digits(target.value)guardlet url = URL(string: "whatsapp://send?phone=\(number)"),let messages = URL(string: "sms:\(dialable(target.value))")else { returnnil }returnHandoff(url: url, fallback: messages) } }/// The page's quiet "Message" action for a person: their first number,/// into WhatsApp or Messages by the conversation's channel.staticfunc message(phones: [String], channel: String?) -> Handoff? {guardlet phone = phones.first(where: { !digits($0).isEmpty }) else { returnnil }let target = ContactTarget(value: phone, kind: .phone)return link(messagesInWhatsApp(channel: channel, phone: phone) ? .whatsApp : .message, target) }/// The page's quiet "Call" action: their first dialable number.staticfunc call(phones: [String]) -> Handoff? {guardlet phone = phones.first(where: { !digits($0).isEmpty }) else { returnnil }return link(.call, ContactTarget(value: phone, kind: .phone)) }/// "+91 98765 43210" — grouped for READING. The number that is copied and/// dialled is still the raw one; only the reading changes, and only for the/// one shape this app actually shows. A wrong grouping reads worse than/// none, so everything else passes through untouched.staticfunc forReading(_ phone: String) -> String {let compact = phone.filter { !$0.isWhitespace && $0 != "-" }guard compact.hasPrefix("+91") else { return phone }let rest = String(compact.dropFirst(3))guard rest.count == 10, rest.allSatisfy(\.isNumber) else { return phone }return"+91 \(rest.prefix(5)) \(rest.dropFirst(5))" }/// An email address laid out for READING on the narrow contact card: a/// zero-width break before the "@", so an address too long for one line/// breaks into its name and its domain rather than mid-word/// ("karan.velloran@examp / le.com"). The address that is copied and mailed/// is still the raw one.staticfunc forReading(email: String) -> String {guardlet at = email.firstIndex(of: "@"), at != email.startIndex else { return email }returnString(email[..<at]) + "\u{200B}" + String(email[at...]) }/// Opens a link, with the universal-link fallback behind it where there/// is one. Only the LAST candidate failing is `failed`: `whatsapp://`/// not opening is expected on a phone without WhatsApp. @MainActor staticfuncopen(_ handoff: Handoff, with openURL: OpenURLAction, failed: @escaping () -> Void) { openURL(handoff.url) { opened inguard !opened else { return }guardlet fallback = handoff.fallback else { return failed() } openURL(fallback) { landed inif !landed { failed() } } } }/// What a contact point's sheet offers, in order.staticfunc actions(for target: ContactTarget) -> [(action: ContactAction, symbol: String, label: String)] {if target.kind == .email {return [(.mail, "envelope", ShellLines.line("faces", "contact.write_email")), (.copy, "doc.on.doc", ShellLines.line("faces", "contact.copy_address"))] }if target.kind == .instagram {return [(.instagram, "camera", ShellLines.line("faces", "contact.open_instagram")), (.copy, "doc.on.doc", ShellLines.line("faces", "contact.copy_handle"))] }var out: [(ContactAction, String, String)] = [ (.call, "phone", ShellLines.line("faces", "contact.call")), (.message, "message", ShellLines.line("faces", "contact.message")), ]if canWhatsApp(target.value) { out.append((.whatsApp, "bubble.left", ShellLines.line("faces", "contact.whatsapp"))) out.append((.whatsAppCall, "phone.bubble", ShellLines.line("faces", "contact.whatsapp_call"))) } out.append((.copy, "doc.on.doc", ShellLines.line("faces", "contact.copy_number")))return out }}extensionContactTarget.Kind {/// The kind a `reach` column's values are.init(_ reach: ReachKind) {switch reach {case .phone: self = .phonecase .email: self = .emailcase .instagram: self = .instagram } }}