mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
Playlists handling improvements
This commit is contained in:
parent
19bb4955a2
commit
1ca7b04e89
@ -13,6 +13,14 @@ final class NavigationModel: ObservableObject {
|
||||
case recentlyOpened(String)
|
||||
case nowPlaying
|
||||
case search
|
||||
|
||||
var playlistID: Playlist.ID? {
|
||||
if case let .playlist(id) = self {
|
||||
return id
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@Published var tabSelection: TabSelection! = .watchNow
|
||||
|
@ -5,8 +5,6 @@ import SwiftUI
|
||||
final class PlaylistsModel: ObservableObject {
|
||||
@Published var playlists = [Playlist]()
|
||||
|
||||
@Published var selectedPlaylistID: Playlist.ID = ""
|
||||
|
||||
var accounts = AccountsModel()
|
||||
|
||||
init(_ playlists: [Playlist] = [Playlist]()) {
|
||||
@ -32,9 +30,6 @@ final class PlaylistsModel: ObservableObject {
|
||||
.onSuccess { resource in
|
||||
if let playlists: [Playlist] = resource.typedContent() {
|
||||
self.playlists = playlists
|
||||
if self.selectedPlaylistID.isEmpty {
|
||||
self.selectPlaylist(self.all.first?.id)
|
||||
}
|
||||
onSuccess()
|
||||
}
|
||||
}
|
||||
@ -43,8 +38,8 @@ final class PlaylistsModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func addVideoToCurrentPlaylist(videoID: Video.ID, onSuccess: @escaping () -> Void = {}) {
|
||||
let resource = accounts.api.playlistVideos(currentPlaylist!.id)
|
||||
func addVideo(playlistID: Playlist.ID, videoID: Video.ID, onSuccess: @escaping () -> Void = {}) {
|
||||
let resource = accounts.api.playlistVideos(playlistID)
|
||||
let body = ["videoId": videoID]
|
||||
|
||||
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)
|
||||
|
||||
resource?.request(.delete).onSuccess { _ in
|
||||
@ -62,23 +57,7 @@ final class PlaylistsModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func selectPlaylist(_ id: String?) {
|
||||
selectedPlaylistID = id ?? ""
|
||||
}
|
||||
|
||||
private var resource: Resource? {
|
||||
accounts.api.playlists
|
||||
}
|
||||
|
||||
private var selectedPlaylist: Playlist? {
|
||||
guard !selectedPlaylistID.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return find(id: selectedPlaylistID)
|
||||
}
|
||||
|
||||
var currentPlaylist: Playlist? {
|
||||
selectedPlaylist ?? all.first
|
||||
}
|
||||
}
|
||||
|
@ -89,11 +89,7 @@ struct Video: Identifiable, Equatable, Hashable {
|
||||
}
|
||||
|
||||
func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
|
||||
if let url = thumbnails.first(where: { $0.quality == quality })?.url.absoluteString {
|
||||
return URL(string: url.replacingOccurrences(of: "hqdefault", with: quality.filename))
|
||||
}
|
||||
|
||||
return nil
|
||||
thumbnails.first { $0.quality == quality }?.url
|
||||
}
|
||||
|
||||
static func == (lhs: Video, rhs: Video) -> Bool {
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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,11 +29,12 @@ 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 {
|
||||
Group {
|
||||
#if os(tvOS)
|
||||
HorizontalCells(items: items)
|
||||
.padding(.top, 40)
|
||||
@ -40,6 +43,8 @@ struct PlaylistsView: View {
|
||||
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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user