IceCubesApp/Packages/Status/Sources/Status/Media/VideoPlayerView.swift

93 lines
2.2 KiB
Swift
Raw Normal View History

2022-12-24 07:29:45 +00:00
import AVKit
import DesignSystem
import Env
import Observation
2023-01-30 06:27:06 +00:00
import SwiftUI
2022-12-24 07:29:45 +00:00
@MainActor
@Observable class VideoPlayerViewModel {
var player: AVPlayer?
2022-12-24 07:29:45 +00:00
private let url: URL
2023-01-17 10:36:01 +00:00
2022-12-24 07:29:45 +00:00
init(url: URL) {
self.url = url
}
2023-01-17 10:36:01 +00:00
func preparePlayer(autoPlay: Bool) {
2022-12-24 07:29:45 +00:00
player = .init(url: url)
2022-12-30 23:07:42 +00:00
player?.isMuted = true
player?.audiovisualBackgroundPlaybackPolicy = .pauses
if autoPlay {
player?.play()
} else {
player?.pause()
}
2022-12-24 07:29:45 +00:00
guard let player else { return }
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
2023-03-13 12:38:28 +00:00
object: player.currentItem, queue: .main)
{ _ in
Task { @MainActor [weak self] in
if autoPlay {
self?.play()
}
}
2022-12-24 07:29:45 +00:00
}
}
2023-01-22 05:38:30 +00:00
func pause() {
player?.pause()
}
2023-01-22 05:38:30 +00:00
func play() {
player?.seek(to: CMTime.zero)
player?.play()
}
2023-01-17 10:36:01 +00:00
2022-12-24 07:29:45 +00:00
deinit {
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: nil)
2022-12-24 07:29:45 +00:00
}
}
struct VideoPlayerView: View {
@Environment(\.scenePhase) private var scenePhase
@Environment(\.isCompact) private var isCompact
2023-09-19 07:18:20 +00:00
@Environment(UserPreferences.self) private var preferences
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
2023-01-30 06:27:06 +00:00
@State var viewModel: VideoPlayerViewModel
2023-01-22 05:38:30 +00:00
2022-12-24 07:29:45 +00:00
var body: some View {
ZStack {
2022-12-24 07:29:45 +00:00
VideoPlayer(player: viewModel.player)
Timeline & Timeline detail accessibility uplift (#1323) * Improve accessibility of StatusPollView Previously, this view did not provide the proper context to indicate that it represented a poll. Now, we’ve added - A container that will stay “Active poll” or “Poll results” when the cursor first hits one of the options; - A prefix to say “Option X of Y” before each option; - A Selected trait on the selected option(s), if present - Consolidating and adding an `.updatesFrequently` trait to the footer view with the countdown. * Add poll description in StatusRowView combinedAccessibilityLabel This largely duplicates the logic in `StatusPollView`. * Improve accessibility of media attachments Previously, the media attachments without alt text would not show up in the consolidated `StatusRowView`, nor would they be meaningfully explained on the status detail screen. Now, they are presented with their attachment type. * Change accessibilityRepresentation of AppAcountsSelectorView * Change Notifications tab title view accessibility representation to Menu Previously it would present as a button * Hide layout `Rectangle`s from accessibility * Consolidate `StatusRowDetailView` accessibility representation * Improve readability of Poll accessibility label * Ensure poll options don’t present as interactive when the poll is finished * Improve accessibility of StatusRowCardView Previously, it would present as four separate elements, including an image without a description, all interactive, none with an interactive trait. Now, it presents as a single element with the `.link` trait * Improve accessibility of StatusRowHeaderView Previously, it had no traits and no actions except inherited ones. Now it presents as a button, triggering its primary action. It also has custom actions corresponding to its context menu * Avoid applying the StatusRowView custom actions to every view when contained * Provide context for the application name * Add pauses to StatusRowView combinedAccessibilityLabel * Hide `TimelineView.scrollToTopView` from accessibility * Set appropriate font style on Notification header After the change the Text needed a `.headline` style to match the prior appearance. * Fix bug in accessibilityRepresentation of TimelineView nav bar title Previously, it would not display the proper label for .remoteLocal filter options. * Ensure that pop-up button nav bar titles are interactive * Ensure TextView responds to Environment.sizeCategory This resolves #1309 * Fix button --------- Co-authored-by: Thomas Ricouard <ricouard77@gmail.com>
2023-03-28 16:48:58 +00:00
.accessibilityAddTraits(.startsMediaSession)
if !preferences.autoPlayVideo {
Image(systemName: "play.fill")
.font(isCompact ? .body : .largeTitle)
.foregroundColor(theme.tintColor)
.padding(.all, isCompact ? 6 : nil)
.background(Circle().fill(.thinMaterial))
.padding(theme.statusDisplayStyle == .compact ? 0 : 10)
}
2022-12-24 07:29:45 +00:00
}.onAppear {
viewModel.preparePlayer(autoPlay: preferences.autoPlayVideo)
2022-12-24 07:29:45 +00:00
}
.onDisappear {
viewModel.pause()
}
.cornerRadius(4)
.onChange(of: scenePhase) { _, newValue in
switch newValue {
case .background, .inactive:
viewModel.pause()
case .active:
if preferences.autoPlayVideo {
viewModel.play()
}
default:
break
}
}
2022-12-24 07:29:45 +00:00
}
}