2021-10-24 19:31:36 +05:30
|
|
|
import AVFAudio
|
2021-09-25 13:48:22 +05:30
|
|
|
import Defaults
|
2022-02-17 02:40:57 +05:30
|
|
|
import MediaPlayer
|
2021-10-22 04:59:10 +05:30
|
|
|
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-12-19 22:47:04 +05:30
|
|
|
@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-17 01:53:11 +05:30
|
|
|
@EnvironmentObject<PlayerControlsModel> private var playerControls
|
2021-12-19 22:47:04 +05:30
|
|
|
@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 13:48:22 +05:30
|
|
|
|
2021-11-09 04:44:28 +05:30
|
|
|
@EnvironmentObject<MenuModel> private var menu
|
|
|
|
|
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-12-01 04:28:11 +05:30
|
|
|
Group {
|
2021-07-12 02:22:49 +05:30
|
|
|
#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-12-20 03:57:20 +05:30
|
|
|
.onChange(of: accounts.signedIn) { _ in
|
|
|
|
subscriptions.load(force: true)
|
|
|
|
playlists.load(force: true)
|
|
|
|
}
|
2021-10-24 14:46:04 +05:30
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
.environmentObject(accounts)
|
2021-12-05 01:05:41 +05:30
|
|
|
.environmentObject(comments)
|
2021-10-06 01:50:09 +05:30
|
|
|
.environmentObject(instances)
|
2021-09-25 13:48:22 +05:30
|
|
|
.environmentObject(navigation)
|
2021-10-06 01:50:09 +05:30
|
|
|
.environmentObject(player)
|
|
|
|
.environmentObject(playlists)
|
2021-09-25 13:48:22 +05:30
|
|
|
.environmentObject(recents)
|
2021-10-06 01:50:09 +05:30
|
|
|
.environmentObject(search)
|
|
|
|
.environmentObject(subscriptions)
|
2021-10-25 03:56:25 +05:30
|
|
|
.environmentObject(thumbnailsModel)
|
|
|
|
|
2021-12-01 04:28:11 +05:30
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
)
|
2021-10-06 01:50:09 +05:30
|
|
|
#if !os(tvOS)
|
2021-12-25 00:50:05 +05:30
|
|
|
.onOpenURL { OpenURLHandler(accounts: accounts, player: player).handle($0) }
|
2021-12-01 04:28:11 +05:30
|
|
|
.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)
|
2021-12-27 02:44:46 +05:30
|
|
|
.environmentObject(player)
|
2021-12-01 04:28:11 +05:30
|
|
|
}
|
|
|
|
)
|
2021-08-02 04:31:24 +05:30
|
|
|
#endif
|
2021-12-20 03:57:20 +05:30
|
|
|
.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-08 04:09:18 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
func configure() {
|
|
|
|
SiestaLog.Category.enabled = .common
|
2021-10-22 04:59:10 +05:30
|
|
|
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
2021-12-02 04:24:58 +05:30
|
|
|
SDWebImageManager.defaultImageCache = PINCache(name: "stream.yattee.app")
|
2021-10-24 19:31:36 +05:30
|
|
|
#if !os(macOS)
|
2022-02-17 02:40:57 +05:30
|
|
|
setupNowPlayingInfoCenter()
|
2021-10-24 19:31:36 +05:30
|
|
|
#endif
|
2021-10-17 04:18:58 +05:30
|
|
|
|
2022-01-03 01:11:04 +05:30
|
|
|
#if os(iOS)
|
|
|
|
if Defaults[.lockPortraitWhenBrowsing] {
|
|
|
|
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
if let account = accounts.lastUsed ??
|
|
|
|
instances.lastUsed?.anonymousAccount ??
|
2021-11-09 04:44:28 +05:30
|
|
|
InstancesModel.all.first?.anonymousAccount
|
2021-10-18 03:19:56 +05:30
|
|
|
{
|
2021-10-20 02:57:04 +05:30
|
|
|
accounts.setCurrent(account)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-10-18 04:36:00 +05:30
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
if accounts.current.isNil {
|
2021-10-18 04:36:00 +05:30
|
|
|
navigation.presentingWelcomeScreen = true
|
|
|
|
}
|
2021-10-20 02:57:04 +05:30
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
playlists.accounts = accounts
|
|
|
|
search.accounts = accounts
|
|
|
|
subscriptions.accounts = accounts
|
2021-10-24 23:31:08 +05:30
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
comments.player = player
|
|
|
|
|
2021-11-09 04:44:28 +05:30
|
|
|
menu.accounts = accounts
|
|
|
|
menu.navigation = navigation
|
|
|
|
menu.player = player
|
2022-02-17 01:53:11 +05:30
|
|
|
playerControls.player = player
|
2021-11-09 04:44:28 +05:30
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
player.accounts = accounts
|
|
|
|
player.comments = comments
|
2022-02-17 01:53:11 +05:30
|
|
|
player.controls = playerControls
|
2021-12-05 01:05:41 +05:30
|
|
|
|
2022-01-09 20:35:05 +05:30
|
|
|
if !accounts.current.isNil {
|
|
|
|
player.restoreQueue()
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:52:19 +05:30
|
|
|
if !Defaults[.saveRecents] {
|
|
|
|
recents.clear()
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:52:19 +05:30
|
|
|
var section = Defaults[.visibleSections].min()?.tabSelection
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
if section == .playlists {
|
|
|
|
section = .search
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
navigation.tabSelection = section ?? .search
|
2021-12-20 03:57:20 +05:30
|
|
|
|
|
|
|
subscriptions.load()
|
|
|
|
playlists.load()
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-10-18 04:36:00 +05:30
|
|
|
|
2022-02-17 02:40:57 +05:30
|
|
|
func setupNowPlayingInfoCenter() {
|
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
|
|
|
|
|
|
|
|
UIApplication.shared.beginReceivingRemoteControlEvents()
|
|
|
|
|
|
|
|
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-18 04:36:00 +05:30
|
|
|
func openWelcomeScreenIfAccountEmpty() {
|
2021-10-20 02:57:04 +05:30
|
|
|
guard Defaults[.instances].isEmpty else {
|
2021-10-18 04:36:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
navigation.presentingWelcomeScreen = true
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|