SPB Git

Repository Public

SPMailApp.swift 3.7 KB 75 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/iOS/SPMailApp.swift3// Description: SP Mail for iOS / iPadOS — adaptive split view, multiple scenes (each window owns its navigation state, all share the session and4// the sync engines), hardware-keyboard commands. The application delegate wires the system layer (Sources/Shared/System): notification5// delegate and background tasks registered before the launch finishes, APNs token, remote notifications.6import SPMailCore7import SPMailSystem8import SwiftUI9import UIKit1011@MainActor12final class AppDelegate: NSObject, UIApplicationDelegate {13    /// The one model of the process: a push, a notification action or a background task can start the app without any scene.14    var model: AppModel? { AppRuntime.model }15    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {16        AppModel.purgeTemporaryFiles()17        // a launch in the background (push, background refresh) is not « active »: new mail must still be announced18        AppActivity.isActive = application.applicationState == .active19        // notification delegate and background task handlers must be registered before the launch finishes20        AppRuntime.model.system.install()21        return true22    }23    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {24        Task { await AppRuntime.model.didRegisterForPush(deviceToken) }25    }26    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {27        AppRuntime.model.didFailToRegisterForPush(error)28    }29    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async -> UIBackgroundFetchResult {30        // a silent push (badge update) or an alert while the app runs: sync within the time the system gives, then put the badge right31        guard let payload = NotificationPayload(userInfo: userInfo) else { return .noData }32        return await AppRuntime.model.system.notifications.handleRemote(payload) ? .newData : .failed33    }34}3536/// Background refresh and maintenance are asked for when the app leaves the foreground (see BackgroundWork). iOS alone decides if and when37/// a task runs (usage patterns, battery, network); nothing stays connected while the app is suspended.38@MainActor39enum BackgroundRefresh {40    static func schedule() { BackgroundWork.schedule(AppRuntime.model) }41}4243/// The root of ONE scene. The first scene drives the app's main MailScene; every further window (iPad « New Window », Stage Manager)44/// gets its own: folder, list, selection and search are independent, session and engines are shared.45struct SceneRoot: View {46    let app: AppModel47    @State private var scene: MailScene?4849    var body: some View {50        Group {51            if let scene { RootView(app: app, scene: scene) } else { SPColor.bg.ignoresSafeArea() }52        }53        .systemScene(app, scene)54        .onAppear { if let scene { app.reclaim(scene) } else { scene = app.claimScene() } }55        .onDisappear { if let scene { app.releaseScene(scene) } }56        #if DEBUG57        .task { if AppConfig.selfTest { await SelfTest.run(app) } else { await DemoFixtures.runIfRequested(app) } }58        #endif59    }60}6162@main63struct SPMailApp: App {64    @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate65    @State private var model = AppRuntime.model6667    var body: some Scene {68        let model = model69        return WindowGroup {70            SceneRoot(app: model)71        }72        .commands { MailCommands(app: model); SystemCommands(app: model) }73    }74}75
Files main
Menu

Theme