2022-12-17 12:37:46 +00:00
|
|
|
import SwiftUI
|
|
|
|
import Models
|
2022-12-22 09:53:36 +00:00
|
|
|
import Env
|
2022-12-22 18:00:23 +00:00
|
|
|
import Shimmer
|
2022-12-25 06:43:02 +00:00
|
|
|
import NukeUI
|
2022-12-27 05:44:31 +00:00
|
|
|
import DesignSystem
|
2022-12-17 12:37:46 +00:00
|
|
|
|
|
|
|
public struct StatusMediaPreviewView: View {
|
2022-12-22 09:53:36 +00:00
|
|
|
@EnvironmentObject private var quickLook: QuickLook
|
2022-12-31 11:29:19 +00:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2022-12-22 09:53:36 +00:00
|
|
|
|
2022-12-17 12:37:46 +00:00
|
|
|
public let attachements: [MediaAttachement]
|
2022-12-29 16:22:07 +00:00
|
|
|
public let isCompact: Bool
|
2022-12-24 07:29:45 +00:00
|
|
|
|
2022-12-22 09:53:36 +00:00
|
|
|
@State private var isQuickLookLoading: Bool = false
|
2022-12-27 05:44:31 +00:00
|
|
|
@State private var width: CGFloat = 0
|
2022-12-24 07:01:35 +00:00
|
|
|
|
|
|
|
private var imageMaxHeight: CGFloat {
|
2022-12-29 16:22:07 +00:00
|
|
|
if isCompact {
|
|
|
|
return 50
|
|
|
|
}
|
2022-12-24 07:01:35 +00:00
|
|
|
if attachements.count == 1 {
|
|
|
|
return 300
|
|
|
|
}
|
|
|
|
return attachements.count > 2 ? 100 : 200
|
|
|
|
}
|
|
|
|
|
2022-12-27 05:44:31 +00:00
|
|
|
private func size(for media: MediaAttachement) -> CGSize? {
|
2022-12-29 16:22:07 +00:00
|
|
|
if isCompact {
|
|
|
|
return .init(width: 50, height: 50)
|
|
|
|
}
|
2022-12-27 05:44:31 +00:00
|
|
|
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 {
|
2022-12-29 16:22:07 +00:00
|
|
|
if isCompact {
|
|
|
|
return .init(width: 50, height: 50)
|
|
|
|
}
|
2022-12-27 05:44:31 +00:00
|
|
|
let ratio = newWidth / from.width
|
|
|
|
let newHeight = from.height * ratio
|
|
|
|
return .init(width: newWidth, height: newHeight)
|
|
|
|
}
|
|
|
|
|
2022-12-17 12:37:46 +00:00
|
|
|
public var body: some View {
|
2022-12-24 07:01:35 +00:00
|
|
|
Group {
|
|
|
|
if attachements.count == 1, let attachement = attachements.first {
|
|
|
|
makeFeaturedImagePreview(attachement: attachement)
|
|
|
|
.onTapGesture {
|
|
|
|
Task {
|
2022-12-27 15:16:25 +00:00
|
|
|
await quickLook.prepareFor(urls: attachements.compactMap{ $0.url }, selectedURL: attachement.url!)
|
2022-12-24 07:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-29 16:22:07 +00:00
|
|
|
if isCompact {
|
2022-12-24 07:01:35 +00:00
|
|
|
HStack {
|
2022-12-29 16:22:07 +00:00
|
|
|
makeAttachementView(for: 0)
|
|
|
|
makeAttachementView(for: 1)
|
|
|
|
makeAttachementView(for: 2)
|
|
|
|
makeAttachementView(for: 3)
|
2022-12-24 07:01:35 +00:00
|
|
|
}
|
2022-12-29 16:22:07 +00:00
|
|
|
} else {
|
|
|
|
VStack {
|
|
|
|
HStack {
|
|
|
|
makeAttachementView(for: 0)
|
|
|
|
makeAttachementView(for: 1)
|
2022-12-24 07:01:35 +00:00
|
|
|
}
|
2022-12-29 16:22:07 +00:00
|
|
|
HStack {
|
|
|
|
makeAttachementView(for: 2)
|
|
|
|
makeAttachementView(for: 3)
|
2022-12-24 07:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 12:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 09:53:36 +00:00
|
|
|
.overlay {
|
|
|
|
if quickLook.isPreparing {
|
2022-12-24 07:01:35 +00:00
|
|
|
quickLookLoadingView
|
2022-12-22 09:53:36 +00:00
|
|
|
.transition(.opacity)
|
|
|
|
}
|
2022-12-19 16:18:16 +00:00
|
|
|
}
|
2022-12-24 07:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 16:22:07 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private func makeAttachementView(for index: Int) -> some View {
|
|
|
|
if attachements.count > index {
|
|
|
|
makePreview(attachement: attachements[index])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 07:01:35 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private func makeFeaturedImagePreview(attachement: MediaAttachement) -> some View {
|
|
|
|
switch attachement.supportedType {
|
|
|
|
case .image:
|
2022-12-27 05:44:31 +00:00
|
|
|
if let size = size(for: attachement) {
|
2023-01-03 06:41:29 +00:00
|
|
|
let avatarColumnWidth = theme.avatarPosition == .leading ? AvatarView.Size.status.size.width + .statusColumnsSpacing : 0
|
|
|
|
let availableWidth = UIScreen.main.bounds.width - (.layoutPadding * 2) - avatarColumnWidth
|
2022-12-27 05:44:31 +00:00
|
|
|
let newSize = imageSize(from: size,
|
2022-12-31 11:29:19 +00:00
|
|
|
newWidth: availableWidth)
|
2022-12-27 05:44:31 +00:00
|
|
|
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()
|
|
|
|
}
|
2022-12-24 07:01:35 +00:00
|
|
|
}
|
2022-12-27 05:44:31 +00:00
|
|
|
} 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()
|
|
|
|
})
|
|
|
|
}
|
2022-12-30 17:40:10 +00:00
|
|
|
case .gifv, .video:
|
2022-12-27 15:16:25 +00:00
|
|
|
if let url = attachement.url {
|
|
|
|
VideoPlayerView(viewModel: .init(url: url))
|
|
|
|
.frame(height: imageMaxHeight)
|
|
|
|
}
|
2022-12-24 07:01:35 +00:00
|
|
|
case .none:
|
|
|
|
EmptyView()
|
|
|
|
}
|
2022-12-19 16:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func makePreview(attachement: MediaAttachement) -> some View {
|
|
|
|
if let type = attachement.supportedType {
|
2022-12-20 07:14:57 +00:00
|
|
|
Group {
|
|
|
|
GeometryReader { proxy in
|
|
|
|
switch type {
|
|
|
|
case .image:
|
2022-12-25 06:43:02 +00:00
|
|
|
LazyImage(url: attachement.url) { state in
|
|
|
|
if let image = state.image {
|
2022-12-19 16:18:16 +00:00
|
|
|
image
|
2022-12-25 06:43:02 +00:00
|
|
|
.resizingMode(.aspectFill)
|
2022-12-24 07:01:35 +00:00
|
|
|
.cornerRadius(4)
|
2022-12-25 06:43:02 +00:00
|
|
|
} else if state.isLoading {
|
2022-12-22 18:00:23 +00:00
|
|
|
RoundedRectangle(cornerRadius: 4)
|
|
|
|
.fill(Color.gray)
|
2022-12-24 07:01:35 +00:00
|
|
|
.frame(maxHeight: imageMaxHeight)
|
2022-12-29 16:22:07 +00:00
|
|
|
.frame(width: isCompact ? imageMaxHeight : proxy.frame(in: .local).width)
|
2022-12-22 18:00:23 +00:00
|
|
|
.shimmering()
|
2022-12-19 16:18:16 +00:00
|
|
|
}
|
2022-12-25 06:43:02 +00:00
|
|
|
}
|
2022-12-29 16:22:07 +00:00
|
|
|
.frame(width: isCompact ? imageMaxHeight : proxy.frame(in: .local).width)
|
2022-12-25 06:43:02 +00:00
|
|
|
.frame(height: imageMaxHeight)
|
2022-12-30 17:40:10 +00:00
|
|
|
case .gifv, .video:
|
2022-12-27 15:16:25 +00:00
|
|
|
if let url = attachement.url {
|
|
|
|
VideoPlayerView(viewModel: .init(url: url))
|
2022-12-29 16:22:07 +00:00
|
|
|
.frame(width: isCompact ? imageMaxHeight : proxy.frame(in: .local).width)
|
2022-12-27 15:16:25 +00:00
|
|
|
.frame(height: imageMaxHeight)
|
|
|
|
}
|
2022-12-19 16:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-29 16:22:07 +00:00
|
|
|
.frame(width: isCompact ? imageMaxHeight : nil)
|
2022-12-24 07:01:35 +00:00
|
|
|
.frame(height: imageMaxHeight)
|
2022-12-20 07:14:57 +00:00
|
|
|
}
|
|
|
|
.onTapGesture {
|
2022-12-22 09:53:36 +00:00
|
|
|
Task {
|
2022-12-27 15:16:25 +00:00
|
|
|
await quickLook.prepareFor(urls: attachements.compactMap{ $0.url }, selectedURL: attachement.url!)
|
2022-12-22 09:53:36 +00:00
|
|
|
}
|
2022-12-17 12:37:46 +00:00
|
|
|
}
|
2022-12-19 15:01:23 +00:00
|
|
|
}
|
2022-12-17 12:37:46 +00:00
|
|
|
}
|
2022-12-19 18:04:07 +00:00
|
|
|
|
2022-12-22 09:53:36 +00:00
|
|
|
private var quickLookLoadingView: some View {
|
|
|
|
ZStack(alignment: .center) {
|
|
|
|
VStack {
|
|
|
|
Spacer()
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
ProgressView()
|
|
|
|
Spacer()
|
2022-12-19 18:04:07 +00:00
|
|
|
}
|
2022-12-22 09:53:36 +00:00
|
|
|
Spacer()
|
2022-12-19 18:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 09:53:36 +00:00
|
|
|
.background(.ultraThinMaterial)
|
2022-12-19 18:04:07 +00:00
|
|
|
}
|
2022-12-17 12:37:46 +00:00
|
|
|
}
|