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,6 +38,8 @@ struct AttachmentView: View {
|
||||||
if !attachments.isEmpty {
|
if !attachments.isEmpty {
|
||||||
TabView(selection: $selectedId) {
|
TabView(selection: $selectedId) {
|
||||||
ForEach(attachments) { atchmnt in
|
ForEach(attachments) { atchmnt in
|
||||||
|
ZStack {
|
||||||
|
if atchmnt.supportedType == .image {
|
||||||
AsyncImage(url: atchmnt.url, content: { image in
|
AsyncImage(url: atchmnt.url, content: { image in
|
||||||
image
|
image
|
||||||
.resizable()
|
.resizable()
|
||||||
|
@ -53,6 +59,70 @@ struct AttachmentView: View {
|
||||||
})
|
})
|
||||||
.tag(atchmnt.id)
|
.tag(atchmnt.id)
|
||||||
.ignoresSafeArea()
|
.ignoresSafeArea()
|
||||||
|
} else if atchmnt.supportedType == .video {
|
||||||
|
ZStack {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
.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