yattee/Shared/Views/VideoContextMenuView.swift

210 lines
6.0 KiB
Swift
Raw Normal View History

import CoreData
import Defaults
import SwiftUI
struct VideoContextMenuView: View {
let video: Video
@Binding var playerNavigationLinkActive: Bool
@Environment(\.inNavigationView) private var inNavigationView
2021-10-28 17:14:55 +00:00
@Environment(\.inChannelView) private var inChannelView
@Environment(\.inChannelPlaylistView) private var inChannelPlaylistView
@Environment(\.navigationStyle) private var navigationStyle
2021-10-24 21:36:24 +00:00
@Environment(\.currentPlaylistID) private var playlistID
2021-10-20 22:21:50 +00:00
@EnvironmentObject<AccountsModel> private var accounts
2021-09-25 08:18:22 +00:00
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<PlaylistsModel> private var playlists
2021-09-25 12:17:58 +00:00
@EnvironmentObject<RecentsModel> private var recents
2021-09-25 08:18:22 +00:00
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@FetchRequest private var watchRequest: FetchedResults<Watch>
@Default(.saveHistory) private var saveHistory
private var viewContext: NSManagedObjectContext = PersistenceController.shared.container.viewContext
init(video: Video, playerNavigationLinkActive: Binding<Bool>) {
self.video = video
_playerNavigationLinkActive = playerNavigationLinkActive
_watchRequest = video.watchFetchRequest
}
var body: some View {
2022-03-27 18:27:26 +00:00
if video.videoID != Video.fixtureID {
contextMenu
}
}
@ViewBuilder var contextMenu: some View {
if saveHistory {
Section {
if let watchedAtString = watchedAtString {
Text(watchedAtString)
}
if !watch.isNil, !watch!.finished, !watchingNow {
continueButton
}
if !watch.isNil, !watchingNow {
removeFromHistoryButton
}
}
}
Section {
playNowButton
}
2021-10-24 21:36:24 +00:00
Section {
playNextButton
addToQueueButton
}
if !inChannelView, !inChannelPlaylistView {
2021-10-28 17:14:55 +00:00
Section {
openChannelButton
2021-10-24 21:36:24 +00:00
if accounts.app.supportsSubscriptions, accounts.api.signedIn {
2021-10-28 17:14:55 +00:00
subscriptionButton
}
2021-10-20 22:21:50 +00:00
}
}
2021-10-20 22:21:50 +00:00
if accounts.app.supportsUserPlaylists {
Section {
2021-10-24 21:36:24 +00:00
addToPlaylistButton
2021-10-24 21:36:24 +00:00
if let id = navigation.tabSelection?.playlistID ?? playlistID {
2021-10-20 22:21:50 +00:00
removeFromPlaylistButton(playlistID: id)
}
}
}
#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 {
return "Watching now"
}
if let watch = watch, let watchedAtString = watch.watchedAtString {
return "Watched \(watchedAtString)"
}
return nil
}
private var continueButton: some View {
Button {
player.play(video, at: .secondsInDefaultTimescale(watch!.stoppedAt), inNavigationView: inNavigationView)
} label: {
Label("Continue from \(watch!.stoppedAt.formattedAsPlaybackTime() ?? "where I left off")", systemImage: "playpause")
}
}
2021-08-25 22:12:59 +00:00
var removeFromHistoryButton: some View {
Button {
guard let watch = watch else {
2021-10-28 17:14:55 +00:00
return
}
player.removeWatch(watch)
} label: {
Label("Remove from history", systemImage: "delete.left.fill")
}
}
private var playNowButton: some View {
Button {
player.play(video, inNavigationView: inNavigationView)
} label: {
Label("Play Now", systemImage: "play")
}
}
2021-07-07 22:39:18 +00:00
2021-10-24 21:36:24 +00:00
private var playNextButton: some View {
Button {
player.playNext(video)
} label: {
Label("Play Next", systemImage: "text.insert")
2021-09-28 23:01:49 +00:00
}
}
2021-10-24 21:36:24 +00:00
private var addToQueueButton: some View {
Button {
player.enqueueVideo(video)
} label: {
Label("Play Last", systemImage: "text.append")
2021-07-07 22:39:18 +00:00
}
}
2021-10-24 21:36:24 +00:00
private var openChannelButton: some View {
2021-09-28 23:01:49 +00:00
Button {
2021-12-17 16:34:55 +00:00
NavigationModel.openChannel(
video.channel,
player: player,
recents: recents,
navigation: navigation,
navigationStyle: navigationStyle
)
2021-09-28 23:01:49 +00:00
} label: {
Label("\(video.author) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
}
}
2021-10-24 21:36:24 +00:00
private var subscriptionButton: some View {
2021-08-25 22:12:59 +00:00
Group {
if subscriptions.isSubscribing(video.channel.id) {
2021-11-28 14:37:55 +00:00
Button {
#if os(tvOS)
subscriptions.unsubscribe(video.channel.id)
#else
2021-09-25 08:18:22 +00:00
navigation.presentUnsubscribeAlert(video.channel)
#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 {
subscriptions.subscribe(video.channel.id) {
2021-09-25 08:18:22 +00:00
navigation.sidebarSectionChanged.toggle()
}
2021-09-28 23:01:49 +00:00
} label: {
Label("Subscribe", systemImage: "star.circle")
2021-08-25 22:12:59 +00:00
}
}
}
}
2021-10-24 21:36:24 +00:00
private var addToPlaylistButton: some View {
2021-09-28 23:01:49 +00:00
Button {
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")
}
}
func removeFromPlaylistButton(playlistID: String) -> some View {
2021-11-28 14:37:55 +00:00
Button {
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")
}
}
}