-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathYouTubePlayerView.swift
150 lines (122 loc) · 4.14 KB
/
YouTubePlayerView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import SwiftUI
// MARK: - YouTubePlayerView
/// The YouTube player SwiftUI view.
public struct YouTubePlayerView<Overlay: View> {
// MARK: Properties
/// The ``YouTubePlayer``.
public let player: YouTubePlayer
/// The The transaction to use when the ``YouTubePlayer/State`` changes.
public let transaction: Transaction
/// A closure which constructs the `Overlay` for a given state.
public let overlay: (YouTubePlayer.State) -> Overlay
/// The current ``YouTubePlayer/State``
@State
private var state: YouTubePlayer.State = .idle
// MARK: Initializer
/// Creates a new instance of ``YouTubePlayerView``
/// - Parameters:
/// - player: The ``YouTubePlayer``.
/// - transaction: The transaction to use when the state changes. Default value `.init()`
/// - overlay: A view builder closure to construct an `Overlay` for the given state.
public init(
_ player: YouTubePlayer,
transaction: Transaction = .init(),
@ViewBuilder
overlay: @escaping (YouTubePlayer.State) -> Overlay
) {
self.player = player
self.transaction = transaction
self.overlay = overlay
}
}
// MARK: - View
extension YouTubePlayerView: View {
/// The content and behavior of the view.
public var body: some View {
YouTubePlayerWebView.Representable(
player: self.player
)
.overlay(
self.overlay(self.state)
)
.onReceive(
self.player.statePublisher
) { state in
withTransaction(self.transaction) {
self.state = state
}
}
}
}
// MARK: - YouTubePlayerWebView+Representable
private extension YouTubePlayerWebView {
#if !os(macOS)
/// The YouTubePlayer UIView SwiftUI Representable
struct Representable: UIViewRepresentable {
/// The YouTube Player
let player: YouTubePlayer
/// Make YouTubePlayerWebView
/// - Parameter context: The Context
/// - Returns: The YouTubePlayerWebView
func makeUIView(
context: Context
) -> YouTubePlayerWebView {
self.player.webView
}
/// Update YouTubePlayerWebView
/// - Parameters:
/// - playerWebView: The YouTubePlayerWebView
/// - context: The Context
func updateUIView(
_ playerWebView: YouTubePlayerWebView,
context: Context
) {}
/// Dismantle YouTubePlayerWebView
/// - Parameters:
/// - playerWebView: The YouTubePlayerWebView
/// - coordinator: The Coordinaotr
static func dismantleUIView(
_ playerWebView: YouTubePlayerWebView,
coordinator: Void
) {
Task { [weak playerWebView] in
try? await playerWebView?.player?.pause()
}
}
}
#else
/// The YouTubePlayer NSView SwiftUI Representable
struct Representable: NSViewRepresentable {
/// The YouTube Player
let player: YouTubePlayer
/// Make YouTubePlayerWebView
/// - Parameter context: The Context
/// - Returns: The YouTubePlayerWebView
func makeNSView(
context: Context
) -> YouTubePlayerWebView {
self.player.webView
}
/// Update YouTubePlayerWebView
/// - Parameters:
/// - playerWebView: The YouTubePlayerWebView
/// - context: The Context
func updateNSView(
_ playerWebView: YouTubePlayerWebView,
context: Context
) {}
/// Dismantle YouTubePlayerWebView
/// - Parameters:
/// - playerWebView: The YouTubePlayerWebView
/// - coordinator: The Coordinaotr
static func dismantleNSView(
_ playerWebView: YouTubePlayerWebView,
coordinator: Void
) {
Task { [weak playerWebView] in
try? await playerWebView?.player?.pause()
}
}
}
#endif
}