2022-12-24 07:29:45 +00:00
|
|
|
import AVKit
|
2023-02-19 10:44:18 +00:00
|
|
|
import DesignSystem
|
2023-01-29 09:19:59 +00:00
|
|
|
import Env
|
2023-09-18 05:01:23 +00:00
|
|
|
import Observation
|
2023-01-30 06:27:06 +00:00
|
|
|
import SwiftUI
|
2022-12-24 07:29:45 +00:00
|
|
|
|
2023-09-15 10:46:15 +00:00
|
|
|
@MainActor
|
2023-09-18 05:01:23 +00:00
|
|
|
@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
|
|
|
|
2023-01-29 09:19:59 +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
|
2023-01-29 10:17:43 +00:00
|
|
|
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
2023-01-29 09:19:59 +00:00
|
|
|
if autoPlay {
|
|
|
|
player?.play()
|
2023-02-04 06:53:03 +00:00
|
|
|
} else {
|
|
|
|
player?.pause()
|
2023-01-29 09:19:59 +00:00
|
|
|
}
|
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)
|
2023-09-15 10:46:15 +00:00
|
|
|
{ _ in
|
|
|
|
Task { @MainActor [weak self] in
|
|
|
|
if autoPlay {
|
|
|
|
self?.play()
|
|
|
|
}
|
2023-02-04 06:53:03 +00:00
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-22 05:38:30 +00:00
|
|
|
|
2023-01-17 18:41:46 +00:00
|
|
|
func pause() {
|
|
|
|
player?.pause()
|
|
|
|
}
|
2023-01-22 05:38:30 +00:00
|
|
|
|
2023-01-17 18:41:46 +00:00
|
|
|
func play() {
|
2023-09-15 10:46:15 +00:00
|
|
|
player?.seek(to: CMTime.zero)
|
2023-01-17 18:41:46 +00:00
|
|
|
player?.play()
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-24 07:29:45 +00:00
|
|
|
deinit {
|
2023-09-15 10:46:15 +00:00
|
|
|
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: nil)
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VideoPlayerView: View {
|
2023-01-17 18:41:46 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
2023-02-22 06:06:01 +00:00
|
|
|
@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
|
|
|
|
2023-09-18 05:01:23 +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 {
|
2023-02-19 10:44:18 +00:00
|
|
|
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)
|
2023-02-19 10:44:18 +00:00
|
|
|
|
|
|
|
if !preferences.autoPlayVideo {
|
|
|
|
Image(systemName: "play.fill")
|
2023-02-22 06:06:01 +00:00
|
|
|
.font(isCompact ? .body : .largeTitle)
|
2023-02-19 10:44:18 +00:00
|
|
|
.foregroundColor(theme.tintColor)
|
2023-02-22 06:06:01 +00:00
|
|
|
.padding(.all, isCompact ? 6 : nil)
|
2023-02-19 10:44:18 +00:00
|
|
|
.background(Circle().fill(.thinMaterial))
|
|
|
|
.padding(theme.statusDisplayStyle == .compact ? 0 : 10)
|
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
}.onAppear {
|
2023-01-29 09:19:59 +00:00
|
|
|
viewModel.preparePlayer(autoPlay: preferences.autoPlayVideo)
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
2023-08-09 11:53:41 +00:00
|
|
|
.onDisappear {
|
|
|
|
viewModel.pause()
|
|
|
|
}
|
2023-02-17 17:17:51 +00:00
|
|
|
.cornerRadius(4)
|
2023-09-18 05:01:23 +00:00
|
|
|
.onChange(of: scenePhase) { _, newValue in
|
|
|
|
switch newValue {
|
2023-01-17 18:41:46 +00:00
|
|
|
case .background, .inactive:
|
|
|
|
viewModel.pause()
|
|
|
|
case .active:
|
2023-02-04 06:53:03 +00:00
|
|
|
if preferences.autoPlayVideo {
|
|
|
|
viewModel.play()
|
|
|
|
}
|
2023-01-17 18:41:46 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2023-09-18 05:01:23 +00:00
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|