// // ContentView.swift // vrtnu // // Created by Jens Timmerman on 23/09/2020. // import SwiftUI import AVKit import AVFoundation import Combine import TVUIKit struct ContentView: View { @ViewBuilder var body: some View { VRTNuView(vrtNu: VRTNu()) } } struct LoginView: View{ var vrtNu: VRTNu @State private var password: String = "" @State private var username: String = "" @State var authenticationDidFail: Bool = false // needed to update parent view when login was succesful @Binding var loginaction: Bool var body: some View { if authenticationDidFail { Text("Information not correct. Try again.") .offset(y: -10) .foregroundColor(.red) } TextField("VRTNu username", text: $username) SecureField("VRTNu password", text: $password) Button("Inloggen", action: { let loginok = vrtNu.login(username: username, password: password) if loginok{ // reload parent view here withAnimation { self.loginaction.toggle() } }else{ authenticationDidFail = true } }) } } struct VRTNuView: View{ var vrtNu: VRTNu var body: some View { NavigationView(){ List(vrtNu.getShows(), id: \.self){ show in NavigationLink(destination: ShowView(show: show)){ HStack{ AsyncImage(url: show.imageURL,placeholder: { //Image(name: "loading") Text("Loading...") }, image:{Image(uiImage:$0).resizable()}).frame(width: 300, height:300) Text(show.title) } } } } } } struct ShowView: View { var show: Show var body: some View { NavigationView(){ List{ ForEach(show.getSeasons(), id: \.self){ season in Section(header: Text(season.seasonName).font(.largeTitle)) { ForEach(season.getEpisodes(), id:\.self){ episode in NavigationLink(destination: VideoView(episode: episode)){ HStack{ AsyncImage(url: episode.imageURL,placeholder: { //Image(name: "loading") Text("Loading...") },image:{Image(uiImage:$0).resizable()}).frame(width: 300, height: 300) Text(episode.name) } } } }} } } } } struct VisibleShowView: View { var show: Show var body: some View { NavigationView(){ List(show.getSeasons(), id: \.self){ season in NavigationLink(destination: SeasonView(season: season)){ Text(season.seasonName) } } } } } struct SeasonView: View { var season: Season var body: some View { NavigationView(){ List(season.getEpisodes(), id:\.self){ episode in NavigationLink(destination: VideoView(episode: episode)){ HStack{ AsyncImage(url: episode.imageURL,placeholder: { //Image(name: "loading") Text("Loading...") },image:{Image(uiImage:$0).resizable()}).frame(width: 300, height: 300) Text(episode.name) } } } } } } struct VideoView: View { var episode: Episode @State var loginaction: Bool = false var body: some View { if episode.authenticated() || loginaction { let player = AVPlayer(url: episode.getVideo().hlsUrl); VideoPlayer(player: player).fixedSize().onAppear(){ player.play() } }else{ LoginView(vrtNu: episode.season.show.vrtNu, loginaction: $loginaction) } }; } struct ContentView_Previews: PreviewProvider { static var previews: some View { SeasonView(season: VRTNu().getShows()[0].getSeasons()[0]) //ContentView() } } /*struct OldContentView: View { var body: some View { NavigationView(){ VStack(alignment: .leading) { ForEach(VRTNu().shows, id: \.self){ show in VStack{ Text(show.title) HStack{ ForEach(show.seasons, id:\.self){ season in List(season.episodes, id:\.self){ episode in NavigationLink(destination: VideoView(url: episode.video.hlsUrl)){ VStack{ Text(season.seasonName) Text(episode.name) AsyncImage(url: episode.imageurl,placeholder: { //Image(name: "loading") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") Text("Loading...") },image:{Image(uiImage:$0).resizable()}) }} } } } } } } } } }*/