| 1 | import SwiftUI |
| 2 | |
| 3 | #if os(iOS) |
| 4 | /// The iPhone app: pairing until there is a team, then Conversation, Needs |
| 5 | /// you and Tasks as tabs, with the task view pushed from any of them. |
| 6 | public struct PhoneRoot: View { |
| 7 | @State private var model: EquipoAppModel |
| 8 | @Environment(\.scenePhase) private var phase |
| 9 | public init(model: EquipoAppModel = EquipoAppModel()) { _model = State(initialValue: model) } |
| 10 | |
| 11 | public var body: some View { |
| 12 | Group { |
| 13 | #if DEBUG |
| 14 | if ProcessInfo.processInfo.environment["EQUIPO_ATTACHMENTS_FIXTURE"] != nil { AttachmentsFixture() } |
| 15 | else { signedInContent } |
| 16 | #else |
| 17 | signedInContent |
| 18 | #endif |
| 19 | } |
| 20 | .tint(Palette.accent) |
| 21 | } |
| 22 | |
| 23 | @ViewBuilder private var signedInContent: some View { |
| 24 | Group { |
| 25 | if let session = model.session, session.status != .signedOut { |
| 26 | PhoneSignedIn(model: model, session: session) |
| 27 | } else { |
| 28 | EquipoSignInView(model: model) |
| 29 | } |
| 30 | } |
| 31 | .tint(Palette.accent) |
| 32 | .onAppear { KeyboardFrame.start() } |
| 33 | .modifier(NotificationActionPresenter(model: model)) |
| 34 | .onChange(of: model.session?.account.address) { model.notificationBindingChanged() } |
| 35 | .onChange(of: phase) { _, now in now == .active ? model.foreground() : model.background() } |
| 36 | .onChange(of: model.session?.status) { _, status in if status == .signedOut { model.sessionEnded() } } |
| 37 | .onChange(of: model.session?.needsYouCount) { _, count in model.needsYouChanged(count) } |
| 38 | .onChange(of: model.session?.tasks) { _, tasks in |
| 39 | // A task that ended while its end push was lost leaves the Lock Screen. |
| 40 | guard let tasks else { return } |
| 41 | let live = model.liveActivities |
| 42 | Task { await live.endFinished(tasks) } |
| 43 | } |
| 44 | // A Live Activity's tap: its task. |
| 45 | .onOpenURL { url in if let route = PushRoute(url: url) { model.open(route) } } |
| 46 | .task { model.foreground(); await model.runRenewals() } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// The signed-in phone: the tabs, with Oficina ID's notice above them. |
| 51 | struct PhoneSignedIn: View { |
| 52 | let model: EquipoAppModel |
| 53 | let session: EquipoSession |
| 54 | var body: some View { |
| 55 | PhoneTabs(model: model, session: session) |
| 56 | .id(ObjectIdentifier(session)) |
| 57 | .safeAreaInset(edge: .top, spacing: 0) { |
| 58 | if session.account.oficinaIdentity != nil { EquipoOficinaNotice(model: model) } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | struct PhoneTabs: View { |
| 64 | let model: EquipoAppModel |
| 65 | let session: EquipoSession |
| 66 | @State private var tab: Destination.Pane = .conversation |
| 67 | @State private var conversationPath: [String] = [] |
| 68 | @State private var needsPath: [String] = [] |
| 69 | @State private var tasksPath: [String] = [] |
| 70 | @State private var projectsPath: [ProjectRoute] = [] |
| 71 | private enum ProjectRoute: Hashable { case project(String), task(String) } |
| 72 | @State private var focusCard: String? |
| 73 | @State private var showingAccount = false |
| 74 | |
| 75 | var body: some View { |
| 76 | TabView(selection: $tab) { |
| 77 | Tab(String(localized: "tab.conversation", bundle: .module), systemImage: "bubble.left.and.bubble.right", value: .conversation) { |
| 78 | stack($conversationPath, title: String(localized: "tab.conversation", bundle: .module)) { |
| 79 | ConversationScreen(session: session, focus: $focusCard) { conversationPath.append($0) } |
| 80 | } |
| 81 | } |
| 82 | Tab(String(localized: "tab.needs", bundle: .module), systemImage: "tray", value: .needs) { |
| 83 | stack($needsPath, title: String(localized: "tab.needs", bundle: .module)) { |
| 84 | NeedsScreen(session: session) { needsPath.append($0) } |
| 85 | } |
| 86 | } |
| 87 | .badge(session.needsYouCount ?? 0) |
| 88 | Tab(String(localized: "nav.projects", bundle: .module), systemImage: "folder", value: .projects) { |
| 89 | NavigationStack(path: $projectsPath) { |
| 90 | ProjectsScreen(session: session) { projectsPath.append(.project($0)) } |
| 91 | .navigationTitle(String(localized: "nav.projects", bundle: .module)) |
| 92 | .navigationBarTitleDisplayMode(.inline) |
| 93 | .navigationDestination(for: ProjectRoute.self) { route in |
| 94 | switch route { |
| 95 | case .project(let id): |
| 96 | ProjectDetailScreen(session: session, projectID: id) { project in |
| 97 | session.projectCatalog.selection = project |
| 98 | conversationPath = []; tab = .conversation |
| 99 | } openTask: { projectsPath.append(.task($0)) } |
| 100 | case .task(let id): TaskScreen(session: session, taskID: id) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | Tab(String(localized: "tab.tasks", bundle: .module), systemImage: "checklist", value: .tasks) { |
| 106 | stack($tasksPath, title: String(localized: "tab.tasks", bundle: .module)) { |
| 107 | TasksScreen(session: session) { tasksPath.append($0) } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | .modifier(SettingsSheetPresenter(session: session)) |
| 112 | // Another tab, or a task opened from any of them: the keyboard goes. |
| 113 | .onChange(of: tab) { KeyboardFrame.dismiss() } |
| 114 | .onChange(of: conversationPath) { KeyboardFrame.dismiss() } |
| 115 | .onChange(of: needsPath) { KeyboardFrame.dismiss() } |
| 116 | .onChange(of: tasksPath) { KeyboardFrame.dismiss() } |
| 117 | .onChange(of: projectsPath) { KeyboardFrame.dismiss() } |
| 118 | .sheet(isPresented: $showingAccount) { |
| 119 | NavigationStack { AccountScreen(model: model, session: session).navigationTitle(session.teamName) } |
| 120 | } |
| 121 | .onChange(of: model.destination, initial: true) { _, destination in |
| 122 | guard let destination else { return } |
| 123 | go(destination) |
| 124 | model.destination = nil |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// A tapped notification or Live Activity: its tab, at its card or task. |
| 129 | private func go(_ destination: Destination) { |
| 130 | showingAccount = false |
| 131 | tab = destination.pane |
| 132 | switch destination.pane { |
| 133 | case .conversation: conversationPath = []; focusCard = destination.cardID |
| 134 | case .needs: needsPath = [] |
| 135 | case .projects: projectsPath = [] |
| 136 | case .tasks: tasksPath = destination.taskID.map { [$0] } ?? [] |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private func stack<C: View>(_ path: Binding<[String]>, title: String, @ViewBuilder content: () -> C) -> some View { |
| 141 | NavigationStack(path: path) { |
| 142 | content() |
| 143 | .navigationTitle(title).navigationBarTitleDisplayMode(.inline) |
| 144 | .toolbar { |
| 145 | ToolbarItem(placement: .topBarLeading) { |
| 146 | Button { showingAccount = true } label: { Image(systemName: "person.crop.circle") } |
| 147 | .accessibilityLabel(Text(String(localized: "account.title", bundle: .module))) |
| 148 | } |
| 149 | } |
| 150 | .navigationDestination(for: String.self) { id in |
| 151 | TaskScreen(session: session, taskID: id).navigationTitle(String(localized: "task.title", bundle: .module)).navigationBarTitleDisplayMode(.inline) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | #endif |
| 157 | |