2021-12-26 21:14:46 +00:00
|
|
|
import CoreData
|
2022-05-29 14:38:37 +00:00
|
|
|
import CoreMedia
|
2021-06-28 15:27:53 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VideoContextMenuView: View {
|
2021-10-05 20:20:09 +00:00
|
|
|
let video: Video
|
|
|
|
|
2021-10-28 17:14:55 +00:00
|
|
|
@Environment(\.inChannelView) private var inChannelView
|
2021-12-19 22:27:20 +00:00
|
|
|
@Environment(\.inChannelPlaylistView) private var inChannelPlaylistView
|
2022-12-18 23:09:54 +00:00
|
|
|
@Environment(\.inQueueListing) private var inQueueListing
|
2021-10-21 23:29:10 +00:00
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
2021-10-24 21:36:24 +00:00
|
|
|
@Environment(\.currentPlaylistID) private var playlistID
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
@ObservedObject private var navigation = NavigationModel.shared
|
|
|
|
@ObservedObject private var player = PlayerModel.shared
|
|
|
|
@ObservedObject private var playlists = PlaylistsModel.shared
|
2022-12-11 15:15:42 +00:00
|
|
|
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
|
2021-06-28 15:27:53 +00:00
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
@FetchRequest private var watchRequest: FetchedResults<Watch>
|
|
|
|
|
2023-05-26 20:49:38 +00:00
|
|
|
@Default(.showPlayNowInBackendContextMenu) private var showPlayNowInBackendContextMenu
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-06-30 10:10:43 +00:00
|
|
|
private var backgroundContext = PersistenceController.shared.container.newBackgroundContext()
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-05-28 21:41:23 +00:00
|
|
|
init(video: Video) {
|
2021-12-26 21:14:46 +00:00
|
|
|
self.video = video
|
|
|
|
_watchRequest = video.watchFetchRequest
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:27:53 +00:00
|
|
|
var body: some View {
|
2022-03-27 18:27:26 +00:00
|
|
|
if video.videoID != Video.fixtureID {
|
|
|
|
contextMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder var contextMenu: some View {
|
2022-12-18 23:09:54 +00:00
|
|
|
if inQueueListing {
|
|
|
|
if let item = player.queue.first(where: { $0.videoID == video.videoID }) {
|
|
|
|
removeFromQueueButton(item)
|
|
|
|
}
|
|
|
|
removeAllFromQueueButton()
|
|
|
|
}
|
2022-11-12 23:01:04 +00:00
|
|
|
if !video.localStreamIsDirectory {
|
2023-06-09 15:46:31 +00:00
|
|
|
if Defaults[.saveHistory] {
|
2022-11-12 23:01:04 +00:00
|
|
|
Section {
|
|
|
|
if let watchedAtString {
|
|
|
|
Text(watchedAtString)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !watch.isNil, !watch!.finished, !watchingNow {
|
|
|
|
continueButton
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(watch?.finished ?? false) {
|
|
|
|
markAsWatchedButton
|
|
|
|
}
|
|
|
|
|
|
|
|
if !watch.isNil, !watchingNow {
|
|
|
|
removeFromHistoryButton
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
2022-11-12 23:01:04 +00:00
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
Section {
|
|
|
|
playNowButton
|
|
|
|
#if !os(tvOS)
|
|
|
|
playNowInPictureInPictureButton
|
|
|
|
playNowInMusicMode
|
|
|
|
#endif
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2023-06-09 15:46:31 +00:00
|
|
|
if Defaults[.showPlayNowInBackendContextMenu] {
|
2023-05-26 20:49:38 +00:00
|
|
|
Section {
|
|
|
|
ForEach(PlayerBackendType.allCases, id: \.self) { backend in
|
|
|
|
playNowInBackendButton(backend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
Section {
|
|
|
|
playNextButton
|
|
|
|
addToQueueButton
|
|
|
|
}
|
|
|
|
|
|
|
|
if accounts.app.supportsUserPlaylists, accounts.signedIn, !video.isLocal {
|
|
|
|
Section {
|
2022-12-16 18:34:12 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
addToPlaylistButton
|
|
|
|
#else
|
|
|
|
addToPlaylistMenu
|
|
|
|
#endif
|
2022-11-12 23:01:04 +00:00
|
|
|
addToLastPlaylistButton
|
2022-06-30 10:10:43 +00:00
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
if let id = navigation.tabSelection?.playlistID ?? playlistID {
|
|
|
|
removeFromPlaylistButton(playlistID: id)
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 21:27:48 +00:00
|
|
|
#if !os(tvOS)
|
2022-11-12 23:01:04 +00:00
|
|
|
Section {
|
|
|
|
ShareButton(contentItem: .init(video: video))
|
|
|
|
}
|
2022-06-07 21:27:48 +00:00
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-24 21:36:24 +00:00
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
#if os(iOS)
|
2022-11-13 17:52:15 +00:00
|
|
|
if video.isLocal,
|
|
|
|
let url = video.localStream?.localURL,
|
|
|
|
DocumentsModel.shared.isDocument(url)
|
|
|
|
{
|
2022-11-12 23:01:04 +00:00
|
|
|
Section {
|
|
|
|
removeDocumentButton
|
2021-10-28 17:14:55 +00:00
|
|
|
}
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
2022-06-26 12:55:23 +00:00
|
|
|
#endif
|
2022-06-26 11:57:02 +00:00
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
if !inChannelView, !inChannelPlaylistView, !video.isLocal {
|
2021-10-20 22:21:50 +00:00
|
|
|
Section {
|
2022-06-24 23:21:05 +00:00
|
|
|
openChannelButton
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2022-06-24 23:21:05 +00:00
|
|
|
if accounts.app.supportsSubscriptions, accounts.api.signedIn {
|
|
|
|
subscriptionButton
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-19 21:27:04 +00:00
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
private var watch: Watch? {
|
|
|
|
watchRequest.first
|
|
|
|
}
|
|
|
|
|
|
|
|
private var watchingNow: Bool {
|
|
|
|
player.currentVideo == video
|
|
|
|
}
|
|
|
|
|
|
|
|
private var watchedAtString: String? {
|
|
|
|
if watchingNow {
|
2022-09-04 15:28:30 +00:00
|
|
|
return "Watching now".localized()
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:27:01 +00:00
|
|
|
if let watch, let watchedAtString = watch.watchedAtString {
|
2022-06-30 10:10:43 +00:00
|
|
|
if watchedAtString == "in 0 seconds" {
|
2022-09-04 15:28:30 +00:00
|
|
|
return "Just watched".localized()
|
2022-06-30 10:10:43 +00:00
|
|
|
}
|
2022-09-04 15:28:30 +00:00
|
|
|
let localizedWatchedString = "Watched %@".localized()
|
|
|
|
return String(format: localizedWatchedString, watchedAtString)
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
private var continueButton: some View {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button {
|
2022-05-28 21:41:23 +00:00
|
|
|
player.play(video, at: .secondsInDefaultTimescale(watch!.stoppedAt))
|
2021-12-26 21:14:46 +00:00
|
|
|
} label: {
|
2022-06-18 12:39:49 +00:00
|
|
|
Label("Continue from \(watch!.stoppedAt.formattedAsPlaybackTime(allowZero: true) ?? "where I left off")", systemImage: "playpause")
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 22:12:59 +00:00
|
|
|
|
2022-06-30 10:10:43 +00:00
|
|
|
var markAsWatchedButton: some View {
|
|
|
|
Button {
|
2022-12-09 00:15:19 +00:00
|
|
|
Watch.markAsWatched(videoID: video.videoID, account: accounts.current, duration: video.length, context: backgroundContext)
|
2022-12-12 23:39:50 +00:00
|
|
|
FeedModel.shared.calculateUnwatchedFeed()
|
2023-05-25 12:28:29 +00:00
|
|
|
WatchModel.shared.watchesChanged()
|
2022-06-30 10:10:43 +00:00
|
|
|
} label: {
|
|
|
|
Label("Mark as watched", systemImage: "checkmark.circle.fill")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
var removeFromHistoryButton: some View {
|
|
|
|
Button {
|
2022-12-12 23:39:50 +00:00
|
|
|
guard let watch else { return }
|
2021-12-26 21:14:46 +00:00
|
|
|
player.removeWatch(watch)
|
|
|
|
} label: {
|
|
|
|
Label("Remove from history", systemImage: "delete.left.fill")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var playNowButton: some View {
|
|
|
|
Button {
|
2022-06-07 21:27:48 +00:00
|
|
|
if player.musicMode {
|
|
|
|
player.toggleMusicMode()
|
|
|
|
}
|
|
|
|
|
2022-05-28 21:41:23 +00:00
|
|
|
player.play(video)
|
2021-10-05 20:20:09 +00:00
|
|
|
} label: {
|
|
|
|
Label("Play Now", systemImage: "play")
|
|
|
|
}
|
|
|
|
}
|
2021-07-07 22:39:18 +00:00
|
|
|
|
2023-05-26 20:49:38 +00:00
|
|
|
private func playNowInBackendButton(_ backend: PlayerBackendType) -> some View {
|
|
|
|
Button {
|
|
|
|
if player.musicMode {
|
|
|
|
player.toggleMusicMode()
|
|
|
|
}
|
|
|
|
|
|
|
|
player.forceBackendOnPlay = backend
|
|
|
|
|
|
|
|
player.play(video)
|
|
|
|
} label: {
|
|
|
|
Label("Play Now in \(backend.label)", systemImage: "play")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 14:38:37 +00:00
|
|
|
private var playNowInPictureInPictureButton: some View {
|
|
|
|
Button {
|
2022-08-26 20:17:21 +00:00
|
|
|
player.avPlayerBackend.startPictureInPictureOnPlay = true
|
|
|
|
|
|
|
|
#if !os(macOS)
|
|
|
|
player.exitFullScreen()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if player.activeBackend != PlayerBackendType.appleAVPlayer {
|
|
|
|
player.changeActiveBackend(from: .mpv, to: .appleAVPlayer)
|
|
|
|
}
|
2022-08-18 22:40:46 +00:00
|
|
|
player.hide()
|
2022-05-29 14:38:37 +00:00
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2022-05-29 15:50:54 +00:00
|
|
|
player.play(video, at: watch?.timeToRestart, showingPlayer: false)
|
2022-05-29 14:38:37 +00:00
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("Play in PiP", systemImage: "pip")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 21:27:48 +00:00
|
|
|
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-24 21:36:24 +00:00
|
|
|
private var playNextButton: some View {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button {
|
|
|
|
player.playNext(video)
|
|
|
|
} label: {
|
|
|
|
Label("Play Next", systemImage: "text.insert")
|
2021-09-28 23:01:49 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
private var addToQueueButton: some View {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button {
|
|
|
|
player.enqueueVideo(video)
|
|
|
|
} label: {
|
|
|
|
Label("Play Last", systemImage: "text.append")
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
2021-06-28 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
#if os(iOS)
|
2022-11-12 23:07:23 +00:00
|
|
|
@ViewBuilder private var removeDocumentButton: some View {
|
|
|
|
let action = {
|
2022-11-12 23:01:04 +00:00
|
|
|
if let url = video.localStream?.localURL {
|
|
|
|
NavigationModel.shared.presentAlert(
|
|
|
|
Alert(
|
|
|
|
title: Text("Are you sure you want to remove this document?"),
|
2022-11-12 23:07:23 +00:00
|
|
|
message: Text(String(format: "\"%@\" will be irreversibly removed from this device.", video.displayTitle)),
|
2022-11-12 23:01:04 +00:00
|
|
|
primaryButton: .destructive(Text("Remove")) {
|
|
|
|
do {
|
|
|
|
try DocumentsModel.shared.removeDocument(url)
|
|
|
|
} catch {
|
|
|
|
NavigationModel.shared.presentAlert(title: "Could not delete document", message: error.localizedDescription)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2022-11-12 23:07:23 +00:00
|
|
|
}
|
2022-11-18 23:06:13 +00:00
|
|
|
let label = Label("Remove…", systemImage: "trash.fill")
|
2022-11-12 23:07:23 +00:00
|
|
|
.foregroundColor(Color("AppRedColor"))
|
|
|
|
|
|
|
|
if #available(iOS 15, macOS 12, *) {
|
|
|
|
Button(role: .destructive, action: action) { label }
|
|
|
|
} else {
|
|
|
|
Button(action: action) { label }
|
2022-11-12 23:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
private var openChannelButton: some View {
|
2021-09-28 23:01:49 +00:00
|
|
|
Button {
|
2022-11-24 20:36:05 +00:00
|
|
|
NavigationModel.shared.openChannel(
|
2021-12-17 16:34:55 +00:00
|
|
|
video.channel,
|
2022-06-30 08:05:32 +00:00
|
|
|
navigationStyle: navigationStyle
|
2021-12-17 16:34:55 +00:00
|
|
|
)
|
2021-09-28 23:01:49 +00:00
|
|
|
} label: {
|
|
|
|
Label("\(video.author) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
|
2021-06-28 15:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
private var subscriptionButton: some View {
|
2021-08-25 22:12:59 +00:00
|
|
|
Group {
|
2021-08-31 21:17:50 +00:00
|
|
|
if subscriptions.isSubscribing(video.channel.id) {
|
2021-11-28 14:37:55 +00:00
|
|
|
Button {
|
2021-08-31 21:17:50 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
subscriptions.unsubscribe(video.channel.id)
|
|
|
|
#else
|
2022-06-24 23:21:05 +00:00
|
|
|
navigation.presentUnsubscribeAlert(video.channel, subscriptions: subscriptions)
|
2021-08-31 21:17:50 +00:00
|
|
|
#endif
|
2021-09-28 23:01:49 +00:00
|
|
|
} label: {
|
|
|
|
Label("Unsubscribe", systemImage: "xmark.circle")
|
2021-08-25 22:12:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-28 23:01:49 +00:00
|
|
|
Button {
|
2021-08-31 21:17:50 +00:00
|
|
|
subscriptions.subscribe(video.channel.id) {
|
2021-09-25 08:18:22 +00:00
|
|
|
navigation.sidebarSectionChanged.toggle()
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
2021-09-28 23:01:49 +00:00
|
|
|
} label: {
|
|
|
|
Label("Subscribe", systemImage: "star.circle")
|
2021-08-25 22:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 15:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 14:53:53 +00:00
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
private var addToPlaylistButton: some View {
|
2021-09-28 23:01:49 +00:00
|
|
|
Button {
|
2021-09-28 18:06:05 +00:00
|
|
|
navigation.presentAddToPlaylist(video)
|
2021-09-28 23:01:49 +00:00
|
|
|
} label: {
|
2022-02-04 17:38:29 +00:00
|
|
|
Label("Add to Playlist...", systemImage: "text.badge.plus")
|
2021-07-09 14:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
@ViewBuilder private var addToLastPlaylistButton: some View {
|
|
|
|
if let playlist = playlists.lastUsed {
|
|
|
|
Button {
|
2022-11-24 20:36:05 +00:00
|
|
|
playlists.addVideo(playlistID: playlist.id, videoID: video.videoID)
|
2022-06-18 12:39:49 +00:00
|
|
|
} label: {
|
|
|
|
Label("Add to \(playlist.title)", systemImage: "text.badge.star")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 18:34:12 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
@ViewBuilder private var addToPlaylistMenu: some View {
|
|
|
|
if playlists.playlists.isEmpty {
|
|
|
|
Text("No Playlists")
|
|
|
|
} else {
|
|
|
|
Menu {
|
|
|
|
ForEach(playlists.editable) { playlist in
|
|
|
|
Button {
|
|
|
|
playlists.addVideo(playlistID: playlist.id, videoID: video.videoID)
|
|
|
|
} label: {
|
|
|
|
Text(playlist.title).tag(playlist.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("Add to Playlist...", systemImage: "text.badge.plus")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
func removeFromPlaylistButton(playlistID: String) -> some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
Button {
|
2022-05-21 22:29:51 +00:00
|
|
|
playlists.removeVideo(index: video.indexID!, playlistID: playlistID)
|
2021-09-28 23:01:49 +00:00
|
|
|
} label: {
|
2022-02-04 17:38:29 +00:00
|
|
|
Label("Remove from Playlist", systemImage: "text.badge.minus")
|
2021-07-09 14:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-18 23:09:54 +00:00
|
|
|
|
|
|
|
private func removeFromQueueButton(_ item: PlayerQueueItem) -> some View {
|
|
|
|
Button {
|
|
|
|
player.remove(item)
|
|
|
|
} label: {
|
|
|
|
Label("Remove from the queue", systemImage: "trash")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func removeAllFromQueueButton() -> some View {
|
|
|
|
Button {
|
|
|
|
player.removeQueueItems()
|
|
|
|
} label: {
|
|
|
|
Label("Clear the queue", systemImage: "trash.fill")
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 15:27:53 +00:00
|
|
|
}
|