// COTTON CONTACTS' STATE (assets/design/contacts/SCREENS.md "How every screen works"): the tab standing, each tab's // stack of pages, the toast, and the revision every screen reads again on. A call the core gives opens a page (a // person, a group, Same person?, a pair, a form, the picker), asks for a name first (rename, a new group), or is an act // the core does, after its `confirm` sentence when it has one, and answers with `said` or `error`. #if os(iOS) import CottonKit import Observation import SwiftUI enum ContactsTab: String, CaseIterable, Sendable { case contacts, groups, circle, you } /// A page over a tab's first screen. enum ContactsRoute: Hashable, Sendable { case person(String) case group(String) case same case pair(ContactsCall) case form(ContactsCall) /// The picker: a new group (its name) or people added to a group; `call` is what Done calls. case pick(call: ContactsCall, name: String?) /// Read your phone's contacts. case read } /// A name asked for before a call (a group renamed, a new group). struct ContactsNaming: Equatable, Sendable { var title: String var call: ContactsCall var makes: Bool } @MainActor @Observable final class ContactsModel { let core: any CottonCalling private(set) var tab: ContactsTab = .contacts /// The tabs opened so far: each keeps its stack once opened. private(set) var visited: Set = [.contacts] var paths: [ContactsTab: [ContactsRoute]] = [:] var toast: String? /// Moves on every change he makes and every sync that brings changes: every screen reads again. var revision = 0 /// An act waiting on his yes (its `confirm`). var confirming: ContactsAct? /// A name being asked for. var naming: ContactsNaming? private var toastTask: Task? init(core: any CottonCalling) { self.core = core } var calls: ContactsCalls { ContactsCalls(core: core) } /// The foot hides while a screen is open over a place. var footShown: Bool { (paths[tab] ?? []).isEmpty } func open(_ tab: ContactsTab) { visited.insert(tab) self.tab = tab } func push(_ route: ContactsRoute) { paths[tab, default: []].append(route) } func back() { if !(paths[tab] ?? []).isEmpty { paths[tab]?.removeLast() } } func changed() { revision += 1 } func say(_ words: String) { toastTask?.cancel() withAnimation(.easeOut(duration: 0.2)) { toast = words } toastTask = Task { [weak self] in try? await Task.sleep(for: .seconds(3)) guard !Task.isCancelled else { return } withAnimation(.easeOut(duration: 0.2)) { self?.toast = nil } } } /// The page a call opens, when it is one. static func route(_ call: ContactsCall) -> ContactsRoute? { let id = (call.request ?? .object([:])).contactsFields["id"]?.string switch call.method { case "contacts.person": return id.map { .person($0) } case "contacts.group": return id.map { .group($0) } case "contacts.same.list": return .same case "contacts.same.pair": return .pair(call) case "contacts.add.form", "contacts.edit.form": return .form(call) case "contacts.group.add": return .pick(call: call, name: nil) default: return nil } } /// An act tapped: asked first when it carries `confirm`, else taken. func tap(_ act: ContactsAct) { if act.confirm != nil { confirming = act } else { Task { await take(act.call) } } } /// A call tapped: its page opens, a name is asked for, or the core does the act and says how it went. func take(_ call: ContactsCall?, adding extra: [String: JSONValue] = [:]) async { guard let call else { return } if let route = Self.route(call) { return push(route) } switch call.method { case "contacts.home": return case "contacts.group.rename" where extra["name"] == nil: naming = ContactsNaming(title: ContactsWords.line("groups.rename"), call: call, makes: false) return case "contacts.group.make" where extra["people"] == nil: naming = ContactsNaming(title: ContactsWords.line("groups.name"), call: call, makes: true) return default: break } let onPair: Bool = if case .pair? = paths[tab]?.last { true } else { false } switch await calls.act(call, adding: extra) { case .read(let said): UINotificationFeedbackGenerator().notificationOccurred(.success) if let words = said.said { say(words) } if ["contacts.archive", "contacts.group.delete"].contains(call.method) || (call.method == "contacts.same" && onPair) { back() } changed() case .refused(let line): UINotificationFeedbackGenerator().notificationOccurred(.warning) say(line) } } /// The name given: a rename is called with it; a new group opens the picker. func named(_ name: String) { guard let naming else { return } self.naming = nil let typed = name.trimmingCharacters(in: .whitespaces) guard !typed.isEmpty else { return } if naming.makes { push(.pick(call: naming.call, name: typed)) } else { Task { await take(naming.call, adding: ["name": .string(typed)]) } } } } #endif