yattee/Shared/Views/VideoContextMenuView.swift

84 lines
2.5 KiB
Swift
Raw Normal View History

import Defaults
import SwiftUI
struct VideoContextMenuView: View {
2021-07-11 20:52:49 +00:00
@EnvironmentObject<NavigationState> private var navigationState
2021-08-25 22:12:59 +00:00
@EnvironmentObject<Subscriptions> private var subscriptions
let video: Video
@Default(.showingAddToPlaylist) var showingAddToPlaylist
@Default(.videoIDToAddToPlaylist) var videoIDToAddToPlaylist
2021-08-25 22:12:59 +00:00
@State private var subscribed = false
var body: some View {
2021-08-25 22:12:59 +00:00
Section {
if navigationState.showOpenChannel(video.channel.id) {
openChannelButton
}
2021-08-25 22:12:59 +00:00
subscriptionButton
.opacity(subscribed ? 1 : 1)
2021-07-07 22:39:18 +00:00
2021-08-25 22:12:59 +00:00
openVideoDetailsButton
2021-08-25 22:12:59 +00:00
if navigationState.tabSelection == .playlists {
removeFromPlaylistButton
} else {
addToPlaylistButton
}
2021-07-07 22:39:18 +00:00
}
}
2021-08-25 22:12:59 +00:00
var openChannelButton: some View {
Button("\(video.author) Channel") {
2021-08-25 22:12:59 +00:00
navigationState.openChannel(video.channel)
2021-09-13 20:41:16 +00:00
navigationState.tabSelection = .channel(video.channel.id)
navigationState.sidebarSectionChanged.toggle()
}
}
2021-08-25 22:12:59 +00:00
var subscriptionButton: some View {
Group {
if subscriptions.isSubscribing(video.channel.id) {
2021-08-25 22:12:59 +00:00
Button("Unsubscribe", role: .destructive) {
#if os(tvOS)
subscriptions.unsubscribe(video.channel.id)
#else
navigationState.presentUnsubscribeAlert(video.channel)
#endif
2021-08-25 22:12:59 +00:00
}
} else {
Button("Subscribe") {
subscriptions.subscribe(video.channel.id) {
navigationState.sidebarSectionChanged.toggle()
}
2021-08-25 22:12:59 +00:00
}
}
}
}
var openVideoDetailsButton: some View {
Button("Open video details") {
2021-07-11 20:52:49 +00:00
navigationState.openVideoDetails(video)
}
}
var addToPlaylistButton: some View {
Button("Add to playlist...") {
videoIDToAddToPlaylist = video.id
showingAddToPlaylist = true
}
}
var removeFromPlaylistButton: some View {
Button("Remove from playlist", role: .destructive) {
let resource = InvidiousAPI.shared.playlistVideo(Defaults[.selectedPlaylistID]!, video.indexID!)
resource.request(.delete).onSuccess { _ in
InvidiousAPI.shared.playlists.load()
}
}
}
}