yattee/Shared/Views/ChannelPlaylistView.swift

142 lines
3.8 KiB
Swift
Raw Normal View History

2021-10-22 23:04:03 +00:00
import Siesta
import SwiftUI
struct ChannelPlaylistView: View {
var playlist: ChannelPlaylist
2021-10-26 22:59:59 +00:00
@State private var presentingShareSheet = false
2021-11-13 15:45:47 +00:00
@State private var shareURL: URL?
2021-10-26 22:59:59 +00:00
2021-10-22 23:04:03 +00:00
@StateObject private var store = Store<ChannelPlaylist>()
@Environment(\.colorScheme) private var colorScheme
@Environment(\.inNavigationView) private var inNavigationView
2021-10-22 23:04:03 +00:00
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
2021-10-22 23:04:03 +00:00
var items: [ContentItem] {
ContentItem.array(of: store.item?.videos ?? [])
}
var resource: Resource? {
accounts.api.channelPlaylist(playlist.id)
}
var body: some View {
#if os(iOS)
if inNavigationView {
content
} else {
2022-02-16 20:23:11 +00:00
BrowserPlayerControls {
2021-10-22 23:04:03 +00:00
content
}
}
#else
2022-02-16 20:23:11 +00:00
BrowserPlayerControls {
2021-10-22 23:04:03 +00:00
content
}
#endif
}
var content: some View {
VStack(alignment: .leading) {
#if os(tvOS)
2021-11-01 21:56:18 +00:00
HStack {
Text(playlist.title)
.font(.title2)
.frame(alignment: .leading)
Spacer()
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
.labelStyle(.iconOnly)
playButton
.labelStyle(.iconOnly)
shuffleButton
.labelStyle(.iconOnly)
2021-11-01 21:56:18 +00:00
}
2021-10-22 23:04:03 +00:00
#endif
VerticalCells(items: items)
.environment(\.inChannelPlaylistView, true)
2021-10-22 23:04:03 +00:00
}
2021-10-26 22:59:59 +00:00
#if os(iOS)
2021-11-08 16:29:35 +00:00
.sheet(isPresented: $presentingShareSheet) {
2021-11-13 15:45:47 +00:00
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
2021-10-26 22:59:59 +00:00
}
2021-11-08 16:29:35 +00:00
}
2021-10-26 22:59:59 +00:00
#endif
2021-10-22 23:04:03 +00:00
.onAppear {
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
#else
2021-11-08 16:29:35 +00:00
.toolbar {
ToolbarItem(placement: .navigation) {
ShareButton(
contentItem: contentItem,
2021-11-13 15:45:47 +00:00
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
2021-11-08 16:29:35 +00:00
)
}
2021-10-26 22:59:59 +00:00
ToolbarItem(placement: playlistButtonsPlacement) {
HStack {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
playButton
shuffleButton
}
2021-10-22 23:04:03 +00:00
}
2021-11-08 16:29:35 +00:00
}
.navigationTitle(playlist.title)
2021-12-17 19:53:36 +00:00
#if os(iOS)
.navigationBarHidden(player.playerNavigationLinkActive)
#endif
#endif
}
private var playlistButtonsPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigationBarTrailing
2021-10-22 23:04:03 +00:00
#else
.automatic
2021-10-22 23:04:03 +00:00
#endif
}
2021-10-26 22:59:59 +00:00
private var playButton: some View {
Button {
player.play(videos, inNavigationView: inNavigationView)
} label: {
Label("Play All", systemImage: "play")
}
}
private var shuffleButton: some View {
Button {
player.play(videos, shuffling: true, inNavigationView: inNavigationView)
} label: {
Label("Shuffle", systemImage: "shuffle")
}
}
private var videos: [Video] {
items.compactMap(\.video)
}
2021-10-26 22:59:59 +00:00
private var contentItem: ContentItem {
ContentItem(playlist: playlist)
}
2021-10-22 23:04:03 +00:00
}
struct ChannelPlaylistView_Previews: PreviewProvider {
static var previews: some View {
ChannelPlaylistView(playlist: ChannelPlaylist.fixture)
.injectFixtureEnvironmentObjects()
}
}