WIP Video support in attachement

This commit is contained in:
Thomas Ricouard 2022-12-19 16:01:23 +01:00
parent b47df4605d
commit ac1b739bf3
2 changed files with 34 additions and 17 deletions

View file

@ -10,8 +10,15 @@ public struct MediaAttachement: Codable, Identifiable {
public let y: Float? public let y: Float?
} }
public enum SupportedType: String {
case image, gifv
}
public let id: String public let id: String
public let type: String public let type: String
public var supportedType: SupportedType? {
SupportedType(rawValue: type)
}
public let url: URL public let url: URL
public let previewUrl: URL public let previewUrl: URL
public let description: String? public let description: String?

View file

@ -1,5 +1,6 @@
import SwiftUI import SwiftUI
import Models import Models
import AVKit
public struct StatusMediaPreviewView: View { public struct StatusMediaPreviewView: View {
public let attachements: [MediaAttachement] public let attachements: [MediaAttachement]
@ -8,37 +9,46 @@ public struct StatusMediaPreviewView: View {
VStack { VStack {
HStack { HStack {
if let firstAttachement = attachements.first { if let firstAttachement = attachements.first {
makePreviewImage(attachement: firstAttachement) makePreview(attachement: firstAttachement)
} }
if attachements.count > 1, let secondAttachement = attachements[1] { if attachements.count > 1, let secondAttachement = attachements[1] {
makePreviewImage(attachement: secondAttachement) makePreview(attachement: secondAttachement)
} }
} }
HStack { HStack {
if attachements.count > 2, let secondAttachement = attachements[2] { if attachements.count > 2, let secondAttachement = attachements[2] {
makePreviewImage(attachement: secondAttachement) makePreview(attachement: secondAttachement)
} }
if attachements.count > 3, let secondAttachement = attachements[3] { if attachements.count > 3, let secondAttachement = attachements[3] {
makePreviewImage(attachement: secondAttachement) makePreview(attachement: secondAttachement)
} }
} }
} }
} }
private func makePreviewImage(attachement: MediaAttachement) -> some View { @ViewBuilder
AsyncImage( private func makePreview(attachement: MediaAttachement) -> some View {
url: attachement.url, if let type = attachement.supportedType {
content: { image in switch type {
image.resizable() case .image:
.aspectRatio(contentMode: .fill) AsyncImage(
url: attachement.url,
content: { image in
image.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: attachements.count > 2 ? 100 : 200)
.clipped()
.cornerRadius(4)
},
placeholder: {
ProgressView()
.frame(maxWidth: 80, maxHeight: 80)
}
)
case .gifv:
VideoPlayer(player: AVPlayer(url: attachement.url))
.frame(maxHeight: attachements.count > 2 ? 100 : 200) .frame(maxHeight: attachements.count > 2 ? 100 : 200)
.clipped()
.cornerRadius(4)
},
placeholder: {
ProgressView()
.frame(maxWidth: 80, maxHeight: 80)
} }
) }
} }
} }