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

@ -13,6 +13,14 @@ final class NavigationModel: ObservableObject {
case recentlyOpened(String) case recentlyOpened(String)
case nowPlaying case nowPlaying
case search case search
var playlistID: Playlist.ID? {
if case let .playlist(id) = self {
return id
}
return nil
}
} }
@Published var tabSelection: TabSelection! = .watchNow @Published var tabSelection: TabSelection! = .watchNow

View File

@ -5,8 +5,6 @@ import SwiftUI
final class PlaylistsModel: ObservableObject { final class PlaylistsModel: ObservableObject {
@Published var playlists = [Playlist]() @Published var playlists = [Playlist]()
@Published var selectedPlaylistID: Playlist.ID = ""
var accounts = AccountsModel() var accounts = AccountsModel()
init(_ playlists: [Playlist] = [Playlist]()) { init(_ playlists: [Playlist] = [Playlist]()) {
@ -32,9 +30,6 @@ final class PlaylistsModel: ObservableObject {
.onSuccess { resource in .onSuccess { resource in
if let playlists: [Playlist] = resource.typedContent() { if let playlists: [Playlist] = resource.typedContent() {
self.playlists = playlists self.playlists = playlists
if self.selectedPlaylistID.isEmpty {
self.selectPlaylist(self.all.first?.id)
}
onSuccess() onSuccess()
} }
} }
@ -43,8 +38,8 @@ final class PlaylistsModel: ObservableObject {
} }
} }
func addVideoToCurrentPlaylist(videoID: Video.ID, onSuccess: @escaping () -> Void = {}) { func addVideo(playlistID: Playlist.ID, videoID: Video.ID, onSuccess: @escaping () -> Void = {}) {
let resource = accounts.api.playlistVideos(currentPlaylist!.id) let resource = accounts.api.playlistVideos(playlistID)
let body = ["videoId": videoID] let body = ["videoId": videoID]
resource?.request(.post, json: body).onSuccess { _ in resource?.request(.post, json: body).onSuccess { _ in
@ -53,7 +48,7 @@ final class PlaylistsModel: ObservableObject {
} }
} }
func removeVideoFromPlaylist(videoIndexID: String, playlistID: Playlist.ID, onSuccess: @escaping () -> Void = {}) { func removeVideo(videoIndexID: String, playlistID: Playlist.ID, onSuccess: @escaping () -> Void = {}) {
let resource = accounts.api.playlistVideo(playlistID, videoIndexID) let resource = accounts.api.playlistVideo(playlistID, videoIndexID)
resource?.request(.delete).onSuccess { _ in resource?.request(.delete).onSuccess { _ in
@ -62,23 +57,7 @@ final class PlaylistsModel: ObservableObject {
} }
} }
func selectPlaylist(_ id: String?) {
selectedPlaylistID = id ?? ""
}
private var resource: Resource? { private var resource: Resource? {
accounts.api.playlists accounts.api.playlists
} }
private var selectedPlaylist: Playlist? {
guard !selectedPlaylistID.isEmpty else {
return nil
}
return find(id: selectedPlaylistID)
}
var currentPlaylist: Playlist? {
selectedPlaylist ?? all.first
}
} }

View File

@ -89,11 +89,7 @@ struct Video: Identifiable, Equatable, Hashable {
} }
func thumbnailURL(quality: Thumbnail.Quality) -> URL? { func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
if let url = thumbnails.first(where: { $0.quality == quality })?.url.absoluteString { thumbnails.first { $0.quality == quality }?.url
return URL(string: url.replacingOccurrences(of: "hqdefault", with: quality.filename))
}
return nil
} }
static func == (lhs: Video, rhs: Video) -> Bool { static func == (lhs: Video, rhs: Video) -> Bool {

View File

@ -17,6 +17,10 @@ private struct NavigationStyleKey: EnvironmentKey {
static let defaultValue = NavigationStyle.tab static let defaultValue = NavigationStyle.tab
} }
private struct CurrentPlaylistID: EnvironmentKey {
static let defaultValue: String? = nil
}
extension EnvironmentValues { extension EnvironmentValues {
var inNavigationView: Bool { var inNavigationView: Bool {
get { self[InNavigationViewKey.self] } get { self[InNavigationViewKey.self] }
@ -32,4 +36,9 @@ extension EnvironmentValues {
get { self[NavigationStyleKey.self] } get { self[NavigationStyleKey.self] }
set { self[NavigationStyleKey.self] = newValue } 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 import SwiftUI
struct AddToPlaylistView: View { struct AddToPlaylistView: View {
@EnvironmentObject<PlaylistsModel> private var model
let video: Video let video: Video
@State private var selectedPlaylistID: Playlist.ID = ""
@Environment(\.dismiss) private var dismiss @Environment(\.dismiss) private var dismiss
@EnvironmentObject<PlaylistsModel> private var model
var body: some View { var body: some View {
Group { Group {
@ -27,7 +28,7 @@ struct AddToPlaylistView: View {
.onAppear { .onAppear {
model.load { model.load {
if let playlist = model.all.first { if let playlist = model.all.first {
model.selectedPlaylistID = playlist.id selectedPlaylistID = playlist.id
} }
} }
} }
@ -86,7 +87,7 @@ struct AddToPlaylistView: View {
#if os(tvOS) #if os(tvOS)
selectPlaylistButton selectPlaylistButton
#else #else
Picker("Playlist", selection: $model.selectedPlaylistID) { Picker("Playlist", selection: $selectedPlaylistID) {
ForEach(model.all) { playlist in ForEach(model.all) { playlist in
Text(playlist.title).tag(playlist.id) Text(playlist.title).tag(playlist.id)
} }
@ -119,24 +120,24 @@ struct AddToPlaylistView: View {
#if !os(tvOS) #if !os(tvOS)
.keyboardShortcut(.defaultAction) .keyboardShortcut(.defaultAction)
#endif #endif
.disabled(model.currentPlaylist.isNil) .disabled(currentPlaylist.isNil)
.padding(.top, 30) .padding(.top, 30)
} }
.padding(.horizontal) .padding(.horizontal)
} }
private var selectPlaylistButton: some View { private var selectPlaylistButton: some View {
Button(model.currentPlaylist?.title ?? "Select playlist") { Button(currentPlaylist?.title ?? "Select playlist") {
guard model.currentPlaylist != nil else { guard currentPlaylist != nil else {
return return
} }
model.selectedPlaylistID = model.all.next(after: model.currentPlaylist!)!.id selectedPlaylistID = model.all.next(after: currentPlaylist!)!.id
} }
.contextMenu { .contextMenu {
ForEach(model.all) { playlist in ForEach(model.all) { playlist in
Button(playlist.title) { Button(playlist.title) {
model.selectedPlaylistID = playlist.id selectedPlaylistID = playlist.id
} }
} }
@ -145,14 +146,18 @@ struct AddToPlaylistView: View {
} }
private func addToPlaylist() { private func addToPlaylist() {
guard model.currentPlaylist != nil else { guard currentPlaylist != nil else {
return return
} }
model.addVideoToCurrentPlaylist(videoID: video.id) { model.addVideo(playlistID: currentPlaylist!.id, videoID: video.videoID) {
dismiss() dismiss()
} }
} }
private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first
}
} }
struct AddToPlaylistView_Previews: PreviewProvider { struct AddToPlaylistView_Previews: PreviewProvider {

View File

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

View File

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