import AVFoundation import Intents import SwiftUI import UIKit final class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, policy: .longFormAudio) MainActor.assumeIsolated { _ = AppGraph.shared } return true } /// Phone windows go to SwiftUI; CarPlay gets its own scene delegate. func application(_ application: UIApplication, configurationForConnecting session: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { if session.role == .carTemplateApplication { let c = UISceneConfiguration(name: "CarPlay", sessionRole: session.role) c.delegateClass = CarPlayScene.self return c } return UISceneConfiguration(name: "Phone", sessionRole: session.role) } func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { Orientation.mask } /// Siri: "Play on Stemify". func application(_ application: UIApplication, handlerFor intent: INIntent) -> Any? { intent is INPlayMediaIntent ? PlayMediaHandler() : nil } } /// Folded on the iPhone Duo the app stays upright; everywhere else it turns as usual. enum Orientation { nonisolated(unsafe) static var mask: UIInterfaceOrientationMask = .allButUpsideDown /// Folded Duo: portrait only. Unfolded Duo: landscape only. Anything else turns freely. /// Also re-checks the window when the mask is unchanged: a request made while the app was /// in the background can be refused, which left the folded screen stuck sideways on return. @MainActor static func lock(_ m: UIInterfaceOrientationMask, retry: Int = 6) { let changed = m != mask mask = m for case let scene as UIWindowScene in UIApplication.shared.connectedScenes where scene.session.role == .windowApplication { if changed { scene.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations() } let now = UIInterfaceOrientationMask(rawValue: 1 << scene.effectiveGeometry.interfaceOrientation.rawValue) guard m != .allButUpsideDown, !m.contains(now) else { continue } scene.requestGeometryUpdate(.iOS(interfaceOrientations: m)) { _ in } // The request can be dropped silently (mid-unfold, just back from the background), so check it took. guard retry > 0 else { continue } Task { @MainActor in try? await Task.sleep(for: .milliseconds(500)) if mask == m { lock(m, retry: retry - 1) } } } } } @main struct StemifyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate @Environment(\.scenePhase) private var phase var body: some Scene { WindowGroup { RootView() } .onChange(of: phase) { _, p in AppActivity.setPhone(p != .background) guard p == .active else { return } AppGraph.shared.eq.refreshRoute() Task.detached { await AppGraph.shared.plex.reroute("foreground") } // Keep downloaded playlists in step with the server, like Plexamp. Task { await AppGraph.shared.downloads.refresh() } } } }