mirror of
https://github.com/lumaa-dev/BubbleApp.git
synced 2025-02-09 00:18:41 +01:00
supports imaging
This commit is contained in:
parent
4bda91845a
commit
95c63952e1
@ -7,7 +7,10 @@ import AVKit
|
|||||||
struct PostAttachment: View {
|
struct PostAttachment: View {
|
||||||
@Environment(AppDelegate.self) private var appDelegate: AppDelegate
|
@Environment(AppDelegate.self) private var appDelegate: AppDelegate
|
||||||
var attachment: MediaAttachment
|
var attachment: MediaAttachment
|
||||||
|
|
||||||
var isFeatured: Bool = true
|
var isFeatured: Bool = true
|
||||||
|
var isImaging: Bool = false
|
||||||
|
|
||||||
@State private var player: AVPlayer?
|
@State private var player: AVPlayer?
|
||||||
|
|
||||||
var appLayoutWidth: CGFloat = 10
|
var appLayoutWidth: CGFloat = 10
|
||||||
@ -24,91 +27,128 @@ struct PostAttachment: View {
|
|||||||
let mediaSize: CGSize = size(for: attachment) ?? .init(width: imageMaxHeight, height: imageMaxHeight)
|
let mediaSize: CGSize = size(for: attachment) ?? .init(width: imageMaxHeight, height: imageMaxHeight)
|
||||||
let newSize = imageSize(from: mediaSize)
|
let newSize = imageSize(from: mediaSize)
|
||||||
|
|
||||||
GeometryReader { _ in
|
if !isImaging {
|
||||||
// Audio later because it's a lil harder
|
GeometryReader { _ in
|
||||||
if attachment.supportedType == .image {
|
// Audio later because it's a lil harder
|
||||||
if let url = attachment.url {
|
if attachment.supportedType == .image {
|
||||||
AsyncImage(url: url) { image in
|
if let url = attachment.url {
|
||||||
image
|
AsyncImage(url: url) { image in
|
||||||
.resizable()
|
image
|
||||||
.aspectRatio(contentMode: .fill)
|
.resizable()
|
||||||
.frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)
|
.aspectRatio(contentMode: .fill)
|
||||||
.overlay(
|
.frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)
|
||||||
RoundedRectangle(cornerRadius: 15)
|
.overlay(
|
||||||
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
RoundedRectangle(cornerRadius: 15)
|
||||||
)
|
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
||||||
} placeholder: {
|
)
|
||||||
ZStack(alignment: .center) {
|
} placeholder: {
|
||||||
|
ZStack(alignment: .center) {
|
||||||
|
Color.gray
|
||||||
|
|
||||||
|
ProgressView()
|
||||||
|
.progressViewStyle(.circular)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if attachment.supportedType == .gifv {
|
||||||
|
ZStack(alignment: .center) {
|
||||||
|
if player != nil {
|
||||||
|
NoControlsPlayerViewController(player: player!)
|
||||||
|
.overlay(
|
||||||
|
RoundedRectangle(cornerRadius: 15)
|
||||||
|
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
Color.gray
|
Color.gray
|
||||||
|
|
||||||
ProgressView()
|
ProgressView()
|
||||||
.progressViewStyle(.circular)
|
.progressViewStyle(.circular)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
.onAppear {
|
||||||
} else if attachment.supportedType == .gifv {
|
if let url = attachment.url {
|
||||||
ZStack(alignment: .center) {
|
player = AVPlayer(url: url)
|
||||||
if player != nil {
|
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
||||||
NoControlsPlayerViewController(player: player!)
|
player?.isMuted = true
|
||||||
.overlay(
|
player?.play()
|
||||||
RoundedRectangle(cornerRadius: 15)
|
|
||||||
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
guard let player else { return }
|
||||||
)
|
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in
|
||||||
} else {
|
Task { @MainActor in
|
||||||
Color.gray
|
player.seek(to: CMTime.zero)
|
||||||
|
player.play()
|
||||||
ProgressView()
|
}
|
||||||
.progressViewStyle(.circular)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
if let url = attachment.url {
|
|
||||||
player = AVPlayer(url: url)
|
|
||||||
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
|
||||||
player?.isMuted = true
|
|
||||||
player?.play()
|
|
||||||
|
|
||||||
guard let player else { return }
|
|
||||||
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in
|
|
||||||
Task { @MainActor in
|
|
||||||
player.seek(to: CMTime.zero)
|
|
||||||
player.play()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onDisappear() {
|
||||||
|
guard player != nil else { return }
|
||||||
|
player?.pause()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if attachment.supportedType == .video {
|
||||||
|
ZStack {
|
||||||
|
if player != nil {
|
||||||
|
VideoPlayer(player: player)
|
||||||
|
.overlay(
|
||||||
|
RoundedRectangle(cornerRadius: 15)
|
||||||
|
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Color.gray
|
||||||
|
|
||||||
|
ProgressView()
|
||||||
|
.progressViewStyle(.circular)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
if let url = attachment.url {
|
||||||
|
player = AVPlayer(url: url)
|
||||||
|
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
||||||
|
player?.isMuted = false
|
||||||
|
player?.play()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onDisappear() {
|
||||||
|
guard player != nil else { return }
|
||||||
|
player?.pause()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.onDisappear() {
|
}
|
||||||
guard player != nil else { return }
|
.frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)
|
||||||
player?.pause()
|
.clipped()
|
||||||
}
|
.clipShape(.rect(cornerRadius: 15))
|
||||||
|
.contentShape(Rectangle())
|
||||||
} else if attachment.supportedType == .video {
|
} else {
|
||||||
ZStack {
|
imaging
|
||||||
if player != nil {
|
}
|
||||||
VideoPlayer(player: player)
|
}
|
||||||
.overlay(
|
|
||||||
RoundedRectangle(cornerRadius: 15)
|
@ViewBuilder
|
||||||
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
var imaging: some View {
|
||||||
)
|
let mediaSize: CGSize = size(for: attachment) ?? .init(width: imageMaxHeight, height: imageMaxHeight)
|
||||||
} else {
|
let newSize = imageSize(from: mediaSize)
|
||||||
|
|
||||||
|
GeometryReader { _ in
|
||||||
|
// Audio later because it's a lil harder
|
||||||
|
if let url = attachment.previewUrl {
|
||||||
|
AsyncImage(url: url) { image in
|
||||||
|
image
|
||||||
|
.resizable()
|
||||||
|
.aspectRatio(contentMode: .fill)
|
||||||
|
.frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)
|
||||||
|
.overlay(
|
||||||
|
RoundedRectangle(cornerRadius: 15)
|
||||||
|
.stroke(.gray.opacity(0.3), lineWidth: 1)
|
||||||
|
)
|
||||||
|
} placeholder: {
|
||||||
|
ZStack(alignment: .center) {
|
||||||
Color.gray
|
Color.gray
|
||||||
|
|
||||||
ProgressView()
|
ProgressView()
|
||||||
.progressViewStyle(.circular)
|
.progressViewStyle(.circular)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
|
||||||
if let url = attachment.url {
|
|
||||||
player = AVPlayer(url: url)
|
|
||||||
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
|
||||||
player?.isMuted = false
|
|
||||||
player?.play()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onDisappear() {
|
|
||||||
guard player != nil else { return }
|
|
||||||
player?.pause()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)
|
.frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user