Files
Commilitia-Drop/ios/CDrop/Sources/CDropApp.swift
T

90 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
// cdrop iOS arch A ios/PLAN.md SwiftUI +
// WKWebView EngineController AuthManager
@main
struct CDropApp: App
{
@UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
@Environment(\.scenePhase) private var scenePhase
@State private var auth = AuthManager()
@State private var engine = EngineController()
var body: some Scene
{
WindowGroup
{
AppRoot(auth: auth, engine: engine)
}
.onChange(of: scenePhase)
{ _, phase in
guard phase == .active else { return }
// App Group iOS share 宿 app
// app
// app onAppear
//
engine.loadPendingShares()
// / ready
//
if auth.session != nil
{
engine.provisionWidgetSessionIfNeeded()
}
}
}
}
//
struct AppRoot: View
{
let auth: AuthManager
let engine: EngineController
var body: some View
{
Group
{
if auth.session != nil
{
RootView()
.environment(engine)
.environment(auth)
}
else
{
LoginView()
.environment(auth)
}
}
.onAppear
{
engine.auth = auth
if ProcessInfo.processInfo.environment["CDROP_DEBUG_SESSION"] == "1"
{
auth.debugSession()
}
// userId reset()
// authExpired / #3
if let uid = auth.session?.user.id { engine.activateRecords(userId: uid) }
}
// session nil authExpired /
.onChange(of: auth.session?.user.id)
{ _, uid in
if let uid { engine.activateRecords(userId: uid) }
}
// Share Extension commilitia-drop://share
// AppRoot 便 / onAppear
.onOpenURL
{ url in
guard url.scheme == "commilitia-drop", url.host == "share" else { return }
engine.loadPendingShares()
}
// AppDelegate +
.onReceive(NotificationCenter.default.publisher(for: .cdropSessionRevoked))
{ _ in
engine.reset()
auth.logout()
}
}
}