1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Navigation/ContentView.swift

162 lines
5.3 KiB
Swift
Raw Normal View History

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