| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | |
| 4 | /// Sheets' account menu for an Oficina ID (spec §9): who is signed in, the |
| 5 | /// identity's teams that have Sheets, "Use another Oficina ID" (Paul, |
| 6 | /// 2026-11-19), "Other ways to sign in…" and "Sign out of <label>". The |
| 7 | /// several-identities menu is slice A2. |
| 8 | @MainActor public struct SheetsTeamPicker: View { |
| 9 | @ObservedObject private var model: OficinaIDModel |
| 10 | private let current: OficinaSignedIn |
| 11 | private let canLeave: Bool |
| 12 | private let refresh: @MainActor () async -> Void |
| 13 | private let otherWays: () -> Void |
| 14 | private let useAnother: () -> Void |
| 15 | private let signOut: () -> Void |
| 16 | private let done: () -> Void |
| 17 | |
| 18 | /// `canLeave` is false while leaving would strand something (a document |
| 19 | /// window of this identity open, an edit not yet confirmed): switching |
| 20 | /// team, "Use another" and signing out are disabled then. |
| 21 | public init(model: OficinaIDModel, current: OficinaSignedIn, canLeave: Bool, |
| 22 | refresh: @escaping @MainActor () async -> Void = {}, |
| 23 | otherWays: @escaping () -> Void, useAnother: @escaping () -> Void, |
| 24 | signOut: @escaping () -> Void, done: @escaping () -> Void) { |
| 25 | self.model = model; self.current = current; self.canLeave = canLeave; self.refresh = refresh |
| 26 | self.otherWays = otherWays; self.useAnother = useAnother; self.signOut = signOut; self.done = done |
| 27 | } |
| 28 | |
| 29 | /// The cached teams that have Sheets, the current one always among them. |
| 30 | public static func teams(_ cached: [OficinaTeam]?, current: OficinaTeam) -> [OficinaTeam] { |
| 31 | var teams = (cached ?? []).filter { $0.origin(for: SheetsOficinaSession.configuration.app) != nil } |
| 32 | if !teams.contains(where: { $0.sqid == current.sqid }) { teams.append(current) } |
| 33 | return teams.sorted { ($0.name.localizedLowercase, $0.sqid) < ($1.name.localizedLowercase, $1.sqid) } |
| 34 | } |
| 35 | |
| 36 | private var teams: [OficinaTeam] { Self.teams(model.store.teams(current.identity.id)?.teams, current: current.team) } |
| 37 | |
| 38 | public var body: some View { |
| 39 | NavigationStack { |
| 40 | List { |
| 41 | Section { |
| 42 | OficinaIdentityRow(identity: current.identity, accent: .sheets) |
| 43 | } header: { Text(verbatim: "Oficina ID") } |
| 44 | Section { |
| 45 | ForEach(teams) { team in |
| 46 | Button { choose(team) } label: { |
| 47 | OficinaTeamRow(team: team).overlay(alignment: .trailing) { |
| 48 | if team.sqid == current.team.sqid { |
| 49 | Image(systemName: "checkmark").font(.headline).foregroundStyle(OficinaPalette.accent(.sheets)) |
| 50 | .padding(.trailing, 14).accessibilityHidden(true) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | .buttonStyle(.plain) |
| 55 | .disabled(team.sqid != current.team.sqid && (!canLeave || model.busy)) |
| 56 | .accessibilityAddTraits(team.sqid == current.team.sqid ? .isSelected : []) |
| 57 | .accessibilityIdentifier("sheets.team-\(team.label)") |
| 58 | } |
| 59 | } header: { Text(verbatim: sheetsMacString("Teams")) } footer: { |
| 60 | if let error = model.error { OficinaErrorLine(error) } |
| 61 | } |
| 62 | Section { |
| 63 | Button(OficinaIDText.text("continue.another")) { done(); useAnother() } |
| 64 | .buttonStyle(.borderless) |
| 65 | .disabled(!canLeave) |
| 66 | .accessibilityIdentifier("sheets.oficinaid.useAnother") |
| 67 | Button(OficinaIDText.text("otherWays")) { done(); otherWays() } |
| 68 | .buttonStyle(.borderless) |
| 69 | .accessibilityIdentifier("sheets.otherWays") |
| 70 | Button(role: .destructive) { done(); signOut() } label: { |
| 71 | Text(verbatim: OficinaIDText.text("signOut", current.identity.label)) |
| 72 | } |
| 73 | .buttonStyle(.borderless) |
| 74 | // Its own colour would hide that it is disabled. |
| 75 | .foregroundStyle(canLeave ? OficinaPalette.negative : OficinaPalette.textMuted) |
| 76 | .disabled(!canLeave) |
| 77 | .accessibilityIdentifier("sheets.oficinaid.signOut") |
| 78 | } |
| 79 | } |
| 80 | .navigationTitle(Text(verbatim: sheetsMacString("Teams"))) |
| 81 | .task { await refresh() } |
| 82 | #if os(iOS) |
| 83 | .navigationBarTitleDisplayMode(.inline) |
| 84 | #endif |
| 85 | .toolbar { |
| 86 | ToolbarItem(placement: .confirmationAction) { |
| 87 | Button { done() } label: { Text(verbatim: sheetsMacString("Done")) } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | .tint(OficinaPalette.accent(.sheets)) |
| 92 | #if os(macOS) |
| 93 | .frame(minWidth: 420, idealWidth: 460, minHeight: 440, idealHeight: 520) |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | private func choose(_ team: OficinaTeam) { |
| 98 | done() |
| 99 | guard team.sqid != current.team.sqid else { return } |
| 100 | Task { await model.choose(team) } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// "Other ways to sign in…": the server address and browser approval kept |
| 105 | /// for self-hosted servers and local accounts (spec §11), with the way back |
| 106 | /// to Oficina ID beneath. |
| 107 | @MainActor public struct SheetsOtherWaysView: View { |
| 108 | @ObservedObject private var connection: NativeConnection |
| 109 | private let oficinaID: () -> Void |
| 110 | |
| 111 | public init(connection: NativeConnection, oficinaID: @escaping () -> Void) { |
| 112 | self.connection = connection; self.oficinaID = oficinaID |
| 113 | } |
| 114 | |
| 115 | public var body: some View { |
| 116 | NativeAccountsView("Sheets", systemImage: "tablecells", connection: connection) |
| 117 | .safeAreaInset(edge: .bottom, spacing: 0) { |
| 118 | Button { connection.cancel(); oficinaID() } label: { |
| 119 | Label { Text(verbatim: sheetsMacString("Sign in with Oficina ID")) } icon: { Image(systemName: "person.crop.circle") } |
| 120 | } |
| 121 | .buttonStyle(.borderless) |
| 122 | .accessibilityIdentifier("sheets.oficinaid.signin") |
| 123 | .padding(16) |
| 124 | .frame(maxWidth: .infinity) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |