Cotton code
111 lines · 5.2 KB · SwiftRaw
// CONTACTS' HOME (PEOPLE-DESIGN.md f.1; `contacts.home {kind?, query?}`): the big title and Add, the search, the// kinds (All, People, Organisations, Assistants), "look like the same person twice", Read your phone's contacts, and the// tiers (Close, Often, Now and then, Everyone else) as the core sorts them; a search shows one section, Found.#if os(iOS)import CottonKitimport SwiftUIstruct ContactsHomeScreen: View {    let model: ContactsModel    @State private var read: ContactsRead<ContactsHome>?    @State private var kind = "all"    @State private var query = ""    var body: some View {        ContactsPage(tag: "contacts-home") {            HStack(alignment: .firstTextBaseline) {                ContactsTitle(text: read?.answer?.title ?? ContactsWords.line("tabs.contacts"))                Spacer(minLength: 8)                if let add = read?.answer?.add {                    Button { Task { await model.take(add.call) } } label: {                        Image(systemName: "plus").font(.system(size: 22, weight: .semibold)).foregroundStyle(ContactsInk.accent)                            .frame(width: 48, height: 48).contentShape(Rectangle())                    }                    .buttonStyle(ContactsPress())                    .accessibilityLabel(Text(verbatim: add.words))                    .accessibilityIdentifier("contacts-add")                }            }            switch read {            case .read(let home)?: page(home)            case .refused(let line)?: ContactsRefused(line: line) { model.changed() }            case nil: ProgressView().tint(ContactsInk.accent).frame(maxWidth: .infinity).padding(.top, 40)            }        }        .task(id: "\(kind)|\(query)|\(model.revision)") {            if read != nil, !query.isEmpty { try? await Task.sleep(for: .milliseconds(250)) }            guard !Task.isCancelled else { return }            let answer = await model.calls.home(kind: kind, query: query)            guard !Task.isCancelled else { return }            read = answer        }    }    @ViewBuilder private func page(_ home: ContactsHome) -> some View {        if let search = home.search {            ContactsSearch(prompt: search.placeholder, text: $query).padding(.top, 10)        }        if !home.kinds.isEmpty {            ScrollView(.horizontal, showsIndicators: false) {                HStack(spacing: 8) {                    ForEach(home.kinds) { one in                        ContactsChip(label: one.words, on: one.on, id: "contacts-kind-\(one.id)") { kind = one.id }                    }                }            }            .padding(.top, 12)        }        if let sorting = home.sorting {            Text(verbatim: sorting).font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet)                .frame(maxWidth: .infinity, alignment: .leading)                .padding(.top, 12)                .accessibilityIdentifier("contacts-sorting")        }        if let same = home.same {            Button { Task { await model.take(same.call) } } label: {                HStack(spacing: 10) {                    Text(verbatim: same.words).font(.system(.subheadline)).foregroundStyle(ContactsInk.ink).multilineTextAlignment(.leading)                    Spacer(minLength: 8)                    if let act = same.act { Text(verbatim: "\(act) ›").font(.system(.subheadline).weight(.semibold)).foregroundStyle(ContactsInk.accent) }                }                .padding(.vertical, 12)                .frame(minHeight: 44)                .contentShape(Rectangle())            }            .buttonStyle(ContactsPress())            .padding(.top, 10)            .accessibilityIdentifier("contacts-same-banner")            ContactsRule()        }        if let empty = home.empty, home.sections.isEmpty {            VStack(alignment: .leading, spacing: 6) {                Text(verbatim: empty.title).font(.system(.title3).weight(.semibold)).foregroundStyle(ContactsInk.ink)                if let message = empty.message { Text(verbatim: message).font(.system(.subheadline)).foregroundStyle(ContactsInk.quiet).fixedSize(horizontal: false, vertical: true) }            }            .padding(.top, 24)            .accessibilityIdentifier("contacts-empty")        }        if let book = home.import {            VStack(alignment: .leading, spacing: 8) {                ContactsButton(label: book.words, filled: true, id: "contacts-import") { model.push(.read) }                if let hint = book.hint { ContactsNote(text: hint) }            }            .padding(.top, 14)        }        ForEach(home.sections) { section in            ContactsHeading(text: section.title, count: section.count)                .accessibilityIdentifier("contacts-section-\(section.id)")            LazyVStack(spacing: 0) {                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, initials: row.initials ?? "#", last: at == section.rows.count - 1)                    }                    .buttonStyle(ContactsPress())                    .accessibilityIdentifier("contacts-row-\(row.id)")                }            }        }    }}#endif