-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
298 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
Sources/Models/JavaScript/YouTubePlayer+JavaScriptEvent+Data.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import Foundation | ||
|
||
// MARK: - YouTubePlayer+JavaScriptEvent+Data | ||
|
||
public extension YouTubePlayer.JavaScriptEvent { | ||
|
||
/// A YouTube player JavaScript event data payload. | ||
struct Data: Hashable, Sendable { | ||
|
||
// MARK: Properties | ||
|
||
/// The value. | ||
public let value: String | ||
|
||
// MARK: Initializer | ||
|
||
/// Creates a new instance of ``YouTubePlayer.JavaScriptEvent.Data`` | ||
/// - Parameter value: The value | ||
public init( | ||
value: String | ||
) { | ||
self.value = value | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
// MARK: - Convenience Initializer | ||
|
||
public extension YouTubePlayer.JavaScriptEvent.Data { | ||
|
||
/// Creates a new instance of ``YouTubePlayer.JavaScriptEvent.Data`` | ||
/// - Parameter value: The value | ||
init?( | ||
urlQueryItem: URLQueryItem | ||
) { | ||
// Verify value of query item is available and is not empty and not equal to "null" | ||
guard let value = urlQueryItem.value?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
!value.isEmpty, | ||
value.lowercased() != "null" else { | ||
// Otherwise return out of function | ||
return nil | ||
} | ||
// Initialize with value | ||
self.init(value: value) | ||
} | ||
|
||
} | ||
|
||
// MARK: - Codable | ||
|
||
extension YouTubePlayer.JavaScriptEvent.Data: Codable { | ||
|
||
/// Creates a new instance of ``YouTubePlayer.JavaScriptEvent.Data`` | ||
/// - Parameter decoder: The decoder. | ||
public init( | ||
from decoder: Decoder | ||
) throws { | ||
let container = try decoder.singleValueContainer() | ||
self.init( | ||
value: try container.decode(String.self) | ||
) | ||
} | ||
|
||
/// Encode. | ||
/// - Parameter encoder: The encoder. | ||
public func encode( | ||
to encoder: Encoder | ||
) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(self.value) | ||
} | ||
|
||
} | ||
|
||
extension YouTubePlayer.JavaScriptEvent.Data: CustomStringConvertible { | ||
|
||
public var description: String { | ||
self.value | ||
} | ||
|
||
} | ||
|
||
public extension YouTubePlayer.JavaScriptEvent.Data { | ||
|
||
func value<T: LosslessStringConvertible>( | ||
as type: T.Type | ||
) -> T? { | ||
.init(self.value) | ||
} | ||
|
||
} | ||
|
||
public extension YouTubePlayer.JavaScriptEvent.Data { | ||
|
||
func jsonValue<D: Decodable>( | ||
as type: D.Type, | ||
jsonDecoder: JSONDecoder = .init() | ||
) throws -> D { | ||
try jsonDecoder.decode( | ||
D.self, | ||
from: .init(self.value.utf8) | ||
) | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
Sources/Models/JavaScript/YouTubePlayer+JavaScriptEvent+Name.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Foundation | ||
|
||
// MARK: - Name | ||
|
||
public extension YouTubePlayer.JavaScriptEvent { | ||
|
||
/// The YouTubePlayer JavaScriptEvent Name | ||
enum Name: String, Codable, Hashable, Sendable, CaseIterable { | ||
/// iFrame Api is ready | ||
case onIframeApiReady | ||
/// iFrame Api failed to load | ||
case onIframeApiFailedToLoad | ||
/// Error | ||
case onError | ||
/// Ready | ||
case onReady | ||
/// API Change | ||
case onApiChange | ||
/// Autoplay blocked | ||
case onAutoplayBlocked | ||
/// State change | ||
case onStateChange | ||
/// Playback quality change | ||
case onPlaybackQualityChange | ||
/// Playback rate change | ||
case onPlaybackRateChange | ||
/// Fullscreen change | ||
case onFullscreenChange | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import Foundation | ||
|
||
// MARK: - YouTubePlayer | ||
|
||
public extension YouTubePlayer { | ||
|
||
/// A YouTube player fullscreen state. | ||
struct FullscreenState: Hashable, Sendable { | ||
|
||
// MARK: Properties | ||
|
||
/// A Boolean indicating whether it is in fullscreen or not. | ||
public let isFullscreen: Bool | ||
|
||
/// The video identifier. | ||
public let videoID: String? | ||
|
||
/// The time. | ||
public let time: Measurement<UnitDuration>? | ||
|
||
// MARK: Initializer | ||
|
||
/// Creates a new instance of ``YouTubePlayer.FullscreenState`` | ||
/// - Parameters: | ||
/// - isFullscreen: A Boolean indicating whether it is in fullscreen or not. | ||
/// - videoID: The video identifier. Default value `nil` | ||
/// - time: The time. Default value `nil` | ||
public init( | ||
isFullscreen: Bool, | ||
videoID: String? = nil, | ||
time: Measurement<UnitDuration>? = nil | ||
) { | ||
self.isFullscreen = isFullscreen | ||
self.videoID = videoID | ||
self.time = time | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
// MARK: - Codable | ||
|
||
extension YouTubePlayer.FullscreenState: Codable { | ||
|
||
/// The coding keys. | ||
private enum CodingKeys: String, CodingKey { | ||
case isFullscreen = "fullscreen" | ||
case videoID = "videoId" | ||
case time | ||
} | ||
|
||
/// Creates a new instance of ``YouTubePlayer.FullscreenState`` | ||
/// - Parameter decoder: The decoder. | ||
public init( | ||
from decoder: Decoder | ||
) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
try self.init( | ||
isFullscreen: container.decode(Bool.self, forKey: .isFullscreen), | ||
videoID: container.decodeIfPresent(String.self, forKey: .videoID), | ||
time: container.decodeIfPresent(Double.self, forKey: .time).flatMap { .init(value: $0, unit: .seconds) } | ||
) | ||
} | ||
|
||
/// Encode. | ||
/// - Parameter encoder: The encoder. | ||
public func encode( | ||
to encoder: Encoder | ||
) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.isFullscreen, forKey: .isFullscreen) | ||
try container.encode(self.videoID, forKey: .videoID) | ||
try container.encode(self.time?.converted(to: .seconds).value, forKey: .time) | ||
} | ||
|
||
} | ||
|
||
// MARK: - ExpressibleByBooleanLiteral | ||
|
||
extension YouTubePlayer.FullscreenState: ExpressibleByBooleanLiteral { | ||
|
||
/// Creates a new instance of ``YouTubePlayer.FullscreenState`` | ||
/// - Parameter isFullscreen: A Boolean indicating whether it is in fullscreen or not. | ||
public init( | ||
booleanLiteral isFullscreen: Bool | ||
) { | ||
self.init( | ||
isFullscreen: isFullscreen | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.