Skip to content

Commit

Permalink
Added PlaylistID
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenTiigi committed Sep 28, 2023
1 parent 6bbba75 commit 6fbe03d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 18 deletions.
20 changes: 10 additions & 10 deletions Sources/API/YouTubePlayer+QueueingAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private extension YouTubePlayer {
struct LoadPlaylistParameter: Encodable {

/// The list
let list: String
let list: Source.PlaylistID

/// The ListType
let listType: Configuration.ListType
Expand Down Expand Up @@ -108,17 +108,17 @@ private extension YouTubePlayer {
startSeconds: startSeconds,
endSeconds: endSeconds
)
case .playlist(let id, let index, let startSeconds),
.channel(let id, let index, let startSeconds):
case .playlist(let id, let index, let startSeconds):
return LoadPlaylistParameter(
list: id,
listType: {
if case .playlist = source {
return .playlist
} else {
return .userUploads
}
}(),
listType: .playlist,
index: index,
startSeconds: startSeconds
)
case .channel(let id, let index, let startSeconds):
return LoadPlaylistParameter(
list: .init(id),
listType: .userUploads,
index: index,
startSeconds: startSeconds
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/Dictionary+json.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension Dictionary {
/// Make JSON String
/// - Parameter options: The JSONSerialization WritingOptions. Default value `.init()`
func jsonData(
options: JSONSerialization.WritingOptions = .init()
options: JSONSerialization.WritingOptions = [.withoutEscapingSlashes]
) throws -> Data {
try JSONSerialization.data(
withJSONObject: self,
Expand All @@ -24,7 +24,7 @@ extension Dictionary {
/// Make JSON String
/// - Parameter options: The JSONSerialization WritingOptions. Default value `.init()`
func jsonString(
options: JSONSerialization.WritingOptions = .init()
options: JSONSerialization.WritingOptions = [.withoutEscapingSlashes]
) throws -> String {
.init(
decoding: try JSONSerialization.data(
Expand Down
8 changes: 6 additions & 2 deletions Sources/Extensions/Encodable+json.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ extension Encodable {
/// - encoder: The JSONEncoder. Default value `.init()`
/// - options: The JSONSerialization WritingOptions. Default value `.init()`
func jsonString(
encoder: JSONEncoder = .init(),
options: JSONSerialization.WritingOptions = .init()
encoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.outputFormatting = [.withoutEscapingSlashes]
return encoder
}(),
options: JSONSerialization.WritingOptions = [.withoutEscapingSlashes]
) throws -> String {
try self
.json(encoder: encoder)
Expand Down
111 changes: 111 additions & 0 deletions Sources/Models/YouTubePlayer+Source+PlaylistID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import Foundation

// MARK: - YouTubePlayer+Source+PlaylistID

public extension YouTubePlayer.Source {

/// A playlist identifier.
enum PlaylistID: Hashable {
/// Playlist identifier.
case playlist(String)
/// An array of video identifiers.
case videos([String])
}

}

// MARK: - PlaylistID+init(playlist:)

public extension YouTubePlayer.Source.PlaylistID {

/// Creates a new instance of `YouTubePlayer.Source.PlaylistID`
/// - Parameter playlist: The playlist identifier
init(
_ playlist: String
) {
if case let videoIds = playlist.components(separatedBy: ","), videoIds.count > 1 {
self.init(videoIds)
} else {
self = .playlist(playlist)
}
}

}

// MARK: - PlaylistID+init(videos:)

public extension YouTubePlayer.Source.PlaylistID {

/// Creates a new instance of `YouTubePlayer.Source.PlaylistID`
/// - Parameter videos: An array of video identifiers
init(
_ videos: [String]
) {
self = .videos(videos)
}

}

// MARK: - ExpressibleByStringLiteral

extension YouTubePlayer.Source.PlaylistID: ExpressibleByStringLiteral {

/// Creates a new instance of `YouTubePlayer.Source.PlaylistID`
/// - Parameter playlist: The playlist identifier
public init(
stringLiteral playlist: String
) {
self.init(playlist)
}

}

// MARK: - ExpressibleByArrayLiteral

extension YouTubePlayer.Source.PlaylistID: ExpressibleByArrayLiteral {

/// Creates a new instance of `YouTubePlayer.Source.PlaylistID`
/// - Parameter videos: An array of video identifiers
public init(
arrayLiteral videos: String...
) {
self.init(videos)
}

}

// MARK: - Encodable

extension YouTubePlayer.Source.PlaylistID: Encodable {

/// Encode
/// - Parameter encoder: The encoder
public func encode(
to encoder: Encoder
) throws {
var container = encoder.singleValueContainer()
switch self {
case .playlist(let playlist):
try container.encode(playlist)
case .videos(let videos):
try container.encode(videos)
}
}

}

// MARK: - Identifiable

extension YouTubePlayer.Source.PlaylistID: Identifiable {

/// The identifier.
public var id: String {
switch self {
case .playlist(let playlist):
return playlist
case .videos(let videos):
return videos.joined(separator: ",")
}
}

}
7 changes: 4 additions & 3 deletions Sources/Models/YouTubePlayer+Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public extension YouTubePlayer {
)
/// Playlist
case playlist(
id: String,
id: PlaylistID,
index: Int? = nil,
startSeconds: Int? = nil
)
Expand All @@ -36,9 +36,10 @@ extension YouTubePlayer.Source: Identifiable {
public var id: String {
switch self {
case .video(let id, _, _),
.playlist(let id, _, _),
.channel(let id, _, _):
return id
case .playlist(let playlistID, _, _):
return playlistID.id
}
}

Expand Down Expand Up @@ -79,7 +80,7 @@ public extension YouTubePlayer.Source {
if let playlistId = urlComponents?.queryItems?["list"] {
// Return playlist source
return .playlist(
id: playlistId
id: .init(playlistId)
)
}
// Check if video id is available
Expand Down
9 changes: 8 additions & 1 deletion Sources/Options/YouTubePlayer+Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ extension YouTubePlayer.Options {
// Set playlist id
playerConfigurationJSON[
YouTubePlayer.Configuration.CodingKeys.list.rawValue
] = id
] = {
switch id {
case .playlist(let playlist):
return playlist
case .videos(let videos):
return videos
}
}()
case .channel(let name, _, _):
// Set user uploads
playerConfigurationJSON[
Expand Down

0 comments on commit 6fbe03d

Please sign in to comment.