Playlists handling improvements

This commit is contained in:
Arkadiusz Fal
2021-10-24 23:36:24 +02:00
parent 19bb4955a2
commit 1ca7b04e89
7 changed files with 81 additions and 76 deletions

View File

@@ -17,6 +17,10 @@ private struct NavigationStyleKey: EnvironmentKey {
static let defaultValue = NavigationStyle.tab
}
private struct CurrentPlaylistID: EnvironmentKey {
static let defaultValue: String? = nil
}
extension EnvironmentValues {
var inNavigationView: Bool {
get { self[InNavigationViewKey.self] }
@@ -32,4 +36,9 @@ extension EnvironmentValues {
get { self[NavigationStyleKey.self] }
set { self[NavigationStyleKey.self] = newValue }
}
var currentPlaylistID: String? {
get { self[CurrentPlaylistID.self] }
set { self[CurrentPlaylistID.self] = newValue }
}
}

View File

@@ -3,11 +3,12 @@ import Siesta
import SwiftUI
struct AddToPlaylistView: View {
@EnvironmentObject<PlaylistsModel> private var model
let video: Video
@State private var selectedPlaylistID: Playlist.ID = ""
@Environment(\.dismiss) private var dismiss
@EnvironmentObject<PlaylistsModel> private var model
var body: some View {
Group {
@@ -27,7 +28,7 @@ struct AddToPlaylistView: View {
.onAppear {
model.load {
if let playlist = model.all.first {
model.selectedPlaylistID = playlist.id
selectedPlaylistID = playlist.id
}
}
}
@@ -86,7 +87,7 @@ struct AddToPlaylistView: View {
#if os(tvOS)
selectPlaylistButton
#else
Picker("Playlist", selection: $model.selectedPlaylistID) {
Picker("Playlist", selection: $selectedPlaylistID) {
ForEach(model.all) { playlist in
Text(playlist.title).tag(playlist.id)
}
@@ -119,24 +120,24 @@ struct AddToPlaylistView: View {
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
.disabled(model.currentPlaylist.isNil)
.disabled(currentPlaylist.isNil)
.padding(.top, 30)
}
.padding(.horizontal)
}
private var selectPlaylistButton: some View {
Button(model.currentPlaylist?.title ?? "Select playlist") {
guard model.currentPlaylist != nil else {
Button(currentPlaylist?.title ?? "Select playlist") {
guard currentPlaylist != nil else {
return
}
model.selectedPlaylistID = model.all.next(after: model.currentPlaylist!)!.id
selectedPlaylistID = model.all.next(after: currentPlaylist!)!.id
}
.contextMenu {
ForEach(model.all) { playlist in
Button(playlist.title) {
model.selectedPlaylistID = playlist.id
selectedPlaylistID = playlist.id
}
}
@@ -145,14 +146,18 @@ struct AddToPlaylistView: View {
}
private func addToPlaylist() {
guard model.currentPlaylist != nil else {
guard currentPlaylist != nil else {
return
}
model.addVideoToCurrentPlaylist(videoID: video.id) {
model.addVideo(playlistID: currentPlaylist!.id, videoID: video.videoID) {
dismiss()
}
}
private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first
}
}
struct AddToPlaylistView_Previews: PreviewProvider {

View File

@@ -3,6 +3,8 @@ import Siesta
import SwiftUI
struct PlaylistsView: View {
@State private var selectedPlaylistID: Playlist.ID = ""
@State private var showingNewPlaylist = false
@State private var createdPlaylist: Playlist?
@@ -16,7 +18,7 @@ struct PlaylistsView: View {
@Namespace private var focusNamespace
var items: [ContentItem] {
ContentItem.array(of: model.currentPlaylist?.videos ?? [])
ContentItem.array(of: currentPlaylist?.videos ?? [])
}
var body: some View {
@@ -27,18 +29,21 @@ struct PlaylistsView: View {
toolbar
#endif
if model.currentPlaylist != nil, items.isEmpty {
if currentPlaylist != nil, items.isEmpty {
hintText("Playlist is empty\n\nTap and hold on a video and then tap \"Add to Playlist\"")
} else if model.all.isEmpty {
hintText("You have no playlists\n\nTap on \"New Playlist\" to create one")
} else {
#if os(tvOS)
HorizontalCells(items: items)
.padding(.top, 40)
Spacer()
#else
VerticalCells(items: items)
#endif
Group {
#if os(tvOS)
HorizontalCells(items: items)
.padding(.top, 40)
Spacer()
#else
VerticalCells(items: items)
#endif
}
.environment(\.currentPlaylistID, currentPlaylist?.id)
}
}
}
@@ -66,7 +71,7 @@ struct PlaylistsView: View {
.prefersDefaultFocus(in: focusNamespace)
}
if model.currentPlaylist != nil {
if currentPlaylist != nil {
editPlaylistButton
}
#endif
@@ -88,7 +93,7 @@ struct PlaylistsView: View {
Spacer()
if model.currentPlaylist != nil {
if currentPlaylist != nil {
editPlaylistButton
}
}
@@ -130,7 +135,7 @@ struct PlaylistsView: View {
}
}
if model.currentPlaylist != nil {
if currentPlaylist != nil {
editPlaylistButton
}
@@ -163,7 +168,7 @@ struct PlaylistsView: View {
model.load(force: true) {
if let id = createdPlaylist?.id {
self.model.selectPlaylist(id)
selectedPlaylistID = id
}
self.createdPlaylist = nil
@@ -172,11 +177,11 @@ struct PlaylistsView: View {
func selectEditedPlaylist() {
if editedPlaylist.isNil {
model.selectPlaylist(nil)
selectedPlaylistID = ""
}
model.load(force: true) {
model.selectPlaylist(editedPlaylist?.id)
self.selectedPlaylistID = editedPlaylist?.id ?? ""
self.editedPlaylist = nil
}
@@ -184,27 +189,27 @@ struct PlaylistsView: View {
var selectPlaylistButton: some View {
#if os(tvOS)
Button(model.currentPlaylist?.title ?? "Select playlist") {
guard model.currentPlaylist != nil else {
Button(currentPlaylist?.title ?? "Select playlist") {
guard currentPlaylist != nil else {
return
}
model.selectPlaylist(model.all.next(after: model.currentPlaylist!)?.id)
selectedPlaylistID = model.all.next(after: currentPlaylist!)?.id ?? ""
}
.contextMenu {
ForEach(model.all) { playlist in
Button(playlist.title) {
model.selectPlaylist(playlist.id)
selectedPlaylistID = playlist.id
}
}
Button("Cancel", role: .cancel) {}
}
#else
Menu(model.currentPlaylist?.title ?? "Select playlist") {
Menu(currentPlaylist?.title ?? "Select playlist") {
ForEach(model.all) { playlist in
Button(action: { model.selectPlaylist(playlist.id) }) {
if playlist == model.currentPlaylist {
Button(action: { selectedPlaylistID = playlist.id }) {
if playlist == currentPlaylist {
Label(playlist.title, systemImage: "checkmark")
} else {
Text(playlist.title)
@@ -217,7 +222,7 @@ struct PlaylistsView: View {
var editPlaylistButton: some View {
Button(action: {
self.editedPlaylist = self.model.currentPlaylist
self.editedPlaylist = self.currentPlaylist
self.showingEditPlaylist = true
}) {
HStack(spacing: 8) {
@@ -237,6 +242,10 @@ struct PlaylistsView: View {
}
}
}
private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first
}
}
struct PlaylistsView_Provider: PreviewProvider {

View File

@@ -8,6 +8,7 @@ struct VideoContextMenuView: View {
@Environment(\.inNavigationView) private var inNavigationView
@Environment(\.navigationStyle) private var navigationStyle
@Environment(\.currentPlaylistID) private var playlistID
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@@ -20,6 +21,7 @@ struct VideoContextMenuView: View {
Section {
playNowButton
}
Section {
playNextButton
addToQueueButton
@@ -27,6 +29,7 @@ struct VideoContextMenuView: View {
Section {
openChannelButton
if accounts.app.supportsSubscriptions {
subscriptionButton
}
@@ -34,13 +37,9 @@ struct VideoContextMenuView: View {
if accounts.app.supportsUserPlaylists {
Section {
if navigation.tabSelection != .playlists {
addToPlaylistButton
} else if let playlist = playlists.currentPlaylist {
removeFromPlaylistButton(playlistID: playlist.id)
}
addToPlaylistButton
if case let .playlist(id) = navigation.tabSelection {
if let id = navigation.tabSelection?.playlistID ?? playlistID {
removeFromPlaylistButton(playlistID: id)
}
}
@@ -51,7 +50,7 @@ struct VideoContextMenuView: View {
#endif
}
var playNowButton: some View {
private var playNowButton: some View {
Button {
player.playNow(video)
@@ -65,7 +64,7 @@ struct VideoContextMenuView: View {
}
}
var playNextButton: some View {
private var playNextButton: some View {
Button {
player.playNext(video)
} label: {
@@ -73,7 +72,7 @@ struct VideoContextMenuView: View {
}
}
var addToQueueButton: some View {
private var addToQueueButton: some View {
Button {
player.enqueueVideo(video)
} label: {
@@ -81,7 +80,7 @@ struct VideoContextMenuView: View {
}
}
var openChannelButton: some View {
private var openChannelButton: some View {
Button {
let recent = RecentItem(from: video.channel)
recents.add(recent)
@@ -96,7 +95,7 @@ struct VideoContextMenuView: View {
}
}
var subscriptionButton: some View {
private var subscriptionButton: some View {
Group {
if subscriptions.isSubscribing(video.channel.id) {
Button(role: .destructive) {
@@ -120,7 +119,7 @@ struct VideoContextMenuView: View {
}
}
var addToPlaylistButton: some View {
private var addToPlaylistButton: some View {
Button {
navigation.presentAddToPlaylist(video)
} label: {
@@ -130,7 +129,7 @@ struct VideoContextMenuView: View {
func removeFromPlaylistButton(playlistID: String) -> some View {
Button(role: .destructive) {
playlists.removeVideoFromPlaylist(videoIndexID: video.indexID!, playlistID: playlistID)
playlists.removeVideo(videoIndexID: video.indexID!, playlistID: playlistID)
} label: {
Label("Remove from playlist", systemImage: "text.badge.minus")
}