Use Nuke if possible for featured media in status

This commit is contained in:
Thomas Ricouard 2022-12-27 06:44:31 +01:00
parent 45eafeb119
commit f339692d3c

View file

@ -4,6 +4,7 @@ import Env
import Shimmer
import Nuke
import NukeUI
import DesignSystem
public struct StatusMediaPreviewView: View {
@EnvironmentObject private var quickLook: QuickLook
@ -11,6 +12,7 @@ public struct StatusMediaPreviewView: View {
public let attachements: [MediaAttachement]
@State private var isQuickLookLoading: Bool = false
@State private var width: CGFloat = 0
private var imageMaxHeight: CGFloat {
if attachements.count == 1 {
@ -19,6 +21,20 @@ public struct StatusMediaPreviewView: View {
return attachements.count > 2 ? 100 : 200
}
private func size(for media: MediaAttachement) -> CGSize? {
if let width = media.meta?.original.width,
let height = media.meta?.original.height {
return .init(width: CGFloat(width), height: CGFloat(height))
}
return nil
}
private func imageSize(from: CGSize, newWidth: CGFloat) -> CGSize {
let ratio = newWidth / from.width
let newHeight = from.height * ratio
return .init(width: newWidth, height: newHeight)
}
public var body: some View {
Group {
if attachements.count == 1, let attachement = attachements.first {
@ -61,21 +77,38 @@ public struct StatusMediaPreviewView: View {
private func makeFeaturedImagePreview(attachement: MediaAttachement) -> some View {
switch attachement.supportedType {
case .image:
AsyncImage(
url: attachement.url,
content: { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
.cornerRadius(4)
},
placeholder: {
RoundedRectangle(cornerRadius: 4)
.fill(Color.gray)
.frame(height: imageMaxHeight)
.shimmering()
if let size = size(for: attachement) {
let newSize = imageSize(from: size,
newWidth: UIScreen.main.bounds.width - (DS.Constants.layoutPadding * 2))
LazyImage(url: attachement.url) { state in
if let image = state.image {
image
.resizingMode(.aspectFill)
.cornerRadius(4)
.frame(width: newSize.width, height: newSize.height)
} else {
RoundedRectangle(cornerRadius: 4)
.fill(Color.gray)
.frame(width: newSize.width, height: newSize.height)
.shimmering()
}
}
)
} else {
AsyncImage(
url: attachement.url,
content: { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
.cornerRadius(4)
},
placeholder: {
RoundedRectangle(cornerRadius: 4)
.fill(Color.gray)
.frame(height: imageMaxHeight)
.shimmering()
})
}
case .gifv:
VideoPlayerView(viewModel: .init(url: attachement.url))
.frame(height: imageMaxHeight)