2021-12-27 02:44:46 +05:30
|
|
|
import CoreData
|
2022-05-29 20:08:37 +05:30
|
|
|
import CoreMedia
|
2021-06-28 20:57:53 +05:30
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VideoContextMenuView: View {
|
2021-10-06 01:50:09 +05:30
|
|
|
let video: Video
|
|
|
|
|
2021-10-28 22:44:55 +05:30
|
|
|
@Environment(\.inChannelView) private var inChannelView
|
2021-12-20 03:57:20 +05:30
|
|
|
@Environment(\.inChannelPlaylistView) private var inChannelPlaylistView
|
2021-10-22 04:59:10 +05:30
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
2021-10-25 03:06:24 +05:30
|
|
|
@Environment(\.currentPlaylistID) private var playlistID
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2021-09-25 13:48:22 +05:30
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2021-10-06 01:50:09 +05:30
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2021-09-28 23:36:05 +05:30
|
|
|
@EnvironmentObject<PlaylistsModel> private var playlists
|
2021-09-25 17:47:58 +05:30
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
2021-09-25 13:48:22 +05:30
|
|
|
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
2021-06-28 20:57:53 +05:30
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
@FetchRequest private var watchRequest: FetchedResults<Watch>
|
|
|
|
|
|
|
|
@Default(.saveHistory) private var saveHistory
|
|
|
|
|
|
|
|
private var viewContext: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
|
|
|
|
2022-05-29 03:11:23 +05:30
|
|
|
init(video: Video) {
|
2021-12-27 02:44:46 +05:30
|
|
|
self.video = video
|
|
|
|
_watchRequest = video.watchFetchRequest
|
|
|
|
}
|
|
|
|
|
2021-06-28 20:57:53 +05:30
|
|
|
var body: some View {
|
2022-03-27 23:57:26 +05:30
|
|
|
if video.videoID != Video.fixtureID {
|
|
|
|
contextMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder var contextMenu: some View {
|
2021-12-27 02:44:46 +05:30
|
|
|
if saveHistory {
|
|
|
|
Section {
|
|
|
|
if let watchedAtString = watchedAtString {
|
|
|
|
Text(watchedAtString)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !watch.isNil, !watch!.finished, !watchingNow {
|
|
|
|
continueButton
|
|
|
|
}
|
|
|
|
|
|
|
|
if !watch.isNil, !watchingNow {
|
|
|
|
removeFromHistoryButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
Section {
|
|
|
|
playNowButton
|
2022-05-29 20:08:37 +05:30
|
|
|
#if os(iOS)
|
|
|
|
playNowInPictureInPictureButton
|
|
|
|
#endif
|
2022-06-08 02:57:48 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
playNowInMusicMode
|
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-10-25 03:06:24 +05:30
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
Section {
|
|
|
|
playNextButton
|
|
|
|
addToQueueButton
|
|
|
|
}
|
|
|
|
|
2021-12-20 03:57:20 +05:30
|
|
|
if !inChannelView, !inChannelPlaylistView {
|
2021-10-28 22:44:55 +05:30
|
|
|
Section {
|
|
|
|
openChannelButton
|
2021-10-25 03:06:24 +05:30
|
|
|
|
2021-12-20 03:57:20 +05:30
|
|
|
if accounts.app.supportsSubscriptions, accounts.api.signedIn {
|
2021-10-28 22:44:55 +05:30
|
|
|
subscriptionButton
|
|
|
|
}
|
2021-10-21 03:51:50 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
if accounts.app.supportsUserPlaylists, accounts.signedIn {
|
2021-10-21 03:51:50 +05:30
|
|
|
Section {
|
2021-10-25 03:06:24 +05:30
|
|
|
addToPlaylistButton
|
2022-06-18 18:09:49 +05:30
|
|
|
addToLastPlaylistButton
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
if let id = navigation.tabSelection?.playlistID ?? playlistID {
|
2021-10-21 03:51:50 +05:30
|
|
|
removeFromPlaylistButton(playlistID: id)
|
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-20 02:57:04 +05:30
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
private var watch: Watch? {
|
|
|
|
watchRequest.first
|
|
|
|
}
|
|
|
|
|
|
|
|
private var watchingNow: Bool {
|
|
|
|
player.currentVideo == video
|
|
|
|
}
|
|
|
|
|
|
|
|
private var watchedAtString: String? {
|
|
|
|
if watchingNow {
|
|
|
|
return "Watching now"
|
|
|
|
}
|
|
|
|
|
|
|
|
if let watch = watch, let watchedAtString = watch.watchedAtString {
|
|
|
|
return "Watched \(watchedAtString)"
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
private var continueButton: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
Button {
|
2022-05-29 03:11:23 +05:30
|
|
|
player.play(video, at: .secondsInDefaultTimescale(watch!.stoppedAt))
|
2021-12-27 02:44:46 +05:30
|
|
|
} label: {
|
2022-06-18 18:09:49 +05:30
|
|
|
Label("Continue from \(watch!.stoppedAt.formattedAsPlaybackTime(allowZero: true) ?? "where I left off")", systemImage: "playpause")
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
|
|
|
}
|
2021-08-26 03:42:59 +05:30
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
var removeFromHistoryButton: some View {
|
|
|
|
Button {
|
|
|
|
guard let watch = watch else {
|
2021-10-28 22:44:55 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
player.removeWatch(watch)
|
|
|
|
} label: {
|
|
|
|
Label("Remove from history", systemImage: "delete.left.fill")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var playNowButton: some View {
|
|
|
|
Button {
|
2022-06-08 02:57:48 +05:30
|
|
|
if player.musicMode {
|
|
|
|
player.toggleMusicMode()
|
|
|
|
}
|
|
|
|
|
2022-05-29 03:11:23 +05:30
|
|
|
player.play(video)
|
2021-10-06 01:50:09 +05:30
|
|
|
} label: {
|
|
|
|
Label("Play Now", systemImage: "play")
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
|
2022-05-29 20:08:37 +05:30
|
|
|
private var playNowInPictureInPictureButton: some View {
|
|
|
|
Button {
|
|
|
|
player.controls.startPiP(startImmediately: false)
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2022-05-29 21:20:54 +05:30
|
|
|
player.play(video, at: watch?.timeToRestart, showingPlayer: false)
|
2022-05-29 20:08:37 +05:30
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("Play in PiP", systemImage: "pip")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 02:57:48 +05:30
|
|
|
private var playNowInMusicMode: some View {
|
|
|
|
Button {
|
|
|
|
if !player.musicMode {
|
|
|
|
player.toggleMusicMode()
|
|
|
|
}
|
|
|
|
|
|
|
|
player.play(video, at: watch?.timeToRestart, showingPlayer: false)
|
|
|
|
} label: {
|
|
|
|
Label("Play Music", systemImage: "music.note")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
private var playNextButton: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
Button {
|
|
|
|
player.playNext(video)
|
|
|
|
} label: {
|
|
|
|
Label("Play Next", systemImage: "text.insert")
|
2021-09-29 04:31:49 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
private var addToQueueButton: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
Button {
|
|
|
|
player.enqueueVideo(video)
|
|
|
|
} label: {
|
|
|
|
Label("Play Last", systemImage: "text.append")
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
2021-06-28 20:57:53 +05:30
|
|
|
}
|
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
private var openChannelButton: some View {
|
2021-09-29 04:31:49 +05:30
|
|
|
Button {
|
2021-12-17 22:04:55 +05:30
|
|
|
NavigationModel.openChannel(
|
|
|
|
video.channel,
|
|
|
|
player: player,
|
|
|
|
recents: recents,
|
|
|
|
navigation: navigation,
|
|
|
|
navigationStyle: navigationStyle
|
|
|
|
)
|
2021-09-29 04:31:49 +05:30
|
|
|
} label: {
|
|
|
|
Label("\(video.author) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
|
2021-06-28 20:57:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
private var subscriptionButton: some View {
|
2021-08-26 03:42:59 +05:30
|
|
|
Group {
|
2021-09-01 02:47:50 +05:30
|
|
|
if subscriptions.isSubscribing(video.channel.id) {
|
2021-11-28 20:07:55 +05:30
|
|
|
Button {
|
2021-09-01 02:47:50 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
subscriptions.unsubscribe(video.channel.id)
|
|
|
|
#else
|
2021-09-25 13:48:22 +05:30
|
|
|
navigation.presentUnsubscribeAlert(video.channel)
|
2021-09-01 02:47:50 +05:30
|
|
|
#endif
|
2021-09-29 04:31:49 +05:30
|
|
|
} label: {
|
|
|
|
Label("Unsubscribe", systemImage: "xmark.circle")
|
2021-08-26 03:42:59 +05:30
|
|
|
}
|
|
|
|
} else {
|
2021-09-29 04:31:49 +05:30
|
|
|
Button {
|
2021-09-01 02:47:50 +05:30
|
|
|
subscriptions.subscribe(video.channel.id) {
|
2021-09-25 13:48:22 +05:30
|
|
|
navigation.sidebarSectionChanged.toggle()
|
2021-09-01 02:47:50 +05:30
|
|
|
}
|
2021-09-29 04:31:49 +05:30
|
|
|
} label: {
|
|
|
|
Label("Subscribe", systemImage: "star.circle")
|
2021-08-26 03:42:59 +05:30
|
|
|
}
|
|
|
|
}
|
2021-06-28 20:57:53 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-09 20:23:53 +05:30
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
private var addToPlaylistButton: some View {
|
2021-09-29 04:31:49 +05:30
|
|
|
Button {
|
2021-09-28 23:36:05 +05:30
|
|
|
navigation.presentAddToPlaylist(video)
|
2021-09-29 04:31:49 +05:30
|
|
|
} label: {
|
2022-02-04 23:08:29 +05:30
|
|
|
Label("Add to Playlist...", systemImage: "text.badge.plus")
|
2021-07-09 20:23:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
@ViewBuilder private var addToLastPlaylistButton: some View {
|
|
|
|
if let playlist = playlists.lastUsed {
|
|
|
|
Button {
|
|
|
|
playlists.addVideo(playlistID: playlist.id, videoID: video.videoID, navigation: navigation)
|
|
|
|
} label: {
|
|
|
|
Label("Add to \(playlist.title)", systemImage: "text.badge.star")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
func removeFromPlaylistButton(playlistID: String) -> some View {
|
2021-11-28 20:07:55 +05:30
|
|
|
Button {
|
2022-05-22 03:59:51 +05:30
|
|
|
playlists.removeVideo(index: video.indexID!, playlistID: playlistID)
|
2021-09-29 04:31:49 +05:30
|
|
|
} label: {
|
2022-02-04 23:08:29 +05:30
|
|
|
Label("Remove from Playlist", systemImage: "text.badge.minus")
|
2021-07-09 20:23:53 +05:30
|
|
|
}
|
|
|
|
}
|
2021-06-28 20:57:53 +05:30
|
|
|
}
|