import SwiftUI #if os(iOS) /// The iPhone app: pairing until there is a team, then Conversation, Needs /// you and Tasks as tabs, with the task view pushed from any of them. public struct PhoneRoot: View { @State private var model: EquipoAppModel @Environment(\.scenePhase) private var phase public init(model: EquipoAppModel = EquipoAppModel()) { _model = State(initialValue: model) } public var body: some View { Group { #if DEBUG if ProcessInfo.processInfo.environment["EQUIPO_ATTACHMENTS_FIXTURE"] != nil { AttachmentsFixture() } else { signedInContent } #else signedInContent #endif } .tint(Palette.accent) } @ViewBuilder private var signedInContent: some View { Group { if let session = model.session, session.status != .signedOut { PhoneSignedIn(model: model, session: session) } else { EquipoSignInView(model: model) } } .tint(Palette.accent) .onAppear { KeyboardFrame.start() } .modifier(NotificationActionPresenter(model: model)) .onChange(of: model.session?.account.address) { model.notificationBindingChanged() } .onChange(of: phase) { _, now in now == .active ? model.foreground() : model.background() } .onChange(of: model.session?.status) { _, status in if status == .signedOut { model.sessionEnded() } } .onChange(of: model.session?.needsYouCount) { _, count in model.needsYouChanged(count) } .onChange(of: model.session?.tasks) { _, tasks in // A task that ended while its end push was lost leaves the Lock Screen. guard let tasks else { return } let live = model.liveActivities Task { await live.endFinished(tasks) } } // A Live Activity's tap: its task. .onOpenURL { url in if let route = PushRoute(url: url) { model.open(route) } } .task { model.foreground(); await model.runRenewals() } } } /// The signed-in phone: the tabs, with Oficina ID's notice above them. struct PhoneSignedIn: View { let model: EquipoAppModel let session: EquipoSession var body: some View { PhoneTabs(model: model, session: session) .id(ObjectIdentifier(session)) .safeAreaInset(edge: .top, spacing: 0) { if session.account.oficinaIdentity != nil { EquipoOficinaNotice(model: model) } } } } struct PhoneTabs: View { let model: EquipoAppModel let session: EquipoSession @State private var tab: Destination.Pane = .conversation @State private var conversationPath: [String] = [] @State private var needsPath: [String] = [] @State private var tasksPath: [String] = [] @State private var projectsPath: [ProjectRoute] = [] private enum ProjectRoute: Hashable { case project(String), task(String) } @State private var focusCard: String? @State private var showingAccount = false var body: some View { TabView(selection: $tab) { Tab(String(localized: "tab.conversation", bundle: .module), systemImage: "bubble.left.and.bubble.right", value: .conversation) { stack($conversationPath, title: String(localized: "tab.conversation", bundle: .module)) { ConversationScreen(session: session, focus: $focusCard) { conversationPath.append($0) } } } Tab(String(localized: "tab.needs", bundle: .module), systemImage: "tray", value: .needs) { stack($needsPath, title: String(localized: "tab.needs", bundle: .module)) { NeedsScreen(session: session) { needsPath.append($0) } } } .badge(session.needsYouCount ?? 0) Tab(String(localized: "nav.projects", bundle: .module), systemImage: "folder", value: .projects) { NavigationStack(path: $projectsPath) { ProjectsScreen(session: session) { projectsPath.append(.project($0)) } .navigationTitle(String(localized: "nav.projects", bundle: .module)) .navigationBarTitleDisplayMode(.inline) .navigationDestination(for: ProjectRoute.self) { route in switch route { case .project(let id): ProjectDetailScreen(session: session, projectID: id) { project in session.projectCatalog.selection = project conversationPath = []; tab = .conversation } openTask: { projectsPath.append(.task($0)) } case .task(let id): TaskScreen(session: session, taskID: id) } } } } Tab(String(localized: "tab.tasks", bundle: .module), systemImage: "checklist", value: .tasks) { stack($tasksPath, title: String(localized: "tab.tasks", bundle: .module)) { TasksScreen(session: session) { tasksPath.append($0) } } } } .modifier(SettingsSheetPresenter(session: session)) // Another tab, or a task opened from any of them: the keyboard goes. .onChange(of: tab) { KeyboardFrame.dismiss() } .onChange(of: conversationPath) { KeyboardFrame.dismiss() } .onChange(of: needsPath) { KeyboardFrame.dismiss() } .onChange(of: tasksPath) { KeyboardFrame.dismiss() } .onChange(of: projectsPath) { KeyboardFrame.dismiss() } .sheet(isPresented: $showingAccount) { NavigationStack { AccountScreen(model: model, session: session).navigationTitle(session.teamName) } } .onChange(of: model.destination, initial: true) { _, destination in guard let destination else { return } go(destination) model.destination = nil } } /// A tapped notification or Live Activity: its tab, at its card or task. private func go(_ destination: Destination) { showingAccount = false tab = destination.pane switch destination.pane { case .conversation: conversationPath = []; focusCard = destination.cardID case .needs: needsPath = [] case .projects: projectsPath = [] case .tasks: tasksPath = destination.taskID.map { [$0] } ?? [] } } private func stack(_ path: Binding<[String]>, title: String, @ViewBuilder content: () -> C) -> some View { NavigationStack(path: path) { content() .navigationTitle(title).navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .topBarLeading) { Button { showingAccount = true } label: { Image(systemName: "person.crop.circle") } .accessibilityLabel(Text(String(localized: "account.title", bundle: .module))) } } .navigationDestination(for: String.self) { id in TaskScreen(session: session, taskID: id).navigationTitle(String(localized: "task.title", bundle: .module)).navigationBarTitleDisplayMode(.inline) } } } } #endif