LiveApple/LiveApple/ContentView.swift

80 lines
2.3 KiB
Swift

//
// ContentView.swift
// LiveApple
//
// Created by Shadowfacts on 9/24/22.
//
import SwiftUI
import AVFoundation
import ActivityKit
import BackgroundTasks
import MediaPlayer
struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
let controller: PlayerController?
var body: some View {
VStack(spacing: 16) {
Spacer()
if let controller {
ControllerView(controller: controller)
} else {
Text("no controller")
}
Spacer()
}
.onChange(of: scenePhase) { newValue in
if newValue == .active {
try! AVAudioSession.sharedInstance().setCategory(.playback)
try! AVAudioSession.sharedInstance().setActive(true)
}
}
}
}
struct ControllerView: View {
@ObservedObject var controller: PlayerController
@State var hasStarted = false
var body: some View {
PlayerView(player: controller.player)
.onChange(of: controller.currentFrame) { frame in
guard let frame else {
return
}
guard !hasStarted else {
return
}
hasStarted = true
let state = BadAppleAttributes.ContentState(frame: frame)
let attributes = BadAppleAttributes()
let activity = try! Activity.request(attributes: attributes, contentState: state)
controller.activity = activity
print(activity)
do {
try BGTaskScheduler.shared.submit(BGProcessingTaskRequest(identifier: "task"))
} catch {
fatalError("submitting task: \(error)")
}
// lol
BGTaskScheduler.shared.perform(Selector(("_simulateLaunchForTaskWithIdentifier:")), with: "task")
}
if let frame = controller.currentFrame {
FrameView(frame: frame)
} else {
Rectangle()
.fill(.black)
.frame(width: 60 / UIScreen.main.scale, height: 45 / UIScreen.main.scale)
}
}
}