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

Commit

Permalink
Fix #4092 - Weakify tab references due to memory leak. (#4093)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T authored Aug 25, 2021
1 parent cd8ec53 commit 5386251
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension BrowserViewController {
VStack(spacing: 0) {
VPNMenuButton(
vpnProductInfo: self.vpnProductInfo,
displayVPNDestination: { vc in
displayVPNDestination: { [unowned self] vc in
(self.presentedViewController as? MenuViewController)?
.pushInnerMenu(vc)
},
Expand All @@ -31,28 +31,57 @@ extension BrowserViewController {

func destinationMenuSection(_ menuController: MenuViewController) -> some View {
VStack(spacing: 0) {
MenuItemButton(icon: #imageLiteral(resourceName: "menu_bookmarks").template, title: Strings.bookmarksMenuItem) {
MenuItemButton(icon: #imageLiteral(resourceName: "menu_bookmarks").template, title: Strings.bookmarksMenuItem) { [unowned self, unowned menuController] in
let vc = BookmarksViewController(folder: Bookmarkv2.lastVisitedFolder(), isPrivateBrowsing: PrivateBrowsingManager.shared.isPrivateBrowsing)
vc.toolbarUrlActionsDelegate = self
menuController.presentInnerMenu(vc)
}
MenuItemButton(icon: #imageLiteral(resourceName: "menu-history").template, title: Strings.historyMenuItem) {
MenuItemButton(icon: #imageLiteral(resourceName: "menu-history").template, title: Strings.historyMenuItem) { [unowned self, unowned menuController] in
let vc = HistoryViewController(isPrivateBrowsing: PrivateBrowsingManager.shared.isPrivateBrowsing)
vc.toolbarUrlActionsDelegate = self
menuController.pushInnerMenu(vc)
}
MenuItemButton(icon: #imageLiteral(resourceName: "menu-downloads").template, title: Strings.downloadsMenuItem) {
MenuItemButton(icon: #imageLiteral(resourceName: "menu-downloads").template, title: Strings.downloadsMenuItem) { [unowned self, unowned menuController] in
let vc = DownloadsPanel(profile: self.profile)
menuController.pushInnerMenu(vc)
}
MenuItemButton(icon: #imageLiteral(resourceName: "playlist_menu").template, title: Strings.playlistMenuItem) {
let playlistController = (UIApplication.shared.delegate as? AppDelegate)?.playlistRestorationController ?? PlaylistViewController(initialItem: nil, initialItemPlaybackOffset: 0.0)
playlistController.modalPresentationStyle = .fullScreen
self.dismiss(animated: true) {
self.present(playlistController, animated: true)
MenuItemButton(icon: #imageLiteral(resourceName: "playlist_menu").template, title: Strings.playlistMenuItem) { [weak self] in
guard let self = self else { return }

let playlistController = (UIApplication.shared.delegate as? AppDelegate)?.playlistRestorationController

// Present existing playlist controller
if let playlistController = playlistController {
self.dismiss(animated: true) {
self.present(playlistController, animated: true)
}
} else {
// Retrieve the item and offset-time from the current tab's webview.
if let item = self.tabManager.selectedTab?.playlistItem,
let webView = self.tabManager.selectedTab?.webView {

PlaylistHelper.getCurrentTime(webView: webView, nodeTag: item.tagId) { [weak self] currentTime in
guard let self = self else { return }

let playlistController = PlaylistViewController(initialItem: item, initialItemPlaybackOffset: currentTime)
playlistController.modalPresentationStyle = .fullScreen

self.dismiss(animated: true) {
self.present(playlistController, animated: true)
}
}
} else {
// Otherwise no item to play, so just open the playlist controller.
let playlistController = PlaylistViewController(initialItem: nil, initialItemPlaybackOffset: 0.0)
playlistController.modalPresentationStyle = .fullScreen

self.dismiss(animated: true) {
self.present(playlistController, animated: true)
}
}
}
}
MenuItemButton(icon: #imageLiteral(resourceName: "menu-settings").template, title: Strings.settingsMenuItem) {
MenuItemButton(icon: #imageLiteral(resourceName: "menu-settings").template, title: Strings.settingsMenuItem) { [unowned self, unowned menuController] in
let vc = SettingsViewController(profile: self.profile, tabManager: self.tabManager, feedDataSource: self.feedDataSource, rewards: self.rewards, legacyWallet: self.legacyWallet)
vc.settingsDelegate = self
menuController.pushInnerMenu(vc)
Expand All @@ -61,75 +90,75 @@ extension BrowserViewController {
}

struct PageActionsMenuSection: View {
var browserViewController: BrowserViewController
var tabURL: URL
var activities: [UIActivity]

@State private var playlistItemAdded: Bool = false

private var playlistActivity: (enabled: Bool, item: PlaylistInfo?)? {
browserViewController.addToPlayListActivityItem ??
browserViewController.openInPlaylistActivityItem
}

private var isPlaylistItemAdded: Bool {
browserViewController.openInPlaylistActivityItem != nil
}

var body: some View {
VStack(alignment: .leading, spacing: 0) {
MenuTabDetailsView(tab: browserViewController.tabManager.selectedTab, url: tabURL)
VStack(spacing: 0) {
if let activity = playlistActivity, activity.enabled, let item = activity.item {
PlaylistMenuButton(isAdded: isPlaylistItemAdded) {
if !isPlaylistItemAdded {
// Add to playlist
browserViewController.addToPlaylist(item: item) { didAddItem in
log.debug("Playlist Item Added")
if didAddItem {
playlistItemAdded = true
}
var browserViewController: BrowserViewController
var tabURL: URL
var activities: [UIActivity]

@State private var playlistItemAdded: Bool = false

private var playlistActivity: (enabled: Bool, item: PlaylistInfo?)? {
browserViewController.addToPlayListActivityItem ??
browserViewController.openInPlaylistActivityItem
}

private var isPlaylistItemAdded: Bool {
browserViewController.openInPlaylistActivityItem != nil
}

var body: some View {
VStack(alignment: .leading, spacing: 0) {
MenuTabDetailsView(tab: browserViewController.tabManager.selectedTab, url: tabURL)
VStack(spacing: 0) {
if let activity = playlistActivity, activity.enabled, let item = activity.item {
PlaylistMenuButton(isAdded: isPlaylistItemAdded) {
if !isPlaylistItemAdded {
// Add to playlist
browserViewController.addToPlaylist(item: item) { didAddItem in
log.debug("Playlist Item Added")
if didAddItem {
playlistItemAdded = true
}
} else {
browserViewController.dismiss(animated: true) {
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)
}
} else {
browserViewController.dismiss(animated: true) {
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)
}
}
}
.animation(.default, value: playlistItemAdded)
}
MenuItemButton(icon: #imageLiteral(resourceName: "nav-share").template, title: Strings.shareWithMenuItem) {
browserViewController.dismiss(animated: true)
browserViewController.tabToolbarDidPressShare()
.animation(.default, value: playlistItemAdded)
}
MenuItemButton(icon: #imageLiteral(resourceName: "nav-share").template, title: Strings.shareWithMenuItem) {
browserViewController.dismiss(animated: true)
browserViewController.tabToolbarDidPressShare()
}
MenuItemButton(icon: #imageLiteral(resourceName: "menu-add-bookmark").template, title: Strings.addToMenuItem) {
browserViewController.dismiss(animated: true) {
browserViewController.openAddBookmark()
}
MenuItemButton(icon: #imageLiteral(resourceName: "menu-add-bookmark").template, title: Strings.addToMenuItem) {
}
ForEach(activities, id: \.activityTitle) { activity in
MenuItemButton(icon: activity.activityImage?.template ?? UIImage(), title: activity.activityTitle ?? "") {
browserViewController.dismiss(animated: true) {
browserViewController.openAddBookmark()
}
}
ForEach(activities, id: \.activityTitle) { activity in
MenuItemButton(icon: activity.activityImage?.template ?? UIImage(), title: activity.activityTitle ?? "") {
browserViewController.dismiss(animated: true) {
activity.perform()
}
activity.perform()
}
}
}
}
}
}
}

struct MenuTabDetailsView: View {
@SwiftUI.Environment(\.colorScheme) var colorScheme: ColorScheme
var tab: Tab?
weak var tab: Tab?
var url: URL

var body: some View {
VStack(alignment: .leading, spacing: 2) {
if let tab = tab {
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Share/RequestDesktopSiteActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
import Shared

class RequestDesktopSiteActivity: UIActivity {
private let tab: Tab?
private weak var tab: Tab?
fileprivate let callback: () -> Void

init(tab: Tab?, callback: @escaping () -> Void) {
Expand Down

0 comments on commit 5386251

Please sign in to comment.