77 lines
3.4 KiB
Swift
77 lines
3.4 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
|
|
|
|
/// 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 = 2) {
|
|
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
|
|
guard retry > 0 else { return }
|
|
Task { @MainActor in
|
|
try? await Task.sleep(for: .milliseconds(300))
|
|
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() }
|
|
}
|
|
}
|
|
}
|