Files
stemify/ios/Stemify/App/StemifyApp.swift
T

66 lines
2.6 KiB
Swift

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 <album> 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
@MainActor static func lock(_ upright: Bool) {
let m: UIInterfaceOrientationMask = upright ? .portrait : .allButUpsideDown
guard m != mask else { return }
mask = m
for case let scene as UIWindowScene in UIApplication.shared.connectedScenes {
scene.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
if upright { scene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait)) { _ in } }
}
}
}
@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()
// Keep downloaded playlists in step with the server, like Plexamp.
Task { await AppGraph.shared.downloads.refresh() }
}
}
}