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

Commit

Permalink
Added back toast!
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Aug 19, 2021
1 parent 2e66cf2 commit f6863f0
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@
CA439A5925E6F29D00FE9150 /* VideoPlayerInfoBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA439A5825E6F29D00FE9150 /* VideoPlayerInfoBar.swift */; };
CA439A7625E8054A00FE9150 /* VideoPlayerControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA439A7525E8054A00FE9150 /* VideoPlayerControlsView.swift */; };
CA439A9025E80EE400FE9150 /* VideoPlayerTrackbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA439A8F25E80EE400FE9150 /* VideoPlayerTrackbar.swift */; };
CA752EA526CEABF8009356EF /* PlaylistToast.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA752EA426CEABF8009356EF /* PlaylistToast.swift */; };
CA9A22FE26A71ADA00923D70 /* PlaylistPopoverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA9A22FD26A71ADA00923D70 /* PlaylistPopoverView.swift */; };
CA9A230026A7370C00923D70 /* FontScaling.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA9A22FF26A7370C00923D70 /* FontScaling.swift */; };
CA9A233426B97B4300923D70 /* PlaylistMenuButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA9A233326B97B4300923D70 /* PlaylistMenuButton.swift */; };
Expand Down Expand Up @@ -2553,6 +2554,7 @@
CA439A5825E6F29D00FE9150 /* VideoPlayerInfoBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoPlayerInfoBar.swift; sourceTree = "<group>"; };
CA439A7525E8054A00FE9150 /* VideoPlayerControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoPlayerControlsView.swift; sourceTree = "<group>"; };
CA439A8F25E80EE400FE9150 /* VideoPlayerTrackbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoPlayerTrackbar.swift; sourceTree = "<group>"; };
CA752EA426CEABF8009356EF /* PlaylistToast.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistToast.swift; sourceTree = "<group>"; };
CA9A22FD26A71ADA00923D70 /* PlaylistPopoverView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistPopoverView.swift; sourceTree = "<group>"; };
CA9A22FF26A7370C00923D70 /* FontScaling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontScaling.swift; sourceTree = "<group>"; };
CA9A233326B97B4300923D70 /* PlaylistMenuButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistMenuButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5195,6 +5197,7 @@
4452CAEF255412800053EFE6 /* DefaultBrowserIntroCalloutViewController.swift */,
0A0A5ED025B1F080007B3E74 /* DefaultBrowserIntroManager.swift */,
0AE50869261C6F2E0099C6A3 /* BraveSearchHelper.swift */,
CA752EA426CEABF8009356EF /* PlaylistToast.swift */,
);
indentWidth = 4;
path = Browser;
Expand Down Expand Up @@ -7441,6 +7444,7 @@
4422D56521BFFB7F00BF1855 /* filtered_re2.cc in Sources */,
277223722469B4FB0059A7EB /* FaviconFetcher.swift in Sources */,
274398F524E71D8700E79605 /* FailableDecodable.swift in Sources */,
CA752EA526CEABF8009356EF /* PlaylistToast.swift in Sources */,
0A918DCD252C81FA00496088 /* BraveVPNRegionPickerViewController.swift in Sources */,
279C756B219DDE3B001CD1CB /* FingerprintingProtection.swift in Sources */,
E650755F1E37F756006961AC /* Try.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,97 @@ extension BrowserViewController: PlaylistHelperDelegate {
popover.present(from: topToolbar.locationView.playlistButton, on: self)
}

func showPlaylistToast(tab: Tab?, state: PlaylistItemAddedState, item: PlaylistInfo?) {
updatePlaylistURLBar(tab: tab, state: state, item: item)

guard let selectedTab = tabManager.selectedTab,
selectedTab === tab,
selectedTab.url?.isPlaylistSupportedSiteURL == true else {
return
}

if let toast = pendingToast as? PlaylistToast {
toast.item = item
return
}

pendingToast = PlaylistToast(item: item, state: state, completion: { [weak self] buttonPressed in
guard let self = self,
let item = (self.pendingToast as? PlaylistToast)?.item else { return }

switch state {
// Item requires user action to add it to playlists
case .none:
if buttonPressed {
// Update playlist with new items..
self.addToPlaylist(item: item) { [weak self] didAddItem in
guard let self = self else { return }

log.debug("Playlist Item Added")
self.pendingToast = nil

if didAddItem {
self.showPlaylistToast(tab: tab, state: .existingItem, item: item)
UIImpactFeedbackGenerator(style: .medium).bzzt()
}
}
} else {
self.pendingToast = nil
}

// Item already exists in playlist, so ask them if they want to view it there
// Item was added to playlist by the user, so ask them if they want to view it there
case .newItem, .existingItem:
if buttonPressed {
UIImpactFeedbackGenerator(style: .medium).bzzt()

DispatchQueue.main.async {
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)
}
}
}

self.pendingToast = nil
}
})

if let pendingToast = pendingToast {
let duration = state == .none ? 10 : 5
show(toast: pendingToast, afterWaiting: .milliseconds(250), duration: .seconds(duration))
}
}

func showPlaylistAlert(tab: Tab?, state: PlaylistItemAddedState, item: PlaylistInfo?) {
// Has to be done otherwise it is impossible to play a video after selecting its elements
UIMenuController.shared.hideMenu()

let style: UIAlertController.Style = UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet
let alert = UIAlertController(
title: Strings.PlayList.addToPlayListAlertTitle, message: Strings.PlayList.addToPlayListAlertDescription, preferredStyle: style)

alert.addAction(UIAlertAction(title: Strings.PlayList.addToPlayListAlertTitle, style: .default, handler: { _ in
// Update playlist with new items..

guard let item = item else { return }
self.addToPlaylist(item: item) { [weak self] addedToPlaylist in
guard let self = self else { return }

UIImpactFeedbackGenerator(style: .medium).bzzt()

if addedToPlaylist {
self.showPlaylistToast(tab: tab, state: .existingItem, item: item)
}
}
}))
alert.addAction(UIAlertAction(title: Strings.cancelButtonTitle, style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}

func showPlaylistOnboarding(tab: Tab?) {
if Preferences.Playlist.showAddToPlaylistURLBarOnboarding.value < 2 && shouldShowPlaylistOnboardingThisSession {
Preferences.Playlist.showAddToPlaylistURLBarOnboarding.value += 1
Expand Down
31 changes: 24 additions & 7 deletions Client/Frontend/Browser/PlaylistHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ enum PlaylistItemAddedState {
protocol PlaylistHelperDelegate: NSObject {
func updatePlaylistURLBar(tab: Tab?, state: PlaylistItemAddedState, item: PlaylistInfo?)
func showPlaylistPopover(tab: Tab?, state: PlaylistPopoverState)
func showPlaylistToast(tab: Tab?, state: PlaylistItemAddedState, item: PlaylistInfo?)
func showPlaylistAlert(tab: Tab?, state: PlaylistItemAddedState, item: PlaylistInfo?)
func showPlaylistOnboarding(tab: Tab?)
}

Expand Down Expand Up @@ -98,13 +100,15 @@ class PlaylistHelper: NSObject, TabContentScript {
}

if PlaylistItem.itemExists(item) {
self.updateItem(item)
// Item already exists, so just update the database with new token or URL.
self.updateItem(item, detected: item.detected)
} else if item.detected {
// Automatic Detection
delegate.updatePlaylistURLBar(tab: self.tab, state: .newItem, item: item)
delegate.showPlaylistOnboarding(tab: self.tab)
} else {
delegate.updatePlaylistURLBar(tab: self.tab, state: .newItem, item: item)
delegate.showPlaylistPopover(tab: self.tab, state: .addToPlaylist)
// Long-Press
delegate.showPlaylistAlert(tab: self.tab, state: .newItem, item: item)
}
}
}
Expand Down Expand Up @@ -161,15 +165,28 @@ class PlaylistHelper: NSObject, TabContentScript {
}
}

private func updateItem(_ item: PlaylistInfo) {
self.delegate?.updatePlaylistURLBar(tab: self.tab, state: .existingItem, item: item)
private func updateItem(_ item: PlaylistInfo, detected: Bool) {
if detected {
self.delegate?.updatePlaylistURLBar(tab: self.tab, state: .existingItem, item: item)
}

PlaylistItem.updateItem(item) {
PlaylistItem.updateItem(item) { [weak self] in
guard let self = self else { return }

log.debug("Playlist Item Updated")

if !self.playlistItems.contains(item.src) {
self.playlistItems.insert(item.src)
self.delegate?.updatePlaylistURLBar(tab: self.tab, state: .existingItem, item: item)

if let delegate = self.delegate {
if detected {
delegate.updatePlaylistURLBar(tab: self.tab, state: .existingItem, item: item)
} else {
delegate.showPlaylistToast(tab: self.tab, state: .existingItem, item: item)
}
}
} else {
self.delegate?.showPlaylistToast(tab: self.tab, state: .existingItem, item: item)
}
}
}
Expand Down
Loading

0 comments on commit f6863f0

Please sign in to comment.