oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1import BaseKit
2import RastrilloApple
3import SwiftUI
4import UIKit
5
6/// Base for the iPhone: Ask first, the apps one tap away (A).
7@main struct BasePhoneApp: App {
8 @State private var session = BaseSession.live()
9 @Environment(\.scenePhase) private var phase
10
11 init() {
12 // Before the first frame, so nothing paints in a fallback face.
13 BaseFonts.register()
14 }
15
16 var body: some Scene {
17 WindowGroup {
18 PhoneRoot(session: session)
19 .onChange(of: phase) { _, now in
20 switch now {
21 case .active: Task { await session.foreground() }
22 case .background: session.background()
23 default: break
24 }
25 }
26 .task { await session.oficina.start() }
27 }
28 }
29}
30
31/// Sign-in until there is a session, then Ask.
32struct PhoneRoot: View {
33 let session: BaseSession
34 @State private var openURL: BaseOpenURL?
35
36 var body: some View {
37 let actions = PhoneActions.make(session: session)
38 Group {
39 if let state = session.signedIn {
40 PhoneAskScreen(me: session.me, home: session.home, ask: session.ask, transport: session.iconTransport, actions: actions)
41 .safeAreaInset(edge: .top, spacing: 0) {
42 NativeOfflineBanner(origin: state.origin, message: BaseText.text("native.offline"))
43 }
44 } else {
45 BaseSignIn(model: session.oficina, openURL: { UIApplication.shared.open($0) })
46 }
47 }
48 .baseAppearance(session.me?.appearance ?? .system)
49 .onOpenURL { url in
50 if openURL == nil { openURL = BaseOpenURL(showAsk: {}, openWeb: { actions.openWeb($0) }) }
51 openURL?.handle(url)
52 }
53 }
54}
55
56/// The iPhone's side of BaseActions: Calls and Equipo by their schemes,
57/// everything else in the browser (P12).
58@MainActor enum PhoneActions {
59 static func make(session: BaseSession) -> BaseActions {
60 BaseActions(
61 open: { app in
62 if case .url(let url) = Opener.phone(app, canOpen: { UIApplication.shared.canOpenURL($0) }) {
63 UIApplication.shared.open(url)
64 }
65 },
66 openWeb: { path in
67 guard let origin = session.signedIn?.origin, let url = URL(string: path, relativeTo: origin)?.absoluteURL else { return }
68 UIApplication.shared.open(url)
69 },
70 signOut: { session.signOut() },
71 teams: { await session.teams() },
72 switchTo: { await session.switchTo($0) },
73 setUpAsk: { Task { await session.setUpAsk() } })
74 }
75}
76