Files
stemify/ios/Stemify/App/StemifyApp.swift
T
rootandClaude Opus 5.5 e02428ab70 Re-pick Plex server address on network change, foreground, and stream failure (dev 36)
The address chosen at sign-in (LAN first) was kept forever, so away from
home streams hit a dead LAN address (TLS error) or the relay (503). Store
every address plex.tv lists, probe them all bypassing the offline cache,
and keep the best one that answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:56:02 -08:00

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