2021-10-24 16:01:36 +02:00
|
|
|
import AVFAudio
|
2021-09-25 10:18:22 +02:00
|
|
|
import Defaults
|
2021-10-22 01:29:10 +02:00
|
|
|
import SDWebImage
|
|
|
|
import SDWebImagePINPlugin
|
|
|
|
import SDWebImageWebPCoder
|
2021-10-17 00:48:58 +02:00
|
|
|
import Siesta
|
2021-06-09 22:51:23 +02:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ContentView: View {
|
2021-10-17 00:48:58 +02:00
|
|
|
@StateObject private var accounts = AccountsModel()
|
2021-10-05 22:20:09 +02:00
|
|
|
@StateObject private var instances = InstancesModel()
|
2021-09-25 10:18:22 +02:00
|
|
|
@StateObject private var navigation = NavigationModel()
|
2021-10-05 22:20:09 +02:00
|
|
|
@StateObject private var player = PlayerModel()
|
|
|
|
@StateObject private var playlists = PlaylistsModel()
|
2021-09-25 14:17:58 +02:00
|
|
|
@StateObject private var recents = RecentsModel()
|
2021-10-05 22:20:09 +02:00
|
|
|
@StateObject private var search = SearchModel()
|
|
|
|
@StateObject private var subscriptions = SubscriptionsModel()
|
2021-10-25 00:26:25 +02:00
|
|
|
@StateObject private var thumbnailsModel = ThumbnailsModel()
|
2021-09-25 10:18:22 +02:00
|
|
|
|
2021-07-11 22:52:49 +02:00
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
|
|
|
#endif
|
2021-06-11 23:11:59 +02:00
|
|
|
|
2021-06-09 22:51:23 +02:00
|
|
|
var body: some View {
|
2021-07-11 22:52:49 +02:00
|
|
|
Section {
|
|
|
|
#if os(iOS)
|
|
|
|
if horizontalSizeClass == .compact {
|
|
|
|
AppTabNavigation()
|
|
|
|
} else {
|
|
|
|
AppSidebarNavigation()
|
2021-06-11 23:11:59 +02:00
|
|
|
}
|
2021-07-11 22:52:49 +02:00
|
|
|
#elseif os(macOS)
|
|
|
|
AppSidebarNavigation()
|
|
|
|
#elseif os(tvOS)
|
|
|
|
TVNavigationView()
|
|
|
|
#endif
|
2021-07-30 00:34:13 +02:00
|
|
|
}
|
2021-10-17 00:48:58 +02:00
|
|
|
.onAppear(perform: configure)
|
2021-10-24 11:16:04 +02:00
|
|
|
|
2021-10-17 00:48:58 +02:00
|
|
|
.environmentObject(accounts)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(instances)
|
2021-09-25 10:18:22 +02:00
|
|
|
.environmentObject(navigation)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(player)
|
|
|
|
.environmentObject(playlists)
|
2021-09-25 10:18:22 +02:00
|
|
|
.environmentObject(recents)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(search)
|
|
|
|
.environmentObject(subscriptions)
|
2021-10-25 00:26:25 +02:00
|
|
|
.environmentObject(thumbnailsModel)
|
|
|
|
|
2021-10-18 01:06:00 +02:00
|
|
|
.sheet(isPresented: $navigation.presentingWelcomeScreen) {
|
|
|
|
WelcomeScreen()
|
|
|
|
.environmentObject(accounts)
|
|
|
|
.environmentObject(navigation)
|
|
|
|
}
|
2021-10-05 22:20:09 +02:00
|
|
|
#if os(iOS)
|
|
|
|
.fullScreenCover(isPresented: $player.presentingPlayer) {
|
|
|
|
VideoPlayerView()
|
2021-10-17 00:48:58 +02:00
|
|
|
.environmentObject(accounts)
|
2021-10-14 00:10:29 +02:00
|
|
|
.environmentObject(instances)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(navigation)
|
|
|
|
.environmentObject(player)
|
2021-10-25 23:29:06 +02:00
|
|
|
.environmentObject(playlists)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(subscriptions)
|
2021-10-25 00:26:25 +02:00
|
|
|
.environmentObject(thumbnailsModel)
|
2021-08-02 01:01:24 +02:00
|
|
|
}
|
2021-10-05 22:20:09 +02:00
|
|
|
#elseif os(macOS)
|
|
|
|
.sheet(isPresented: $player.presentingPlayer) {
|
|
|
|
VideoPlayerView()
|
|
|
|
.frame(minWidth: 900, minHeight: 800)
|
2021-10-17 00:48:58 +02:00
|
|
|
.environmentObject(accounts)
|
2021-10-14 00:10:29 +02:00
|
|
|
.environmentObject(instances)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(navigation)
|
|
|
|
.environmentObject(player)
|
2021-10-25 23:29:06 +02:00
|
|
|
.environmentObject(playlists)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(subscriptions)
|
2021-10-25 00:26:25 +02:00
|
|
|
.environmentObject(thumbnailsModel)
|
2021-10-05 22:20:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if !os(tvOS)
|
2021-10-24 14:31:10 +02:00
|
|
|
.handlesExternalEvents(preferring: Set(["*"]), allowing: Set(["*"]))
|
|
|
|
.onOpenURL(perform: handleOpenedURL)
|
2021-09-28 20:06:05 +02:00
|
|
|
.sheet(isPresented: $navigation.presentingAddToPlaylist) {
|
|
|
|
AddToPlaylistView(video: navigation.videoToAddToPlaylist)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(playlists)
|
2021-09-28 20:06:05 +02:00
|
|
|
}
|
2021-09-25 10:18:22 +02:00
|
|
|
.sheet(isPresented: $navigation.presentingPlaylistForm) {
|
|
|
|
PlaylistFormView(playlist: $navigation.editedPlaylist)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(playlists)
|
2021-09-25 10:18:22 +02:00
|
|
|
}
|
2021-10-18 01:06:00 +02:00
|
|
|
.sheet(isPresented: $navigation.presentingSettings, onDismiss: openWelcomeScreenIfAccountEmpty) {
|
2021-09-25 10:18:22 +02:00
|
|
|
SettingsView()
|
2021-10-18 01:06:00 +02:00
|
|
|
.environmentObject(accounts)
|
2021-10-05 22:20:09 +02:00
|
|
|
.environmentObject(instances)
|
2021-08-29 23:36:18 +02:00
|
|
|
}
|
2021-08-02 01:01:24 +02:00
|
|
|
#endif
|
2021-07-08 00:39:18 +02:00
|
|
|
}
|
2021-10-05 22:20:09 +02:00
|
|
|
|
2021-10-17 00:48:58 +02:00
|
|
|
func configure() {
|
|
|
|
SiestaLog.Category.enabled = .common
|
2021-10-22 01:29:10 +02:00
|
|
|
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
|
|
|
SDWebImageManager.defaultImageCache = PINCache(name: "net.yattee.app")
|
2021-10-24 16:01:36 +02:00
|
|
|
#if !os(macOS)
|
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
|
|
|
|
#endif
|
2021-10-17 00:48:58 +02:00
|
|
|
|
2021-10-19 23:27:04 +02:00
|
|
|
if let account = accounts.lastUsed ??
|
|
|
|
instances.lastUsed?.anonymousAccount ??
|
|
|
|
instances.all.first?.anonymousAccount
|
2021-10-17 23:49:56 +02:00
|
|
|
{
|
2021-10-19 23:27:04 +02:00
|
|
|
accounts.setCurrent(account)
|
2021-10-05 22:20:09 +02:00
|
|
|
}
|
2021-10-18 01:06:00 +02:00
|
|
|
|
2021-10-19 23:27:04 +02:00
|
|
|
if accounts.current.isNil {
|
2021-10-18 01:06:00 +02:00
|
|
|
navigation.presentingWelcomeScreen = true
|
|
|
|
}
|
2021-10-19 23:27:04 +02:00
|
|
|
|
2021-10-17 00:48:58 +02:00
|
|
|
player.accounts = accounts
|
|
|
|
playlists.accounts = accounts
|
|
|
|
search.accounts = accounts
|
|
|
|
subscriptions.accounts = accounts
|
2021-10-24 20:01:08 +02:00
|
|
|
|
|
|
|
if !accounts.current.isNil {
|
|
|
|
player.loadHistoryDetails()
|
|
|
|
}
|
2021-10-05 22:20:09 +02:00
|
|
|
}
|
2021-10-18 01:06:00 +02:00
|
|
|
|
|
|
|
func openWelcomeScreenIfAccountEmpty() {
|
2021-10-19 23:27:04 +02:00
|
|
|
guard Defaults[.instances].isEmpty else {
|
2021-10-18 01:06:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
navigation.presentingWelcomeScreen = true
|
|
|
|
}
|
2021-10-24 11:16:04 +02:00
|
|
|
|
2021-10-24 14:31:10 +02:00
|
|
|
#if !os(tvOS)
|
|
|
|
func handleOpenedURL(_ url: URL) {
|
|
|
|
guard !accounts.current.isNil else {
|
|
|
|
return
|
|
|
|
}
|
2021-10-24 11:16:04 +02:00
|
|
|
|
2021-10-24 14:31:10 +02:00
|
|
|
let parser = VideoURLParser(url: url)
|
2021-10-24 11:16:04 +02:00
|
|
|
|
2021-10-24 14:31:10 +02:00
|
|
|
guard let id = parser.id else {
|
|
|
|
return
|
|
|
|
}
|
2021-10-24 11:16:04 +02:00
|
|
|
|
2021-10-24 14:31:10 +02:00
|
|
|
accounts.api.video(id).load().onSuccess { response in
|
|
|
|
if let video: Video = response.typedContent() {
|
2021-10-25 10:25:41 +02:00
|
|
|
player.addCurrentItemToHistory()
|
2021-10-24 14:31:10 +02:00
|
|
|
self.player.playNow(video, at: parser.time)
|
|
|
|
self.player.presentPlayer()
|
|
|
|
}
|
2021-10-24 11:16:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 14:31:10 +02:00
|
|
|
#endif
|
2021-06-09 22:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
ContentView()
|
2021-09-29 13:45:00 +02:00
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-06-09 22:51:23 +02:00
|
|
|
}
|
|
|
|
}
|