I've been watching various tutorials and have managed to come up with the following code:
import SwiftUI
import AVKit
struct ContentView: View {
@State private var wolData = [Main]()
var body: some View {
NavigationView{List(wolData, id: \.id) { item in
NavigationLink(destination: MainDetail(json: item)) {
HStack() {
Text(item.name)
.font(.headline)
Text(item.date)
.font(.footnote)
if #available(iOS 15.0, *) {
AsyncImage(url: URL(string: item.thumbnail))
{ image in
image
.resizable()
.scaledToFill()
} placeholder: {
Color.purple.opacity(0.1)
}
.frame(width: 20, height: 20)
} else {
// Fallback on earlier versions
}
}
}
}.onAppear(perform: loadData)}
}
}
extension ContentView
{
func loadData() {
guard let url = URL(string: "https://wolvideos.firebaseapp.com/Simple.json") else {
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let response_obj = try? JSONDecoder().decode([Main].self, from: data) {
DispatchQueue.main.async {
self.wolData = response_obj
}
}
}
}.resume()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import Foundation
struct Main: Decodable {
var id: Int
var name: String
var interactive: String
var thumbnail: String
var date: String
var videolink: String
var sharelink: String
}
import SwiftUI
import AVKit
struct MainDetail: View {
var json: Main
private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
var body: some View {
VideoPlayer(player: player)
.onAppear() {
// Start the player going, otherwise controls don't appear
player.play()
}
.onDisappear() {
// Stop the player when the view disappears
player.pause()
}
}
}
Now that I've shared that, the intent is to when the user clicks an option load the corresponding video (from the json not the current test file) via HLS in a default player (like the one in safari)... should I be going about this in a different way? I'm starting to think committing to SwiftUI is a mistake.