1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 23:00:32 +05:30
yattee/Shared/Views/VideoContextMenuView.swift

367 lines
12 KiB
Swift
Raw Normal View History

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