diff --git a/Client/Frontend/Browser/BrowserViewController/BrowserViewController+Menu.swift b/Client/Frontend/Browser/BrowserViewController/BrowserViewController+Menu.swift index a82578bc7d9..d1b25635afe 100644 --- a/Client/Frontend/Browser/BrowserViewController/BrowserViewController+Menu.swift +++ b/Client/Frontend/Browser/BrowserViewController/BrowserViewController+Menu.swift @@ -92,7 +92,13 @@ extension BrowserViewController { } } else { browserViewController.dismiss(animated: true) { - browserViewController.openPlaylist() + if let webView = browserViewController.tabManager.selectedTab?.webView { + PlaylistHelper.getCurrentTime(webView: webView, nodeTag: item.tagId) { [weak browserViewController] currentTime in + browserViewController?.openPlaylist(item: item, playbackOffset: currentTime) + } + } else { + browserViewController.openPlaylist(item: item, playbackOffset: 0.0) + } } } } diff --git a/Client/Frontend/Browser/Playlist/BrowserViewController+Playlist.swift b/Client/Frontend/Browser/Playlist/BrowserViewController+Playlist.swift index dbf440a4d20..308c8a15458 100644 --- a/Client/Frontend/Browser/Playlist/BrowserViewController+Playlist.swift +++ b/Client/Frontend/Browser/Playlist/BrowserViewController+Playlist.swift @@ -99,7 +99,13 @@ extension BrowserViewController: PlaylistHelperDelegate { self.dismiss(animated: true) { DispatchQueue.main.async { - self.openPlaylist() + if let webView = tab?.webView { + PlaylistHelper.getCurrentTime(webView: webView, nodeTag: item.tagId) { [weak self] currentTime in + self?.openPlaylist(item: item, playbackOffset: currentTime) + } + } else { + self.openPlaylist(item: item, playbackOffset: 0.0) + } } } } @@ -121,16 +127,25 @@ extension BrowserViewController: PlaylistHelperDelegate { popover.present(from: topToolbar.locationView.playlistButton, on: self) } - func openPlaylist() { - let playlistController = (UIApplication.shared.delegate as? AppDelegate)?.playlistRestorationController ?? PlaylistViewController() + func openPlaylist(item: PlaylistInfo?, playbackOffset: Double) { + let isRestoredController = (UIApplication.shared.delegate as? AppDelegate)?.playlistRestorationController != nil + + let playlistController = (UIApplication.shared.delegate as? AppDelegate)?.playlistRestorationController as? PlaylistViewController ?? PlaylistViewController() playlistController.modalPresentationStyle = .fullScreen + playlistController.initialItem = item + playlistController.initialItemPlaybackOffset = playbackOffset + /// Donate Open Playlist Activity for suggestions let openPlaylist = ActivityShortcutManager.shared.createShortcutActivity(type: .openPlayList) self.userActivity = openPlaylist openPlaylist.becomeCurrent() - present(playlistController, animated: true) + present(playlistController, animated: true) { + if isRestoredController { + playlistController.initiatePlaybackOfLastPlayedItem() + } + } } func addToPlayListActivity(info: PlaylistInfo?, itemDetected: Bool) { diff --git a/Client/Frontend/Browser/Playlist/PlaylistViewController.swift b/Client/Frontend/Browser/Playlist/PlaylistViewController.swift index df7b2abcc1b..e18094b5f17 100644 --- a/Client/Frontend/Browser/Playlist/PlaylistViewController.swift +++ b/Client/Frontend/Browser/Playlist/PlaylistViewController.swift @@ -28,14 +28,30 @@ private enum DisplayMode { class PlaylistViewController: UIViewController { // MARK: Properties + + var initialItem: PlaylistInfo? + var initialItemPlaybackOffset = 0.0 private let splitController = UISplitViewController() private let listController = ListController() private let detailController = DetailController() + func initiatePlaybackOfLastPlayedItem() { + if let initialItem = initialItem { + Preferences.Playlist.lastPlayedItemUrl.value = initialItem.pageSrc + Preferences.Playlist.lastPlayedItemTime.value = initialItemPlaybackOffset + listController.initiatePlaybackOfLastPlayedItem() + } + } + override func viewDidLoad() { super.viewDidLoad() + if let initialItem = initialItem { + Preferences.Playlist.lastPlayedItemUrl.value = initialItem.pageSrc + Preferences.Playlist.lastPlayedItemTime.value = initialItemPlaybackOffset + } + overrideUserInterfaceStyle = .dark splitController.do { @@ -240,6 +256,10 @@ private class ListController: UIViewController { fetchResults() } + func initiatePlaybackOfLastPlayedItem() { + fetchResults() + } + // MARK: Internal private func setTheme() { @@ -625,7 +645,8 @@ extension ListController: UITableViewDataSource { mimeType: item.mimeType, duration: duration.seconds, detected: item.detected, - dateAdded: item.dateAdded) + dateAdded: item.dateAdded, + tagId: item.tagId) PlaylistItem.updateItem(newItem) { completion(duration.seconds, asset) diff --git a/Client/Frontend/Browser/PlaylistHelper.swift b/Client/Frontend/Browser/PlaylistHelper.swift index c317a154093..c4b06568ae4 100644 --- a/Client/Frontend/Browser/PlaylistHelper.swift +++ b/Client/Frontend/Browser/PlaylistHelper.swift @@ -202,7 +202,7 @@ extension PlaylistHelper: UIGestureRecognizerDelegate { extension PlaylistHelper { static func getCurrentTime(webView: WKWebView, nodeTag: String, completion: @escaping (Double) -> Void) { let token = UserScriptManager.securityTokenString - let javascript = String(format: "window.mediaCurrentTimeFromTag_%@('%s')", token, nodeTag) + let javascript = String(format: "window.mediaCurrentTimeFromTag_%@('%@')", token, nodeTag) // swiftlint:disable:next safe_javascript webView.evaluateJavaScript(javascript, completionHandler: { value, error in @@ -210,10 +210,12 @@ extension PlaylistHelper { log.error("Error Retrieving Playlist Page Media Current Time: \(error)") } - if let value = value as? Double { - completion(value) - } else { - completion(0.0) + DispatchQueue.main.async { + if let value = value as? Double { + completion(value) + } else { + completion(0.0) + } } }) }