Skip to content

Commit 97206f0

Browse files
committed
Fix AsyncImage video support
1 parent a2c74e0 commit 97206f0

File tree

5 files changed

+71
-113
lines changed

5 files changed

+71
-113
lines changed

Modules/Sources/AsyncImageKit/Views/AsyncVideoThumbnailView.swift

Lines changed: 0 additions & 91 deletions
This file was deleted.

Modules/Sources/Support/InternalDataProvider.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Foundation
2+
import AVFoundation
3+
import AsyncImageKit
24
import WordPressCoreProtocols
35

46
// This file is all module-internal and provides sample data for UI development
@@ -9,7 +11,8 @@ extension SupportDataProvider {
911
botConversationDataProvider: InternalBotConversationDataProvider(),
1012
userDataProvider: InternalUserDataProvider(),
1113
supportConversationDataProvider: InternalSupportConversationDataProvider(),
12-
diagnosticsDataProvider: InternalDiagnosticsDataProvider()
14+
diagnosticsDataProvider: InternalDiagnosticsDataProvider(),
15+
mediaHost: InternalMediaHost()
1316
)
1417

1518
static let applicationLog = ApplicationLog(path: URL(filePath: #filePath), createdAt: Date(), modifiedAt: Date())
@@ -434,3 +437,13 @@ actor InternalDiagnosticsDataProvider: DiagnosticsDataProvider {
434437
}
435438
}
436439
}
440+
441+
actor InternalMediaHost: MediaHostProtocol {
442+
func authenticatedRequest(for url: URL) async throws -> URLRequest {
443+
URLRequest(url: url)
444+
}
445+
446+
func authenticatedAsset(for url: URL) async throws -> AVURLAsset {
447+
AVURLAsset(url: url)
448+
}
449+
}

Modules/Sources/Support/SupportDataProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import AsyncImageKit
23
import WordPressCoreProtocols
34

45
public enum SupportFormAction {
@@ -33,6 +34,7 @@ public final class SupportDataProvider: ObservableObject, Sendable {
3334
private let userDataProvider: CurrentUserDataProvider
3435
private let supportConversationDataProvider: SupportConversationDataProvider
3536
private let diagnosticsDataProvider: DiagnosticsDataProvider
37+
let mediaHost: MediaHostProtocol
3638

3739
private weak var supportDelegate: SupportDelegate?
3840

@@ -42,13 +44,15 @@ public final class SupportDataProvider: ObservableObject, Sendable {
4244
userDataProvider: CurrentUserDataProvider,
4345
supportConversationDataProvider: SupportConversationDataProvider,
4446
diagnosticsDataProvider: DiagnosticsDataProvider,
47+
mediaHost: MediaHostProtocol,
4548
delegate: SupportDelegate? = nil
4649
) {
4750
self.applicationLogProvider = applicationLogProvider
4851
self.botConversationDataProvider = botConversationDataProvider
4952
self.userDataProvider = userDataProvider
5053
self.supportConversationDataProvider = supportConversationDataProvider
5154
self.diagnosticsDataProvider = diagnosticsDataProvider
55+
self.mediaHost = mediaHost
5256
self.supportDelegate = delegate
5357
}
5458

Modules/Sources/Support/UI/Support Conversations/AttachmentListView.swift

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,41 @@ struct SingleImageView: View {
3131
}
3232

3333
struct SingleVideoView: View {
34-
private let player: AVPlayer
3534

36-
init(url: URL) {
37-
self.player = AVPlayer(url: url)
35+
@State
36+
private var player: AVPlayer? = nil
37+
38+
private let url: URL
39+
private let host: MediaHostProtocol?
40+
41+
init(url: URL, host: MediaHostProtocol? = nil) {
42+
self.url = url
43+
self.host = host
3844
}
3945

4046
var body: some View {
41-
VideoPlayer(player: player)
42-
.ignoresSafeArea()
43-
.onAppear {
44-
self.player.play()
47+
Group {
48+
if let player {
49+
VideoPlayer(player: player)
50+
.ignoresSafeArea()
51+
.onAppear {
52+
player.play()
53+
}
54+
} else {
55+
FullScreenProgressView("Loading Video")
56+
}
57+
}.task {
58+
if let host {
59+
do {
60+
let asset = try await host.authenticatedAsset(for: url)
61+
self.player = AVPlayer(playerItem: AVPlayerItem(asset: asset))
62+
} catch {
63+
debugPrint(error.localizedDescription)
64+
}
65+
} else {
66+
self.player = AVPlayer(url: url)
4567
}
68+
}
4669
}
4770
}
4871

@@ -98,6 +121,10 @@ struct AttachmentListView: View {
98121
}
99122

100123
struct AttachmentThumbnailView: View {
124+
125+
@EnvironmentObject
126+
private var supportDataProvider: SupportDataProvider
127+
101128
let attachment: Attachment
102129

103130
var body: some View {
@@ -107,37 +134,41 @@ struct AttachmentThumbnailView: View {
107134
}
108135

109136
if attachment.isVideo {
110-
SingleVideoView(url: attachment.url)
137+
SingleVideoView(url: attachment.url, host: supportDataProvider.mediaHost)
111138
}
112139
} label: {
113140
ZStack {
114141
if attachment.isImage {
115-
CachedAsyncImage(url: attachment.url) { image in
142+
CachedAsyncImage(url: attachment.url, host: supportDataProvider.mediaHost, mutability: .immutable) { image in
116143
image
117144
.resizable()
118145
.aspectRatio(contentMode: .fill)
119146
} placeholder: {
120147
Color.gray.opacity(0.2).overlay {
121148
ProgressView()
122149
}
150+
123151
}
124152
}
125153

126154
if attachment.isVideo {
127-
CachedAsyncVideoPreview(url: attachment.url) { image in
128-
image
129-
.resizable()
130-
.aspectRatio(contentMode: .fill)
131-
.overlay {
132-
Image(systemName: "play.circle")
133-
.foregroundStyle(Color.white)
155+
CachedAsyncImage(
156+
videoUrl: attachment.url,
157+
host: supportDataProvider.mediaHost,
158+
mutability: .immutable
159+
) { image in
160+
image
161+
.resizable()
162+
.aspectRatio(contentMode: .fill)
163+
.overlay {
164+
Image(systemName: "play.circle")
165+
.foregroundStyle(Color.white)
166+
}
167+
} placeholder: {
168+
Color.gray.opacity(0.2).overlay {
169+
ProgressView()
134170
}
135-
136-
} placeholder: {
137-
Color.gray.opacity(0.2).overlay {
138-
ProgressView()
139171
}
140-
}
141172
}
142173
}
143174
.frame(width: 80, height: 80)

WordPress/Classes/ViewRelated/NewSupport/SupportDataProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extension SupportDataProvider {
2525
wpcomClient: WordPressDotComClient()
2626
),
2727
diagnosticsDataProvider: WpDiagnosticsDataProvider(),
28+
mediaHost: WordPressDotComClient(),
2829
delegate: WpSupportDelegate()
2930
)
3031
}

0 commit comments

Comments
 (0)