// CONTACTS' OTHER PLACES (assets/design/contacts/SCREENS.md): Same person? (`contacts.same.list`, or one pair: // `contacts.same.pair`), Groups and one group (`contacts.groups`, `contacts.group`), the picker (a new group, or people // added), the form (`contacts.add.form` / `contacts.edit.form`), Read your phone's contacts, Circle and You. #if os(iOS) import CottonKit import SwiftUI import UIKit // MARK: - Same person? struct ContactsSameScreen: View { let model: ContactsModel /// One pair (`contacts.same.pair`), or nil for the list. let pair: ContactsCall? @State private var list: ContactsRead? @State private var one: ContactsRead? var body: some View { ContactsPage(tag: "contacts-same") { if pair != nil { switch one { case .read(let found)?: card(found) case .refused(let line)?: ContactsRefused(line: line) { model.changed() } case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40) } } else { switch list { case .read(let same)?: ContactsTitle(text: (same.count ?? 0) > 1 ? "\(same.title) ยท \((same.count ?? 0).formatted())" : same.title) if let first = same.pairs.first { card(first) } else { ContactsNote(text: same.empty?.message ?? ContactsWords.line("same.none")).padding(.top, 16) } case .refused(let line)?: ContactsRefused(line: line) { model.changed() } case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40) } } } .task(id: model.revision) { if let pair { one = await model.calls.ask(pair) } else { list = await model.calls.same() } } } private func card(_ pair: ContactsSameList.Pair) -> some View { VStack(alignment: .leading, spacing: 14) { HStack(alignment: .top, spacing: 14) { side(pair.a); side(pair.b) } if let why = pair.why { ContactsNote(text: ContactsWords.line("same.why", ["why": why])) } if let first = pair.acts.first { ContactsButton(label: first.words, id: "contacts-same-\(first.id)") { model.tap(first) } } HStack(spacing: 8) { ForEach(pair.acts.dropFirst()) { act in ContactsButton(label: act.words, filled: false, id: "contacts-same-\(act.id)") { model.tap(act) } } } } .padding(.top, 16) } private func side(_ one: ContactsSameList.Side) -> some View { VStack(alignment: .leading, spacing: 4) { ContactsRing(initials: one.initials ?? ContactsStandIn.initials(one.words), size: 48) Text(verbatim: one.words).font(.system(.callout).weight(.semibold)).foregroundStyle(ContactsInk.ink) if let detail = one.detail { Text(verbatim: detail).font(ContactsType.small).foregroundStyle(ContactsInk.quiet) } ForEach(one.reach ?? [], id: \.self) { Text(verbatim: $0).font(ContactsType.small).foregroundStyle(ContactsInk.ink) } } .frame(maxWidth: .infinity, alignment: .leading) } } // MARK: - Groups struct ContactsGroupsScreen: View { let model: ContactsModel @State private var read: ContactsRead? var body: some View { ContactsPage(tag: "contacts-groups") { HStack(alignment: .firstTextBaseline) { ContactsTitle(text: read?.answer?.title ?? ContactsWords.line("tabs.groups")) Spacer(minLength: 8) if let add = read?.answer?.add { Button(add.words) { Task { await model.take(add.call) } } .font(.system(.callout).weight(.semibold)).foregroundStyle(ContactsInk.accent) .frame(minHeight: 44) .accessibilityIdentifier("contacts-group-new") } } switch read { case .read(let groups)?: ForEach(groups.sections) { section in ContactsHeading(text: section.title) ForEach(Array(section.rows.enumerated()), id: \.element.id) { at, row in Button { Task { await model.take(row.call) } } label: { ContactsRowView(title: row.words, detail: row.detail, last: at == section.rows.count - 1) } .buttonStyle(ContactsPress()) .accessibilityIdentifier("contacts-row-\(row.id)") } } if let foot = groups.foot { Text(verbatim: foot).font(ContactsType.small).foregroundStyle(ContactsInk.faint).padding(.top, 16) .fixedSize(horizontal: false, vertical: true) } case .refused(let line)?: ContactsRefused(line: line) { model.changed() } case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40) } } .task(id: model.revision) { read = await model.calls.groups() } } } struct ContactsGroupScreen: View { let model: ContactsModel let id: String @State private var read: ContactsRead? var body: some View { ContactsPage(tag: "contacts-group") { switch read { case .read(let group)?: ContactsTitle(text: group.title) if let detail = group.detail { Text(verbatim: detail).font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet).padding(.top, 4) } LazyVStack(spacing: 0) { ForEach(Array(group.rows.enumerated()), id: \.element.id) { at, row in Button { Task { await model.take(row.call) } } label: { ContactsRowView(title: row.words, detail: row.detail, initials: row.initials ?? "#", last: at == group.rows.count - 1) { if let acts = row.acts, !acts.isEmpty { HStack(spacing: 10) { ForEach(acts) { act in Button(act.words) { model.tap(act) }.font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet).frame(minHeight: 44) } } } else { ContactsChevron() } } } .buttonStyle(ContactsPress()) .accessibilityIdentifier("contacts-row-\(row.id)") } } .padding(.top, 14) VStack(spacing: 0) { ForEach(group.acts ?? []) { 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-group-act-\(act.id)") ContactsRule() } } .padding(.top, 24) case .refused(let line)?: ContactsRefused(line: line) { model.changed() } case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40) } } .task(id: model.revision) { read = await model.calls.group(id) } } } // MARK: - The picker /// Everyone from `contacts.home {kind: all}`, each with a tick; Done calls `contacts.group.make {name, people}` (then /// opens the new group) or `contacts.group.add {id, people}`. struct ContactsPickScreen: View { let model: ContactsModel let call: ContactsCall let name: String? @State private var everyone: [ContactsRow] = [] @State private var chosen: Set = [] @State private var query = "" @State private var busy = false var body: some View { ContactsPage(tag: "contacts-pick") { ContactsTitle(text: name ?? ContactsWords.line("groups.add_people")) ContactsSearch(prompt: ContactsWords.line("home.search_people"), text: $query).padding(.top, 10) let shown = query.isEmpty ? everyone : everyone.filter { $0.words.localizedCaseInsensitiveContains(query) } LazyVStack(spacing: 0) { ForEach(Array(shown.enumerated()), id: \.element.id) { at, row in Button { if chosen.contains(row.id) { chosen.remove(row.id) } else { chosen.insert(row.id) } } label: { ContactsRowView(title: row.words, detail: row.detail, initials: row.initials ?? "#", last: at == shown.count - 1) { Image(systemName: chosen.contains(row.id) ? "checkmark.square.fill" : "square") .font(.system(size: 22)).foregroundStyle(chosen.contains(row.id) ? ContactsInk.accent : ContactsInk.faint) } } .buttonStyle(ContactsPress()) .accessibilityAddTraits(chosen.contains(row.id) ? .isSelected : []) } } .padding(.top, 10) } .toolbar { ToolbarItem(placement: .topBarTrailing) { Button(ContactsWords.line("ask.done")) { done() } .disabled(busy) .foregroundStyle(ContactsInk.accent) .accessibilityIdentifier("contacts-pick-done") } } .task { let home = await model.calls.home(kind: "all", query: nil) everyone = (home.answer?.sections ?? []).flatMap(\.rows) } } private func done() { var extra: [String: JSONValue] = ["people": .array(everyone.map(\.id).filter(chosen.contains).map(JSONValue.string))] if let name { extra["name"] = .string(name) } busy = true Task { switch await model.calls.act(call, adding: extra) { case .read(let said): if let words = said.said { model.say(words) } model.back() if call.method == "contacts.group.make", let id = said.id { model.push(.group(id)) } model.changed() case .refused(let line): model.say(line) } busy = false } } } // MARK: - The form struct ContactsFormScreen: View { let model: ContactsModel let call: ContactsCall @State private var read: ContactsRead? @State private var kind = "person" @State private var values: [String: String] = [:] @State private var lists: [String: [String]] = [:] @State private var saving = false @State private var error: String? var body: some View { ContactsPage(tag: "contacts-form") { switch read { case .read(let form)?: page(form) case .refused(let line)?: ContactsRefused(line: line) { model.changed() } case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40) } } .toolbar { if let form = read?.answer { ToolbarItem(placement: .topBarTrailing) { Button(form.save.words) { save(form) } .disabled(saving) .foregroundStyle(ContactsInk.accent) .accessibilityIdentifier("contacts-form-save") } } } .task(id: call) { let answer = await model.calls.form(call) if let form = answer.answer { kind = form.kinds?.first(where: \.on)?.id ?? "person" for field in form.fields { if let many = field.values { lists[field.id] = many.isEmpty ? [""] : many } else { values[field.id] = field.value ?? "" } } } read = answer } } @ViewBuilder private func page(_ form: ContactsForm) -> some View { ContactsTitle(text: form.title) if let kinds = form.kinds, kinds.count > 1 { HStack(spacing: 8) { ForEach(kinds) { one in ContactsChip(label: one.words, on: kind == one.id, id: "contacts-kind-\(one.id)") { kind = one.id } } } .padding(.top, 12) } ForEach(form.fields) { field in Text(verbatim: field.words).font(ContactsType.small).foregroundStyle(ContactsInk.quiet).padding(.top, 18) if field.values != nil { ForEach(Array((lists[field.id] ?? [""]).enumerated()), id: \.offset) { at, _ in box(Binding(get: { lists[field.id].flatMap { at < $0.count ? $0[at] : nil } ?? "" }, set: { lists[field.id]?[at] = $0 }), field: field.id, tag: "contacts-form-\(field.id)-\(at)") } if let add = field.add { Button("+ \(add)") { lists[field.id, default: []].append("") } .font(ContactsType.small.weight(.semibold)).foregroundStyle(ContactsInk.accent).frame(minHeight: 44) } } else { box(Binding(get: { values[field.id] ?? "" }, set: { values[field.id] = $0 }), field: field.id, tag: "contacts-form-\(field.id)", long: field.id == "notes") } } if let error { Text(verbatim: error).font(.system(.subheadline)).foregroundStyle(ContactsInk.ink).padding(.top, 16) .fixedSize(horizontal: false, vertical: true) } } private func box(_ text: Binding, field: String, tag: String, long: Bool = false) -> some View { TextField("", text: text, axis: long ? .vertical : .horizontal) .font(ContactsType.body) .foregroundStyle(ContactsInk.ink) .keyboardType(field == "phones" ? .phonePad : field == "emails" ? .emailAddress : .default) .textInputAutocapitalization(field == "emails" ? .never : .words) .autocorrectionDisabled(field != "notes") .lineLimit(long ? 3...8 : 1...1) .padding(.horizontal, 12) .padding(.vertical, 10) .frame(minHeight: 44) .overlay(RoundedRectangle(cornerRadius: 10, style: .continuous).strokeBorder(ContactsInk.rule, lineWidth: 1)) .padding(.top, 6) .accessibilityIdentifier(tag) } private func save(_ form: ContactsForm) { var extra: [String: JSONValue] = ["kind": .string(kind)] for (key, value) in values { extra[key] = .string(value.trimmingCharacters(in: .whitespacesAndNewlines)) } for (key, many) in lists { extra[key] = .array(many.map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }.map(JSONValue.string)) } saving = true error = nil Task { let save = form.save.call ?? ContactsCall(method: "contacts.add") switch await model.calls.act(save, adding: extra) { case .read(let said): if let words = said.said { model.say(words) } model.back() if save.method == "contacts.add", let id = said.id { model.push(.person(id)) } model.changed() case .refused(let line): error = line } saving = false } } } // MARK: - Read your phone's contacts struct ContactsReadScreen: View { let model: ContactsModel private enum Step: Equatable { case checking, ask, off, reading, said(String) } @State private var step: Step = .checking @Environment(\.openURL) private var openURL var body: some View { ContactsPage(tag: "contacts-read") { switch step { case .checking, .reading: VStack(alignment: .leading, spacing: 12) { ProgressView().tint(ContactsInk.accent) if step == .reading { ContactsNote(text: ContactsWords.line("home.loading")) } } .padding(.top, 30) case .ask: say(title: "import.title", line: "import.line") ContactsButton(label: ContactsWords.line("import.allow"), id: "contacts-import-allow") { Task { await ask() } }.padding(.top, 20) case .off: say(title: "import.off_title", line: "import.off_line") ContactsButton(label: ContactsWords.line("import.settings"), id: "contacts-import-settings") { if let url = URL(string: UIApplication.openSettingsURLString) { openURL(url) } } .padding(.top, 20) case .said(let words): Text(verbatim: words).font(.system(.body)).foregroundStyle(ContactsInk.ink).padding(.top, 20) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("contacts-import-said") ContactsButton(label: ContactsWords.line("import.close"), id: "contacts-import-close") { model.back() model.changed() } .padding(.top, 20) } } .task { switch await ContactBook.current() { case .loading: await readAll() case .ask: step = .ask default: step = .off } } } @ViewBuilder private func say(title: String, line: String) -> some View { Text(verbatim: ContactsWords.line(title)).font(.system(size: 22, weight: .semibold)).foregroundStyle(ContactsInk.ink).padding(.top, 10) .fixedSize(horizontal: false, vertical: true) ContactsNote(text: ContactsWords.line(line)).padding(.top, 8) } private func ask() async { if await ContactBook.request() == .loading { await readAll() } else { step = .off } } private func readAll() async { step = .reading do { let cards = try await ContactBook.read() switch await model.calls.read(cards) { case .read(let said): step = .said(said) case .refused(let line): step = .said(line) } } catch { step = .said(ContactsWords.line("import.unreadable")) } } } // MARK: - Circle struct ContactsCircleScreen: View { let model: ContactsModel @State private var read: ContactsRead? var body: some View { ContactsPage(tag: "contacts-circle") { ContactsTitle(text: read?.answer?.title ?? ContactsWords.line("tabs.circle")) switch read { case .read(let circle)?: if let empty = circle.empty { VStack(alignment: .leading, spacing: 6) { Text(verbatim: empty.title).font(.system(size: 18, weight: .semibold)).foregroundStyle(ContactsInk.ink) if let message = empty.message { Text(verbatim: message).font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet) } } .padding(.top, 20) } case .refused(let line)?: ContactsRefused(line: line) { model.changed() } case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40) } } .task(id: model.revision) { read = await model.calls.circle() } } } // MARK: - You @MainActor extension SettingsLook { /// The shared settings parts in Contacts' colours. static var contacts: SettingsLook { SettingsLook( ink: ContactsInk.ink, quiet: ContactsInk.quiet, rule: ContactsInk.rule, ground: ContactsInk.ground, body: ContactsType.body, small: ContactsType.small, heading: { AnyView(ContactsHeading(text: $0)) }, act: { words, id, enabled, tap in AnyView(ContactsButton(label: words, filled: false, id: id, action: tap).disabled(!enabled).opacity(enabled ? 1 : 0.45)) }, choice: { words, on, id, enabled, tap in AnyView(ContactsChip(label: words, on: on, id: id, action: tap).disabled(!enabled)) }, danger: ContactsInk.red, title: ContactsType.title ) } } /// YOU: the title and Contacts' line, then the card (Sign in, or the account) and Sync, Privacy, Storage, Help and /// About as every Cotton app has them (`shell.settings`). struct ContactsYouScreen: View { let model: ContactsModel @State private var home = SettingsHome() @State private var reads = ReadInTurn() @Environment(\.cottonSync) private var sync var body: some View { ContactsPage(tag: "contacts-you") { if home.place != nil { Button { home.back {} } label: { HStack(spacing: 4) { Image(systemName: "chevron.left") Text(verbatim: ContactsWords.line("ask.back")) } .font(ContactsType.body).foregroundStyle(ContactsInk.accent).frame(minHeight: 44) } .buttonStyle(ContactsPress()) ContactsTitle(text: home.title(own: { _ in nil }) ?? "") } else { ContactsTitle(text: ContactsWords.line("you.title")) ContactsNote(text: ContactsWords.line("you.line")).padding(.top, 6) } page.padding(.top, 10) } .task(id: "\(sync?.revision ?? 0)/\(model.revision)") { reads.ask { await home.read(model.core) } } } @ViewBuilder private var page: some View { switch home.place { case nil: SettingsHomeList(home: home, look: .contacts, own: []) { SettingsYouCard(home: home, core: model.core, look: .contacts) { model.say($0) } } case .profile?: SettingsProfileHead(home: home, look: .contacts) SettingsProfileFoot(home: home, core: model.core, look: .contacts, said: { model.say($0) }) { home.place = nil } case .own?: EmptyView() case let shared?: SettingsSharedPage(place: shared, home: home, core: model.core, look: .contacts, said: { model.say($0) }) { Task { await home.read(model.core) } } } } } #endif