Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Open playlist item at current time offset from page view offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Aug 13, 2021
1 parent ab80b28 commit 08a8a1a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand All @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion Client/Frontend/Browser/Playlist/PlaylistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -240,6 +256,10 @@ private class ListController: UIViewController {
fetchResults()
}

func initiatePlaybackOfLastPlayedItem() {
fetchResults()
}

// MARK: Internal

private func setTheme() {
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions Client/Frontend/Browser/PlaylistHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,20 @@ 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
if let error = error {
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)
}
}
})
}
Expand Down

0 comments on commit 08a8a1a

Please sign in to comment.