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