From ad558510906c6a2da3397f4ee5a8c2386d959229 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 3 May 2023 23:30:33 -0400 Subject: [PATCH] Play back videos in focused attachment view --- .../FocusedAttachmentController.swift | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift index 2e227d19..201e369b 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift @@ -7,17 +7,27 @@ import SwiftUI import MatchedGeometryPresentation +import AVKit class FocusedAttachmentController: ViewController { unowned let parent: ComposeController let attachment: DraftAttachment let thumbnailController: AttachmentThumbnailController + private let player: AVPlayer? init(parent: ComposeController, attachment: DraftAttachment, thumbnailController: AttachmentThumbnailController) { self.parent = parent self.attachment = attachment 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 { @@ -35,16 +45,20 @@ class FocusedAttachmentController: ViewController { VStack(spacing: 0) { Spacer(minLength: 0) - let attachmentView = - ControllerView(controller: { controller.thumbnailController }) - .environment(\.attachmentThumbnailConfiguration, .init(contentMode: .fit, fullSize: true)) + if let player = controller.player { + VideoPlayer(player: player) .matchedGeometryDestination(id: attachment.id) - if #available(iOS 16.0, *) { + .onAppear { + player.play() + } + } else if #available(iOS 16.0, *) { ZoomableScrollView { attachmentView + .matchedGeometryDestination(id: attachment.id) } } else { attachmentView + .matchedGeometryDestination(id: attachment.id) } Spacer(minLength: 0) @@ -71,6 +85,11 @@ class FocusedAttachmentController: ViewController { .padding([.top, .leading], 4) }) } + + private var attachmentView: some View { + ControllerView(controller: { controller.thumbnailController }) + .environment(\.attachmentThumbnailConfiguration, .init(contentMode: .fit, fullSize: true)) + } } }