Add YouTube scrubbing thumbnails

This commit is contained in:
Ilya Laktyushin
2020-05-02 19:02:03 +04:00
parent 890db9606c
commit 9ae27ffde6
14 changed files with 378 additions and 44 deletions

View File

@@ -8,7 +8,7 @@ import SyncCore
import UniversalMediaPlayer
protocol WebEmbedImplementation {
func setup(_ webView: WKWebView, userContentController: WKUserContentController, evaluateJavaScript: @escaping (String) -> Void, updateStatus: @escaping (MediaPlayerStatus) -> Void, onPlaybackStarted: @escaping () -> Void)
func setup(_ webView: WKWebView, userContentController: WKUserContentController, evaluateJavaScript: @escaping (String, ((Any?) -> Void)?) -> Void, updateStatus: @escaping (MediaPlayerStatus) -> Void, onPlaybackStarted: @escaping () -> Void)
func play()
func pause()
@@ -73,7 +73,7 @@ final class WebEmbedPlayerNode: ASDisplayNode, WKNavigationDelegate {
return self.readyValue.get()
}
private let impl: WebEmbedImplementation
let impl: WebEmbedImplementation
private let intrinsicDimensions: CGSize
private let webView: WKWebView
@@ -118,8 +118,8 @@ final class WebEmbedPlayerNode: ASDisplayNode, WKNavigationDelegate {
}
self.view.addSubview(self.webView)
self.impl.setup(self.webView, userContentController: userContentController, evaluateJavaScript: { [weak self] js in
self?.evaluateJavaScript(js: js)
self.impl.setup(self.webView, userContentController: userContentController, evaluateJavaScript: { [weak self] js, completion in
self?.evaluateJavaScript(js: js, completion: completion)
}, updateStatus: { [weak self] status in
self?.statusValue.set(status)
}, onPlaybackStarted: { [weak self] in
@@ -168,12 +168,15 @@ final class WebEmbedPlayerNode: ASDisplayNode, WKNavigationDelegate {
}
}
private func evaluateJavaScript(js: String) {
private func evaluateJavaScript(js: String, completion: ((Any?) -> Void)?) {
self.queue.async { [weak self] in
if let strongSelf = self {
let impl = {
strongSelf.webView.evaluateJavaScript(js, completionHandler: { (_, _) in
strongSelf.webView.evaluateJavaScript(js, completionHandler: { (result, _) in
strongSelf.semaphore.signal()
if let completion = completion {
completion(result)
}
})
}