Supports video and gifs + Dragging is fixed
This commit is contained in:
parent
e35565c246
commit
8ec5d75656
|
@ -1,6 +1,8 @@
|
|||
//Made by Lumaa
|
||||
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
import AVKit
|
||||
|
||||
struct AttachmentView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
@ -14,6 +16,8 @@ struct AttachmentView: View {
|
|||
return attachments.filter({ $0.id == selectedId })[0]
|
||||
}
|
||||
|
||||
@State private var player: AVPlayer?
|
||||
|
||||
@State private var readAlt: Bool = false
|
||||
@State private var hasSwitch: Bool = false
|
||||
|
||||
|
@ -34,6 +38,8 @@ struct AttachmentView: View {
|
|||
if !attachments.isEmpty {
|
||||
TabView(selection: $selectedId) {
|
||||
ForEach(attachments) { atchmnt in
|
||||
ZStack {
|
||||
if atchmnt.supportedType == .image {
|
||||
AsyncImage(url: atchmnt.url, content: { image in
|
||||
image
|
||||
.resizable()
|
||||
|
@ -53,6 +59,70 @@ struct AttachmentView: View {
|
|||
})
|
||||
.tag(atchmnt.id)
|
||||
.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)
|
||||
.scaleEffect(currentZoom + totalZoom)
|
||||
}
|
||||
|
@ -108,7 +178,10 @@ struct AttachmentView: View {
|
|||
DragGesture()
|
||||
.onChanged { gesture in
|
||||
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 {
|
||||
guard !hasSwitch && attachments.count > 1 else { return }
|
||||
if gesture.translation.width >= 40 || gesture.translation.width <= -40 {
|
||||
|
|
Loading…
Reference in New Issue