Play back videos in focused attachment view

This commit is contained in:
Shadowfacts 2023-05-03 23:30:33 -04:00
parent a37423a119
commit ad55851090
1 changed files with 23 additions and 4 deletions

View File

@ -7,17 +7,27 @@
import SwiftUI import SwiftUI
import MatchedGeometryPresentation import MatchedGeometryPresentation
import AVKit
class FocusedAttachmentController: ViewController { class FocusedAttachmentController: ViewController {
unowned let parent: ComposeController unowned let parent: ComposeController
let attachment: DraftAttachment let attachment: DraftAttachment
let thumbnailController: AttachmentThumbnailController let thumbnailController: AttachmentThumbnailController
private let player: AVPlayer?
init(parent: ComposeController, attachment: DraftAttachment, thumbnailController: AttachmentThumbnailController) { init(parent: ComposeController, attachment: DraftAttachment, thumbnailController: AttachmentThumbnailController) {
self.parent = parent self.parent = parent
self.attachment = attachment self.attachment = attachment
self.thumbnailController = thumbnailController self.thumbnailController = thumbnailController
if case let .file(url, type) = attachment.data,
type.conforms(to: .movie) {
self.player = AVPlayer(url: url)
self.player!.isMuted = true
} else {
self.player = nil
}
} }
var view: some View { var view: some View {
@ -35,16 +45,20 @@ class FocusedAttachmentController: ViewController {
VStack(spacing: 0) { VStack(spacing: 0) {
Spacer(minLength: 0) Spacer(minLength: 0)
let attachmentView = if let player = controller.player {
ControllerView(controller: { controller.thumbnailController }) VideoPlayer(player: player)
.environment(\.attachmentThumbnailConfiguration, .init(contentMode: .fit, fullSize: true))
.matchedGeometryDestination(id: attachment.id) .matchedGeometryDestination(id: attachment.id)
if #available(iOS 16.0, *) { .onAppear {
player.play()
}
} else if #available(iOS 16.0, *) {
ZoomableScrollView { ZoomableScrollView {
attachmentView attachmentView
.matchedGeometryDestination(id: attachment.id)
} }
} else { } else {
attachmentView attachmentView
.matchedGeometryDestination(id: attachment.id)
} }
Spacer(minLength: 0) Spacer(minLength: 0)
@ -71,6 +85,11 @@ class FocusedAttachmentController: ViewController {
.padding([.top, .leading], 4) .padding([.top, .leading], 4)
}) })
} }
private var attachmentView: some View {
ControllerView(controller: { controller.thumbnailController })
.environment(\.attachmentThumbnailConfiguration, .init(contentMode: .fit, fullSize: true))
}
} }
} }