oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1import RastrilloApple
2import SwiftUI
3
4/// What the apps do for the screens: opening Home's apps and Base's own
5/// pages, and the account's choices. The Mac and iPhone apps fill it in
6/// (NSWorkspace, UIApplication, the kit); snapshots and previews leave it
7/// doing nothing.
8public struct BaseActions {
9 public var open: (HomeData.App) -> Void
10 /// A page of Base on the web, by path ("/", "/signin").
11 public var openWeb: (String) -> Void
12 public var signOut: () -> Void
13 public var teams: () async -> [OficinaTeam]
14 public var switchTo: (OficinaTeam) async -> Void
15 public var setUpAsk: () -> Void
16 /// Ask's set-up on the web (P2): Base's /ask in the browser.
17 public var setUpAskOnWeb: () -> Void { { openWeb("/ask") } }
18
19 public init(open: @escaping (HomeData.App) -> Void = { _ in }, openWeb: @escaping (String) -> Void = { _ in },
20 signOut: @escaping () -> Void = {}, teams: @escaping () async -> [OficinaTeam] = { [] },
21 switchTo: @escaping (OficinaTeam) async -> Void = { _ in }, setUpAsk: @escaping () -> Void = {}) {
22 self.open = open; self.openWeb = openWeb; self.signOut = signOut; self.teams = teams; self.switchTo = switchTo; self.setUpAsk = setUpAsk
23 }
24
25 public static var none: BaseActions { BaseActions() }
26}
27
28/// The person's initials on the warm gradient (`.me`): dark ink on
29/// ember into rose in both themes.
30public struct Avatar: View {
31 let initials: String
32 let size: CGFloat
33 @Environment(\.colorScheme) private var scheme
34 public init(_ initials: String, size: CGFloat = 32) { self.initials = initials; self.size = size }
35 public var body: some View {
36 let p = Palette(scheme)
37 Text(verbatim: initials)
38 .font(BaseFont.body(size * 0.38, .semibold, relativeTo: .caption))
39 .foregroundStyle(p[.ink])
40 .frame(width: size, height: size)
41 .background(LinearGradient(colors: [p[.ember], p[.rose]], startPoint: .topLeading, endPoint: .bottomTrailing), in: Circle())
42 // Two letters in a fixed circle: they don't grow with the text.
43 .dynamicTypeSize(...DynamicTypeSize.large)
44 .accessibilityHidden(true)
45 }
46}
47
48/// The account's choices, as a menu's items: Switch team, Sign out (asks
49/// first), Open Base on the web. The sidebar's foot and the iPhone's
50/// avatar show it.
51public struct AccountMenuItems: View {
52 let actions: BaseActions
53 @Binding var switching: Bool
54 @Binding var confirmingSignOut: Bool
55 public init(actions: BaseActions, switching: Binding<Bool>, confirmingSignOut: Binding<Bool>) {
56 self.actions = actions; _switching = switching; _confirmingSignOut = confirmingSignOut
57 }
58 public var body: some View {
59 Button(BaseText.text("native.account.switch")) { switching = true }
60 Button(BaseText.text("native.account.web")) { actions.openWeb("/") }
61 Divider()
62 Button(BaseText.text("native.account.signout")) { confirmingSignOut = true }
63 }
64}
65
66public extension View {
67 /// Sign out's confirmation and Switch team's list, for whatever shows
68 /// AccountMenuItems.
69 func accountChoices(actions: BaseActions, switching: Binding<Bool>, confirmingSignOut: Binding<Bool>) -> some View {
70 confirmationDialog(BaseText.text("native.settings.account.signout.confirm.title"), isPresented: confirmingSignOut, titleVisibility: .visible) {
71 Button(BaseText.text("native.settings.account.signout.confirm.action"), role: .destructive) { actions.signOut() }
72 Button(BaseText.text("native.settings.account.signout.confirm.cancel"), role: .cancel) {}
73 } message: {
74 Text(BaseText.text("native.settings.account.signout.confirm.message"))
75 }
76 .sheet(isPresented: switching) { TeamPicker(actions: actions) { switching.wrappedValue = false } }
77 }
78}
79
80/// Switch team: the Oficina ID's teams; choosing one opens its Base.
81struct TeamPicker: View {
82 let actions: BaseActions
83 let done: () -> Void
84 @State private var teams: [OficinaTeam]?
85 @Environment(\.colorScheme) private var scheme
86 var body: some View {
87 let p = Palette(scheme)
88 VStack(alignment: .leading, spacing: 12) {
89 HStack {
90 Text(BaseText.text("native.account.switch")).font(BaseFont.heading(20, .semibold))
91 Spacer()
92 Button(action: done) { Icon(.close).frame(width: 18, height: 18).frame(width: 44, height: 44) }
93 .buttonStyle(.plain)
94 .accessibilityLabel(Text(BaseText.text("ask.close")))
95 }
96 if let teams {
97 ForEach(teams) { team in
98 Button {
99 Task { await actions.switchTo(team); done() }
100 } label: {
101 Text(verbatim: team.name).font(BaseFont.body(16, .medium)).frame(maxWidth: .infinity, minHeight: 44, alignment: .leading)
102 }
103 .buttonStyle(BaseButtonStyle(.ghost, expands: true))
104 .disabled(team.origin(for: "base") == nil)
105 }
106 } else {
107 ProgressView().frame(maxWidth: .infinity)
108 }
109 }
110 .foregroundStyle(p[.frost])
111 .padding(20)
112 .frame(minWidth: 340)
113 .background(p[.menuBase])
114 .task { teams = await actions.teams() }
115 }
116}
117
118/// A first step's mark: a ring, or filled with a tick once done.
119struct StepDot: View {
120 let done: Bool
121 var size: CGFloat = 16
122 @Environment(\.colorScheme) private var scheme
123 var body: some View {
124 let p = Palette(scheme)
125 ZStack {
126 if done {
127 Circle().fill(p[.signal])
128 Icon(.done, weight: 3).foregroundStyle(p[.onSignal]).frame(width: size * 0.62, height: size * 0.62)
129 } else {
130 Circle().strokeBorder(p[.ring], lineWidth: 1.5)
131 }
132 }
133 .frame(width: size, height: size)
134 .accessibilityHidden(true)
135 }
136}
137