93 lines
2.6 KiB
Swift
93 lines
2.6 KiB
Swift
//
|
|
// VideoPlayerView.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/27/23.
|
|
//
|
|
|
|
import SwiftUI
|
|
import AVKit
|
|
import SafariServices
|
|
|
|
struct VideoPlayerView: View {
|
|
@State var url: URL
|
|
@Environment(\.dismiss) var dismiss
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Button(action: {
|
|
dismiss()
|
|
}, label: {
|
|
Text("Done")
|
|
})
|
|
.padding()
|
|
.frame(maxWidth: .infinity)
|
|
.background(Color(uiColor: UIColor(red: 0.11, green: 0.11, blue: 0.12, alpha: 1)))
|
|
|
|
VideoViewControllerView(url: $url)
|
|
}
|
|
.background(.black)
|
|
}
|
|
}
|
|
|
|
struct VideoViewControllerView: UIViewControllerRepresentable {
|
|
@Binding var url: URL
|
|
|
|
func makeUIViewController(context: Context) -> VideoViewController {
|
|
return VideoViewController(videoURL: url)
|
|
}
|
|
|
|
func updateUIViewController(_ uiViewController: VideoViewController, context: Context) {
|
|
if url != uiViewController.videoURL {
|
|
uiViewController.videoURL = url
|
|
uiViewController.layoutView()
|
|
}
|
|
}
|
|
}
|
|
|
|
class VideoViewController: UIViewController {
|
|
var videoURL: URL
|
|
|
|
init(videoURL: URL) {
|
|
self.videoURL = videoURL
|
|
super.init(nibName: nil, bundle: nil)
|
|
layoutView()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
}
|
|
|
|
func layoutView() {
|
|
self.view.subviews.forEach({
|
|
$0.removeFromSuperview()
|
|
})
|
|
|
|
let sfVC = SFSafariViewController(url: self.videoURL)
|
|
sfVC.view.translatesAutoresizingMaskIntoConstraints = false
|
|
view.backgroundColor = .green
|
|
self.addChild(sfVC)
|
|
sfVC.didMove(toParent: self)
|
|
sfVC.view.backgroundColor = .magenta
|
|
sfVC.view.frame = self.view.bounds;
|
|
self.view.addSubview(sfVC.view)
|
|
sfVC.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
|
sfVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
|
sfVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
|
|
sfVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
|
|
}
|
|
}
|
|
|
|
//struct VideoPlayerView_Previews: PreviewProvider {
|
|
// static let exercise = PreviewData.parseExercises().first!
|
|
//
|
|
// static var previews: some View {
|
|
// VideoPlayerView(url: Bundle.main.url(forResource: "Straight_Leg_Sit_Up", withExtension: "mp4")!)
|
|
// }
|
|
//}
|
|
|