SPB Git

Repository Public

RootView.swift 5.4 KB 118 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/App/RootView.swift4//  Description: Root of the interface: sign-in, the adaptive split navigation (iPhone stack / iPad sidebar) and the app lock.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import SwiftUI1112struct RootView: View {13    @Environment(AppEnvironment.self) private var env14    @Environment(\.scenePhase) private var scenePhase1516    var body: some View {17        ZStack {18            Group {19                switch env.auth.phase {20                case .signedOut: SignInView().transition(.opacity)21                case .signedIn: MainView().transition(.opacity)22                }23            }24            .animation(.smooth(duration: 0.3), value: env.auth.phase)2526            // The lock and the privacy cover sit above everything, sheets included (they are presented by MainView, below).27            if env.auth.phase == .signedIn && env.appLock.isLocked { LockView().transition(.opacity).zIndex(2) }28            else if env.auth.phase == .signedIn && env.appLock.isEnabled && env.appLock.isObscured { PrivacyCover().zIndex(1) }29        }30        .preferredColorScheme(env.preferences.theme.colorScheme)31        .tint(.accentColor)32        .onChange(of: scenePhase) { _, phase in33            switch phase {34            case .active:35                env.appLock.isObscured = false36                env.appLock.willEnterForeground()37                Task { if env.appLock.isLocked { await env.appLock.unlock() }; await env.synchronize() }38            case .inactive: env.appLock.isObscured = true39            case .background: env.appLock.isObscured = true; env.appLock.didEnterBackground(); BackgroundSync.schedule(); env.stopSync()40            @unknown default: break41            }42        }43        .onChange(of: env.network.reconnections) { Task { await env.synchronize() } }44        .onOpenURL { env.handle(url: $0) }45        .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { activity in if let url = activity.webpageURL { env.handle(url: url) } }46    }47}4849/// List + conversation. `NavigationSplitView` gives a persistent sidebar on iPad (and in wide multitasking widths) and50/// collapses into a navigation stack on iPhone and in narrow Split View / Slide Over.51struct MainView: View {52    @Environment(AppEnvironment.self) private var env53    // iPad: the sidebar is shown next to the conversation from the start, in both orientations (the user can hide it)54    @State private var visibility: NavigationSplitViewVisibility = .all55    @State private var compactColumn: NavigationSplitViewColumn = .detail5657    var body: some View {58        @Bindable var router = env.router59        NavigationSplitView(columnVisibility: $visibility, preferredCompactColumn: $compactColumn) {60            ConversationList(onOpen: { compactColumn = .detail })61                .navigationSplitViewColumnWidth(min: 280, ideal: 330, max: 420)62        } detail: {63            NavigationStack {64                if let session = router.current {65                    ChatView(session: session).id(session.id)66                } else {67                    ContentUnavailableView { Label("SP Chat", systemImage: "bubble.left.and.text.bubble.right") } description: { Text("Choisis une conversation ou commence-en une nouvelle.") }68                        .task { router.current = env.newSession() }69                }70            }71        }72        .navigationSplitViewStyle(.balanced)73        .sheet(item: $router.sheet) { sheet in74            switch sheet {75            case .settings: SettingsView()76            case .search: SearchView(onOpen: { compactColumn = .detail })77            case .prompts: PromptLibraryView(onInsert: nil)78            case .commandCenter: CommandCenterView(onOpen: { compactColumn = .detail }).presentationDetents([.large])79            case .explorer: ExplorerView().presentationSizing(.page)80            case .arena: ArenaView()81            case .constellation: ConstellationView(onOpen: { compactColumn = .detail }).presentationSizing(.page)      // the graph deserves the width of the iPad82            case .projects: ProjectsView()83            case .research: ResearchView(onOpen: { compactColumn = .detail })84            case .images: ImageStudioView()85            }86        }87        .onChange(of: router.current?.id) { compactColumn = .detail }88    }89}9091/// Covers the content in the app switcher when the lock is enabled.92struct PrivacyCover: View {93    var body: some View {94        ZStack { Color.appBackground.ignoresSafeArea(); BrandMark(size: 84) }95            .accessibilityHidden(true)96    }97}9899struct LockView: View {100    @Environment(AppEnvironment.self) private var env101    var body: some View {102        ZStack {103            Color.appBackground.ignoresSafeArea()104            VStack(spacing: 22) {105                BrandMark(size: 88)106                Text("SP Chat est verrouillé").font(.title3.weight(.semibold)).foregroundStyle(Color.textPrimary)107                if let e = env.appLock.lastError { Text(e).font(.footnote).foregroundStyle(Color.danger).multilineTextAlignment(.center) }108                Button { Task { await env.appLock.unlock() } } label: {109                    Label("Déverrouiller avec \(AppLock.biometryName)", systemImage: "lock.open.fill").font(.body.weight(.semibold)).frame(maxWidth: 320).frame(minHeight: 50)110                }111                .buttonStyle(.borderedProminent).disabled(env.appLock.isAuthenticating)112                .accessibilityIdentifier("lock.unlock")113            }114            .padding(32)115        }116    }117}118
Files main
Menu

Theme