1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Views/ChannelPlaylistView.swift

179 lines
5.0 KiB
Swift
Raw Normal View History

2021-10-23 04:34:03 +05:30
import Siesta
import SwiftUI
struct ChannelPlaylistView: View {
2022-05-30 00:39:57 +05:30
#if os(iOS)
static let hiddenOffset = max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) + 100
#endif
var playlist: ChannelPlaylist?
2021-10-23 04:34:03 +05:30
2021-10-27 04:29:59 +05:30
@State private var presentingShareSheet = false
2021-11-13 21:15:47 +05:30
@State private var shareURL: URL?
2021-10-27 04:29:59 +05:30
2022-05-30 00:39:57 +05:30
#if os(iOS)
@State private var viewVerticalOffset = Self.hiddenOffset
#endif
2021-10-23 04:34:03 +05:30
@StateObject private var store = Store<ChannelPlaylist>()
@Environment(\.colorScheme) private var colorScheme
2022-05-30 00:39:57 +05:30
@Environment(\.navigationStyle) private var navigationStyle
2021-10-23 04:34:03 +05:30
@EnvironmentObject<AccountsModel> private var accounts
2022-05-30 00:39:57 +05:30
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
2022-05-30 00:39:57 +05:30
@EnvironmentObject<RecentsModel> private var recents
2021-10-23 04:34:03 +05:30
2022-05-30 00:39:57 +05:30
private var items: [ContentItem] {
2021-10-23 04:34:03 +05:30
ContentItem.array(of: store.item?.videos ?? [])
}
2022-05-30 00:39:57 +05:30
private var presentedPlaylist: ChannelPlaylist? {
2022-05-30 01:42:59 +05:30
playlist ?? recents.presentedPlaylist
2022-05-30 00:39:57 +05:30
}
private var resource: Resource? {
guard let playlist = presentedPlaylist else {
return nil
}
let resource = accounts.api.channelPlaylist(playlist.id)
resource?.addObserver(store)
return resource
2021-10-23 04:34:03 +05:30
}
var body: some View {
2022-05-30 00:39:57 +05:30
if navigationStyle == .tab {
NavigationView {
BrowserPlayerControls {
content
}
}
#if os(iOS)
.onChange(of: navigation.presentingPlaylist) { newValue in
if newValue {
2022-05-30 01:42:59 +05:30
store.clear()
2022-05-30 00:39:57 +05:30
viewVerticalOffset = 0
resource?.load()
} else {
viewVerticalOffset = Self.hiddenOffset
}
}
.offset(y: viewVerticalOffset)
2022-05-30 01:42:59 +05:30
.opacity(viewVerticalOffset == Self.hiddenOffset ? 0 : 1)
2022-05-30 00:39:57 +05:30
.animation(.easeIn(duration: 0.2), value: viewVerticalOffset)
#endif
} else {
BrowserPlayerControls {
content
}
}
2021-10-23 04:34:03 +05:30
}
var content: some View {
VStack(alignment: .leading) {
#if os(tvOS)
2021-11-02 03:26:18 +05:30
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-02 03:26:18 +05:30
}
2021-10-23 04:34:03 +05:30
#endif
VerticalCells(items: items)
.environment(\.inChannelPlaylistView, true)
2021-10-23 04:34:03 +05:30
}
2021-10-27 04:29:59 +05:30
#if os(iOS)
2021-11-08 21:59:35 +05:30
.sheet(isPresented: $presentingShareSheet) {
2021-11-13 21:15:47 +05:30
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
2021-10-27 04:29:59 +05:30
}
2021-11-08 21:59:35 +05:30
}
2021-10-27 04:29:59 +05:30
#endif
2021-10-23 04:34:03 +05:30
.onAppear {
resource?.loadIfNeeded()
}
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
#else
2021-11-08 21:59:35 +05:30
.toolbar {
ToolbarItem(placement: .navigation) {
2022-05-30 00:39:57 +05:30
if navigationStyle == .tab {
Button("Done") {
navigation.presentingPlaylist = false
}
}
2021-11-08 21:59:35 +05:30
}
2021-10-27 04:29:59 +05:30
ToolbarItem(placement: playlistButtonsPlacement) {
HStack {
2022-05-30 00:39:57 +05:30
ShareButton(
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
if let playlist = presentedPlaylist {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
}
playButton
shuffleButton
}
2021-10-23 04:34:03 +05:30
}
2021-11-08 21:59:35 +05:30
}
2022-05-30 00:39:57 +05:30
.navigationTitle(presentedPlaylist?.title ?? "")
#endif
}
private var playlistButtonsPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigationBarTrailing
2021-10-23 04:34:03 +05:30
#else
.automatic
2021-10-23 04:34:03 +05:30
#endif
}
2021-10-27 04:29:59 +05:30
private var playButton: some View {
Button {
player.play(videos)
} label: {
Label("Play All", systemImage: "play")
}
}
private var shuffleButton: some View {
Button {
player.play(videos, shuffling: true)
} label: {
Label("Shuffle", systemImage: "shuffle")
}
}
private var videos: [Video] {
items.compactMap(\.video)
}
2021-10-27 04:29:59 +05:30
private var contentItem: ContentItem {
ContentItem(playlist: playlist)
}
2021-10-23 04:34:03 +05:30
}
struct ChannelPlaylistView_Previews: PreviewProvider {
static var previews: some View {
ChannelPlaylistView(playlist: ChannelPlaylist.fixture)
.injectFixtureEnvironmentObjects()
}
}