SPB Git

Repository Public MIT

RootView.swift 8.4 KB 207 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Packages/AstraUI/Sources/Shell/RootView.swift10// Module           : Shell11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import SwiftUI89import LocalAuthentication90import AstraCore91import AstraDesign9293/// The app's entry view: boots the services, then shows onboarding, the lock screen or the five tabs.94public struct AstraRootView: View {95    @State private var app: AppModel?96    @State private var failure: String?97    public init() {}9899    public var body: some View {100        Group {101            if let app { MainView().environment(app) }102            else if let failure { BootFailureView(message: failure) { Task { await boot() } } }103            else { LaunchView() }104        }105        .task { if app == nil { await boot() } }106    }107108    private func boot() async {109        failure = nil110        do { app = try await AppModel.bootstrap() }111        catch { failure = AstraError.wrap(error).message }112    }113}114115struct LaunchView: View {116    var body: some View {117        ZStack {118            Starfield(density: 0.8)119            AstraGlyph().frame(width: 96, height: 96)120        }121        .accessibilityLabel("ORIX Astra is starting")122    }123}124125struct BootFailureView: View {126    let message: String127    let retry: () -> Void128    var body: some View {129        ZStack {130            Starfield()131            EmptyStateView(symbol: "exclamationmark.triangle", title: "ORIX Astra could not open its data", message: message) {132                Button("Try again", action: retry).buttonStyle(AstraPrimaryButtonStyle())133            }134        }135    }136}137138struct MainView: View {139    @Environment(AppModel.self) private var app140    @Environment(\.scenePhase) private var phase141142    var body: some View {143        @Bindable var app = app144        ZStack {145            if !app.onboardingDone { OnboardingView().transition(.opacity) }146            else {147                TabView(selection: $app.tab) {148                    Tab(AppTab.chat.title, systemImage: AppTab.chat.symbol, value: AppTab.chat) { ChatHome() }149                    Tab(AppTab.models.title, systemImage: AppTab.models.symbol, value: AppTab.models) { ModelsScreen() }150                    Tab(AppTab.arena.title, systemImage: AppTab.arena.symbol, value: AppTab.arena) { ArenaScreen() }151                    Tab(AppTab.agents.title, systemImage: AppTab.agents.symbol, value: AppTab.agents) { AgentsScreen() }152                    Tab(AppTab.lab.title, systemImage: AppTab.lab.symbol, value: AppTab.lab) { LabScreen() }153                }154                .transition(.opacity)155            }156            if app.isLocked { LockView().transition(.opacity).zIndex(10) }157        }158        .tint(AstraColor.violet)159        .astraToast($app.toast)160        .preferredColorScheme(app.appearance)161        .animation(AstraMotion.gentle, value: app.onboardingDone)162        .animation(AstraMotion.gentle, value: app.isLocked)163        .onChange(of: phase) { _, new in164            // Lock when the app leaves the foreground; the lock view also hides content in the app switcher.165            if new == .background, app.appLockEnabled { app.isLocked = true }166            if new == .background { Task { await app.local.refreshInstalled() } }167        }168        .onChange(of: app.tab) { _, _ in AstraHaptics.selection() }169    }170}171172/// Covers the app until the owner of the device authenticates. Falls back to the device passcode when biometrics fail or are unavailable.173struct LockView: View {174    @Environment(AppModel.self) private var app175    @State private var message: String?176177    var body: some View {178        ZStack {179            Starfield(density: 0.8)180            VStack(spacing: 22) {181                AstraGlyph().frame(width: 88, height: 88)182                Text("ORIX Astra is locked").font(AstraFont.title).foregroundStyle(AstraColor.textPrimary)183                if let message { Text(message).font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary).multilineTextAlignment(.center) }184                Button("Unlock") { Task { await unlock() } }.buttonStyle(AstraPrimaryButtonStyle()).frame(maxWidth: 240).accessibilityIdentifier("unlock")185            }186            .padding(AstraSpace.xxl)187        }188        .task { await unlock() }189    }190191    private func unlock() async {192        let context = LAContext()193        var error: NSError?194        // .deviceOwnerAuthentication = Face ID / Touch ID with the passcode as fallback.195        guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else {196            // No passcode is set on this device: there is nothing to authenticate against, so the lock cannot protect anything.197            message = "This iPhone has no passcode, so the app lock cannot be used. It has been turned off."198            app.appLockEnabled = false; app.isLocked = false199            try? await app.store.setSetting("privacy.appLock", false)200            return201        }202        do {203            if try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Unlock your conversations") { app.isLocked = false; message = nil }204        } catch { message = "Authentication did not succeed. Try again." }205    }206}207
Files main
Menu

Theme