110 lines
3.0 KiB
Swift
110 lines
3.0 KiB
Swift
//
|
|
// ContentView.swift
|
|
// WekoutThotViewer
|
|
//
|
|
// Created by Trey Tartt on 6/18/24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import AVKit
|
|
import Combine
|
|
|
|
struct ContentView: View {
|
|
@State public var needsUpdating: Bool = true
|
|
@State var isUpdating = false
|
|
@ObservedObject var dataStore = DataStore.shared
|
|
@State var nsfwVideos: [NSFWVideo]?
|
|
@State private var showLoginView = false
|
|
@State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4") ?? URL(fileURLWithPath: "/dev/null"))
|
|
@State private var currentVideoURL: URL?
|
|
|
|
let videoEnded = NotificationCenter.default.publisher(for: NSNotification.Name.AVPlayerItemDidPlayToEndTime)
|
|
|
|
var body: some View {
|
|
VStack {
|
|
if isUpdating {
|
|
ProgressView()
|
|
.progressViewStyle(.circular)
|
|
} else {
|
|
PlayerView(player: $avPlayer)
|
|
.onAppear{
|
|
avPlayer.isMuted = true
|
|
avPlayer.play()
|
|
}
|
|
.onReceive(videoEnded){ _ in
|
|
playRandomVideo()
|
|
}
|
|
}
|
|
}
|
|
.onAppear(perform: {
|
|
maybeRefreshData()
|
|
})
|
|
.sheet(isPresented: $showLoginView) {
|
|
LoginView(completion: {
|
|
needsUpdating = true
|
|
maybeRefreshData()
|
|
})
|
|
.interactiveDismissDisabled()
|
|
}
|
|
.onDisappear {
|
|
avPlayer.pause()
|
|
}
|
|
}
|
|
|
|
func playRandomVideo() {
|
|
if let video = nsfwVideos?.randomElement() {
|
|
playVideo(url: video.videoFile)
|
|
}
|
|
}
|
|
|
|
func playVideo(url: String) {
|
|
guard let videoURL = URL(string: BaseURLs.currentBaseURL + url) else {
|
|
return
|
|
}
|
|
if currentVideoURL == videoURL {
|
|
avPlayer.seek(to: .zero)
|
|
avPlayer.isMuted = true
|
|
avPlayer.play()
|
|
return
|
|
}
|
|
|
|
currentVideoURL = videoURL
|
|
avPlayer = AVPlayer(url: videoURL)
|
|
avPlayer.isMuted = true
|
|
avPlayer.play()
|
|
}
|
|
|
|
func maybeRefreshData() {
|
|
guard UserStore.shared.token != nil else {
|
|
isUpdating = false
|
|
showLoginView = true
|
|
return
|
|
}
|
|
|
|
if UserStore.shared.plannedWorkouts.isEmpty {
|
|
UserStore.shared.fetchPlannedWorkouts()
|
|
}
|
|
|
|
if needsUpdating {
|
|
self.isUpdating = true
|
|
dataStore.fetchAllData(completion: {
|
|
DispatchQueue.main.async {
|
|
guard let allNSFWVideos = dataStore.allNSFWVideos else {
|
|
self.isUpdating = false
|
|
return
|
|
}
|
|
self.nsfwVideos = allNSFWVideos
|
|
self.isUpdating = false
|
|
self.needsUpdating = false
|
|
|
|
playRandomVideo()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ContentView()
|
|
}
|