This commit is contained in:
Trey t
2023-06-28 23:41:46 -05:00
parent 71dedca94e
commit 939ea16716
3 changed files with 68 additions and 25 deletions

View File

@@ -24,33 +24,69 @@ struct VideoPlayerView: View {
.frame(maxWidth: .infinity)
.background(Color(uiColor: UIColor(red: 0.11, green: 0.11, blue: 0.12, alpha: 1)))
SafariWebView(url: $url)
VideoViewControllerView(url: $url)
}
.background(.black)
}
func updateVideoURL(url: URL) {
self.url = url
}
}
struct SafariWebView: UIViewControllerRepresentable {
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
func makeUIViewController(context: Context) -> SFSafariViewController {
return SFSafariViewController(url: url)
init(videoURL: URL) {
self.videoURL = videoURL
super.init(nibName: nil, bundle: nil)
layoutView()
}
func updateUIViewController(_ uiViewController: SFSafariViewController, context: Context) {
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")!)
// }
//}
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")!)
}
}