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 enum SupportedType: String {
case image, gifv
}
public let id: String
public let type: String
public var supportedType: SupportedType? {
SupportedType(rawValue: type)
}
public let url: URL
public let previewUrl: URL
public let description: String?

View file

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