yattee/Shared/Navigation/ContentView.swift

233 lines
7.4 KiB
Swift
Raw Normal View History

import AVFAudio
2021-09-25 08:18:22 +00:00
import Defaults
2022-02-16 21:10:57 +00:00
import MediaPlayer
import SDWebImage
import SDWebImagePINPlugin
import SDWebImageWebPCoder
2021-10-16 22:48:58 +00:00
import Siesta
2021-06-09 20:51:23 +00:00
import SwiftUI
struct ContentView: View {
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<CommentsModel> private var comments
@EnvironmentObject<InstancesModel> private var instances
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
2022-02-16 20:23:11 +00:00
@EnvironmentObject<PlayerControlsModel> private var playerControls
@EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var search
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@EnvironmentObject<ThumbnailsModel> private var thumbnailsModel
2021-09-25 08:18:22 +00:00
2021-11-08 23:14:28 +00:00
@EnvironmentObject<MenuModel> private var menu
2021-07-11 20:52:49 +00:00
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
2021-06-11 21:11:59 +00:00
2021-06-09 20:51:23 +00:00
var body: some View {
2021-11-30 22:58:11 +00:00
Group {
2021-07-11 20:52:49 +00:00
#if os(iOS)
if horizontalSizeClass == .compact {
AppTabNavigation()
} else {
AppSidebarNavigation()
2021-06-11 21:11:59 +00:00
}
2021-07-11 20:52:49 +00:00
#elseif os(macOS)
AppSidebarNavigation()
#elseif os(tvOS)
TVNavigationView()
#endif
2021-07-29 22:34:13 +00:00
}
2021-10-16 22:48:58 +00:00
.onAppear(perform: configure)
.onChange(of: accounts.signedIn) { _ in
subscriptions.load(force: true)
playlists.load(force: true)
}
2021-10-24 09:16:04 +00:00
2021-10-16 22:48:58 +00:00
.environmentObject(accounts)
2021-12-04 19:35:41 +00:00
.environmentObject(comments)
.environmentObject(instances)
2021-09-25 08:18:22 +00:00
.environmentObject(navigation)
.environmentObject(player)
.environmentObject(playlists)
2021-09-25 08:18:22 +00:00
.environmentObject(recents)
.environmentObject(search)
.environmentObject(subscriptions)
2021-10-24 22:26:25 +00:00
.environmentObject(thumbnailsModel)
2021-11-30 22:58:11 +00:00
// iOS 14 has problem with multiple sheets in one view
// but it's ok when it's in background
.background(
EmptyView().sheet(isPresented: $navigation.presentingWelcomeScreen) {
WelcomeScreen()
.environmentObject(accounts)
.environmentObject(navigation)
}
)
#if !os(tvOS)
2021-12-24 19:20:05 +00:00
.onOpenURL { OpenURLHandler(accounts: accounts, player: player).handle($0) }
2021-11-30 22:58:11 +00:00
.background(
EmptyView().sheet(isPresented: $navigation.presentingAddToPlaylist) {
AddToPlaylistView(video: navigation.videoToAddToPlaylist)
.environmentObject(playlists)
}
)
.background(
EmptyView().sheet(isPresented: $navigation.presentingPlaylistForm) {
PlaylistFormView(playlist: $navigation.editedPlaylist)
.environmentObject(accounts)
.environmentObject(playlists)
}
)
.background(
EmptyView().sheet(isPresented: $navigation.presentingSettings, onDismiss: openWelcomeScreenIfAccountEmpty) {
SettingsView()
.environmentObject(accounts)
.environmentObject(instances)
.environmentObject(player)
2021-11-30 22:58:11 +00:00
}
)
2021-08-01 23:01:24 +00:00
#endif
.alert(isPresented: $navigation.presentingUnsubscribeAlert) {
Alert(
title: Text(
"Are you sure you want to unsubscribe from \(navigation.channelToUnsubscribe.name)?"
),
primaryButton: .destructive(Text("Unsubscribe")) {
subscriptions.unsubscribe(navigation.channelToUnsubscribe.id)
},
secondaryButton: .cancel()
)
}
2021-07-07 22:39:18 +00:00
}
2021-10-16 22:48:58 +00:00
func configure() {
SiestaLog.Category.enabled = .common
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
2021-12-01 22:54:58 +00:00
SDWebImageManager.defaultImageCache = PINCache(name: "stream.yattee.app")
#if !os(macOS)
2022-02-16 21:10:57 +00:00
setupNowPlayingInfoCenter()
#endif
2021-10-16 22:48:58 +00:00
#if os(iOS)
if Defaults[.lockPortraitWhenBrowsing] {
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
}
#endif
if let account = accounts.lastUsed ??
instances.lastUsed?.anonymousAccount ??
2021-11-08 23:14:28 +00:00
InstancesModel.all.first?.anonymousAccount
2021-10-17 21:49:56 +00:00
{
accounts.setCurrent(account)
}
2021-10-17 23:06:00 +00:00
if accounts.current.isNil {
2021-10-17 23:06:00 +00:00
navigation.presentingWelcomeScreen = true
}
2021-10-16 22:48:58 +00:00
playlists.accounts = accounts
search.accounts = accounts
subscriptions.accounts = accounts
2021-12-04 19:35:41 +00:00
comments.player = player
2021-11-08 23:14:28 +00:00
menu.accounts = accounts
menu.navigation = navigation
menu.player = player
2022-02-16 20:23:11 +00:00
playerControls.player = player
2021-11-08 23:14:28 +00:00
2021-12-04 19:35:41 +00:00
player.accounts = accounts
player.comments = comments
2022-02-16 20:23:11 +00:00
player.controls = playerControls
2021-12-04 19:35:41 +00:00
2022-01-09 15:05:05 +00:00
if !accounts.current.isNil {
player.restoreQueue()
}
if !Defaults[.saveRecents] {
recents.clear()
}
var section = Defaults[.visibleSections].min()?.tabSelection
#if os(macOS)
if section == .playlists {
section = .search
}
#endif
navigation.tabSelection = section ?? .search
subscriptions.load()
playlists.load()
}
2021-10-17 23:06:00 +00:00
2022-02-16 21:10:57 +00:00
func setupNowPlayingInfoCenter() {
2022-02-27 20:31:17 +00:00
#if !os(macOS)
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
2022-02-16 21:10:57 +00:00
2022-02-27 20:31:17 +00:00
UIApplication.shared.beginReceivingRemoteControlEvents()
#endif
2022-02-16 21:10:57 +00:00
MPRemoteCommandCenter.shared().playCommand.addTarget { _ in
player.play()
return .success
}
MPRemoteCommandCenter.shared().pauseCommand.addTarget { _ in
player.pause()
return .success
}
MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget { remoteEvent in
guard let event = remoteEvent as? MPChangePlaybackPositionCommandEvent
else {
return .commandFailed
}
player.backend.seek(to: event.positionTime)
return .success
}
let skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand
skipForwardCommand.isEnabled = true
skipForwardCommand.preferredIntervals = [10]
skipForwardCommand.addTarget { _ in
player.backend.seek(relative: .secondsInDefaultTimescale(10))
return .success
}
let skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand
skipBackwardCommand.isEnabled = true
skipBackwardCommand.preferredIntervals = [10]
skipBackwardCommand.addTarget { _ in
player.backend.seek(relative: .secondsInDefaultTimescale(-10))
return .success
}
}
2021-10-17 23:06:00 +00:00
func openWelcomeScreenIfAccountEmpty() {
guard Defaults[.instances].isEmpty else {
2021-10-17 23:06:00 +00:00
return
}
navigation.presentingWelcomeScreen = true
}
2021-06-09 20:51:23 +00:00
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
2021-09-29 11:45:00 +00:00
.injectFixtureEnvironmentObjects()
2021-06-09 20:51:23 +00:00
}
}