Supports video and gifs + Dragging is fixed
This commit is contained in:
parent
e35565c246
commit
8ec5d75656
|
@ -1,6 +1,8 @@
|
||||||
//Made by Lumaa
|
//Made by Lumaa
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import UIKit
|
||||||
|
import AVKit
|
||||||
|
|
||||||
struct AttachmentView: View {
|
struct AttachmentView: View {
|
||||||
@Environment(\.dismiss) private var dismiss
|
@Environment(\.dismiss) private var dismiss
|
||||||
|
@ -14,6 +16,8 @@ struct AttachmentView: View {
|
||||||
return attachments.filter({ $0.id == selectedId })[0]
|
return attachments.filter({ $0.id == selectedId })[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@State private var player: AVPlayer?
|
||||||
|
|
||||||
@State private var readAlt: Bool = false
|
@State private var readAlt: Bool = false
|
||||||
@State private var hasSwitch: Bool = false
|
@State private var hasSwitch: Bool = false
|
||||||
|
|
||||||
|
@ -34,25 +38,91 @@ struct AttachmentView: View {
|
||||||
if !attachments.isEmpty {
|
if !attachments.isEmpty {
|
||||||
TabView(selection: $selectedId) {
|
TabView(selection: $selectedId) {
|
||||||
ForEach(attachments) { atchmnt in
|
ForEach(attachments) { atchmnt in
|
||||||
AsyncImage(url: atchmnt.url, content: { image in
|
ZStack {
|
||||||
image
|
if atchmnt.supportedType == .image {
|
||||||
.resizable()
|
AsyncImage(url: atchmnt.url, content: { image in
|
||||||
.scaledToFit()
|
image
|
||||||
.frame(width: size.width)
|
.resizable()
|
||||||
|
.scaledToFit()
|
||||||
|
.frame(width: size.width)
|
||||||
|
.ignoresSafeArea()
|
||||||
|
}, placeholder: {
|
||||||
|
ZStack {
|
||||||
|
Rectangle()
|
||||||
|
.fill(Color.gray)
|
||||||
|
.frame(width: size.width - 10, height: size.width - 10)
|
||||||
|
|
||||||
|
ProgressView()
|
||||||
|
.progressViewStyle(.circular)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
.tag(atchmnt.id)
|
||||||
.ignoresSafeArea()
|
.ignoresSafeArea()
|
||||||
}, placeholder: {
|
} else if atchmnt.supportedType == .video {
|
||||||
ZStack {
|
ZStack {
|
||||||
Rectangle()
|
if player != nil {
|
||||||
.fill(Color.gray)
|
VideoPlayer(player: player)
|
||||||
.frame(width: size.width - 10, height: size.width - 10)
|
.scaledToFit()
|
||||||
|
.frame(width: size.width)
|
||||||
ProgressView()
|
.ignoresSafeArea()
|
||||||
.progressViewStyle(.circular)
|
} else {
|
||||||
|
Color.gray
|
||||||
|
|
||||||
|
ProgressView()
|
||||||
|
.progressViewStyle(.circular)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
if let url = atchmnt.url {
|
||||||
|
player = AVPlayer(url: url)
|
||||||
|
player?.preventsDisplaySleepDuringVideoPlayback = false
|
||||||
|
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
||||||
|
player?.isMuted = true
|
||||||
|
player?.play()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onDisappear() {
|
||||||
|
guard player != nil else { return }
|
||||||
|
player?.pause()
|
||||||
|
}
|
||||||
|
} else if atchmnt.supportedType == .gifv {
|
||||||
|
ZStack(alignment: .center) {
|
||||||
|
if player != nil {
|
||||||
|
VideoPlayer(player: player)
|
||||||
|
.scaledToFit()
|
||||||
|
.frame(width: size.width)
|
||||||
|
.ignoresSafeArea()
|
||||||
|
} else {
|
||||||
|
Color.gray
|
||||||
|
|
||||||
|
ProgressView()
|
||||||
|
.progressViewStyle(.circular)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
if let url = atchmnt.url {
|
||||||
|
player = AVPlayer(url: url)
|
||||||
|
player?.preventsDisplaySleepDuringVideoPlayback = false
|
||||||
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
|
||||||
.tag(atchmnt.id)
|
|
||||||
.ignoresSafeArea()
|
|
||||||
.offset(x: currentPos.width + totalPos.width, y: currentPos.height + totalPos.height)
|
.offset(x: currentPos.width + totalPos.width, y: currentPos.height + totalPos.height)
|
||||||
.scaleEffect(currentZoom + totalZoom)
|
.scaleEffect(currentZoom + totalZoom)
|
||||||
}
|
}
|
||||||
|
@ -108,7 +178,10 @@ struct AttachmentView: View {
|
||||||
DragGesture()
|
DragGesture()
|
||||||
.onChanged { gesture in
|
.onChanged { gesture in
|
||||||
if totalZoom > 1.1 {
|
if totalZoom > 1.1 {
|
||||||
currentPos = gesture.translation
|
var fixedGesture = gesture.translation
|
||||||
|
fixedGesture.width = fixedGesture.width / (self.currentZoom + self.totalZoom)
|
||||||
|
fixedGesture.height = fixedGesture.height / (self.currentZoom + self.totalZoom)
|
||||||
|
currentPos = fixedGesture
|
||||||
} else {
|
} else {
|
||||||
guard !hasSwitch && attachments.count > 1 else { return }
|
guard !hasSwitch && attachments.count > 1 else { return }
|
||||||
if gesture.translation.width >= 40 || gesture.translation.width <= -40 {
|
if gesture.translation.width >= 40 || gesture.translation.width <= -40 {
|
||||||
|
|
Loading…
Reference in New Issue