Add Play/Shuffle All for playlists (fixes #39)

Add Remove All from queue button on tvOS
This commit is contained in:
Arkadiusz Fal 2022-01-02 19:59:57 +01:00
parent ba21583a95
commit 04df9551ba
5 changed files with 125 additions and 36 deletions

View File

@ -8,16 +8,30 @@ extension PlayerModel {
currentItem?.video currentItem?.video
} }
func playAll(_ videos: [Video]) { func play(_ videos: [Video], shuffling: Bool = false, inNavigationView: Bool = false) {
let first = videos.first let videosToPlay = shuffling ? videos.shuffled() : videos
videos.forEach { video in guard let first = videosToPlay.first else {
enqueueVideo(video) { _, item in return
}
enqueueVideo(first, prepending: true) { _, item in
self.advanceToItem(item)
}
videosToPlay.dropFirst().reversed().forEach { video in
enqueueVideo(video, prepending: true) { _, item in
if item.video == first { if item.video == first {
self.advanceToItem(item) self.advanceToItem(item)
} }
} }
} }
if inNavigationView {
playerNavigationLinkActive = true
} else {
show()
}
} }
func playNext(_ video: Video) { func playNext(_ video: Video) {

View File

@ -100,12 +100,12 @@ struct PlaylistsView: View {
Text("No Playlists") Text("No Playlists")
.foregroundColor(.secondary) .foregroundColor(.secondary)
} else { } else {
Text("Current Playlist")
.foregroundColor(.secondary)
selectPlaylistButton selectPlaylistButton
} }
playButton
shuffleButton
Spacer() Spacer()
newPlaylistButton newPlaylistButton
@ -142,23 +142,14 @@ struct PlaylistsView: View {
selectPlaylistButton selectPlaylistButton
} }
Button {
player.playAll(items.compactMap(\.video))
player.show()
} label: {
HStack(spacing: 15) {
Image(systemName: "play.fill")
Text("Play All")
}
}
if currentPlaylist != nil {
editPlaylistButton
}
if let playlist = currentPlaylist { if let playlist = currentPlaylist {
editPlaylistButton
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id))) FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id)))
.labelStyle(.iconOnly) .labelStyle(.iconOnly)
playButton
shuffleButton
} }
Spacer() Spacer()
@ -265,6 +256,22 @@ struct PlaylistsView: View {
} }
} }
private var playButton: some View {
Button {
player.play(items.compactMap(\.video))
} label: {
Image(systemName: "play")
}
}
private var shuffleButton: some View {
Button {
player.play(items.compactMap(\.video), shuffling: true)
} label: {
Image(systemName: "shuffle")
}
}
private var currentPlaylist: Playlist? { private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first model.find(id: selectedPlaylistID) ?? model.all.first
} }

View File

@ -10,13 +10,10 @@ struct ChannelPlaylistView: View {
@StateObject private var store = Store<ChannelPlaylist>() @StateObject private var store = Store<ChannelPlaylist>()
@Environment(\.colorScheme) private var colorScheme @Environment(\.colorScheme) private var colorScheme
@Environment(\.inNavigationView) private var inNavigationView
#if os(iOS)
@Environment(\.inNavigationView) private var inNavigationView
@EnvironmentObject<PlayerModel> private var player
#endif
@EnvironmentObject<AccountsModel> private var accounts @EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
var items: [ContentItem] { var items: [ContentItem] {
ContentItem.array(of: store.item?.videos ?? []) ContentItem.array(of: store.item?.videos ?? [])
@ -54,6 +51,11 @@ struct ChannelPlaylistView: View {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title))) FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
.labelStyle(.iconOnly) .labelStyle(.iconOnly)
playButton
.labelStyle(.iconOnly)
shuffleButton
.labelStyle(.iconOnly)
} }
#endif #endif
VerticalCells(items: items) VerticalCells(items: items)
@ -70,7 +72,9 @@ struct ChannelPlaylistView: View {
resource?.addObserver(store) resource?.addObserver(store)
resource?.loadIfNeeded() resource?.loadIfNeeded()
} }
#if !os(tvOS) #if os(tvOS)
.background(Color.background(scheme: colorScheme))
#else
.toolbar { .toolbar {
ToolbarItem(placement: .navigation) { ToolbarItem(placement: .navigation) {
ShareButton( ShareButton(
@ -80,19 +84,50 @@ struct ChannelPlaylistView: View {
) )
} }
ToolbarItem { ToolbarItem(placement: playlistButtonsPlacement) {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title))) HStack {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
playButton
shuffleButton
}
} }
} }
.navigationTitle(playlist.title) .navigationTitle(playlist.title)
#if os(iOS) #if os(iOS)
.navigationBarHidden(player.playerNavigationLinkActive) .navigationBarHidden(player.playerNavigationLinkActive)
#endif #endif
#else
.background(Color.background(scheme: colorScheme))
#endif #endif
} }
private var playlistButtonsPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigationBarTrailing
#else
.automatic
#endif
}
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)
}
private var contentItem: ContentItem { private var contentItem: ContentItem {
ContentItem(playlist: playlist) ContentItem(playlist: playlist)
} }

View File

@ -4,25 +4,54 @@ import SwiftUI
struct PlaylistVideosView: View { struct PlaylistVideosView: View {
let playlist: Playlist let playlist: Playlist
var videos: [ContentItem] { @Environment(\.inNavigationView) private var inNavigationView
@EnvironmentObject<PlayerModel> private var player
var contentItems: [ContentItem] {
ContentItem.array(of: playlist.videos) ContentItem.array(of: playlist.videos)
} }
var videos: [Video] {
contentItems.compactMap(\.video)
}
init(_ playlist: Playlist) { init(_ playlist: Playlist) {
self.playlist = playlist self.playlist = playlist
} }
var body: some View { var body: some View {
PlayerControlsView { PlayerControlsView {
VerticalCells(items: videos) VerticalCells(items: contentItems)
#if !os(tvOS) #if !os(tvOS)
.navigationTitle("\(playlist.title) Playlist") .navigationTitle("\(playlist.title) Playlist")
#endif #endif
} }
.toolbar { .toolbar {
ToolbarItem { ToolbarItem(placement: playlistButtonsPlacement) {
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id))) HStack {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
Button {
player.play(videos, inNavigationView: inNavigationView)
} label: {
Label("Play All", systemImage: "play")
}
Button {
player.play(videos, shuffling: true, inNavigationView: inNavigationView)
} label: {
Label("Shuffle", systemImage: "shuffle")
}
}
} }
} }
} }
private var playlistButtonsPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigationBarTrailing
#else
.automatic
#endif
}
} }

View File

@ -68,9 +68,13 @@ struct NowPlayingView: View {
VideoBanner(video: item.video) VideoBanner(video: item.video)
} }
.contextMenu { .contextMenu {
Button("Delete", role: .destructive) { Button("Remove", role: .destructive) {
player.remove(item) player.remove(item)
} }
Button("Remove All", role: .destructive) {
player.removeQueueItems()
}
} }
} }
} }