From 6eb462aeb727b91ffe13b47d268dea3827bda5c7 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 30 Mar 2021 15:39:24 -0700 Subject: [PATCH] Fix Leaks --- App/Browser View/BrowserViewController.swift | 4 ++-- App/Tabs/TabBarView.swift | 13 +++++++------ App/Tabs/TabBarViewController.swift | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/App/Browser View/BrowserViewController.swift b/App/Browser View/BrowserViewController.swift index c840b34..617028b 100644 --- a/App/Browser View/BrowserViewController.swift +++ b/App/Browser View/BrowserViewController.swift @@ -244,7 +244,7 @@ class BrowserViewController: UIViewController webView.goBack() }, for: .touchUpInside) - documentControls.observations.append(webView.observe(\.canGoBack, changeHandler: { (_, _) in + documentControls.observations.append(webView.observe(\.canGoBack, changeHandler: { (webView, _) in documentControls.navigationControlView.backButton.isEnabled = webView.canGoBack })) @@ -253,7 +253,7 @@ class BrowserViewController: UIViewController webView.goForward() }, for: .touchUpInside) - documentControls.observations.append(webView.observe(\.canGoForward, changeHandler: { (_, _) in + documentControls.observations.append(webView.observe(\.canGoForward, changeHandler: { (webView, _) in documentControls.navigationControlView.forwardButton.isEnabled = webView.canGoForward })) diff --git a/App/Tabs/TabBarView.swift b/App/Tabs/TabBarView.swift index 5fdc38f..ceb8403 100644 --- a/App/Tabs/TabBarView.swift +++ b/App/Tabs/TabBarView.swift @@ -128,8 +128,8 @@ class TabBarView: UIView { static let preferredHeight: CGFloat = 30.0 - public var delegate: TabBarViewDelegate? - public var dataSource: TabBarViewDataSource? + public weak var delegate: TabBarViewDelegate? + public weak var dataSource: TabBarViewDataSource? private var tabViews: [TabView] = [] private var activeTabIndex: Int = 0 @@ -192,9 +192,10 @@ class TabBarView: UIView UIView.animate(withDuration: 0.22, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: [], animations: { [unowned self] in layoutSubviews() - }, completion: { [unowned self] finished in + }, completion: { [weak self] finished in + guard let self = self else { return } tabViewsRemoved.forEach { $0.removeFromSuperview() } - tabViews.removeAll { tabViewsRemoved.contains($0) } + self.tabViews.removeAll { tabViewsRemoved.contains($0) } }) } @@ -232,14 +233,14 @@ class TabBarView: UIView let tabView = TabView() tabView.identifier = identifier - tabView.addAction(UIAction(handler: { [unowned self, tabView] _ in + tabView.addAction(UIAction(handler: { [unowned self, unowned tabView] _ in guard let delegate = self.delegate else { return } guard let tabIndex = self.tabViews.firstIndex(of: tabView) else { return } delegate.tabBarView(self, didClickToActivateTabAtIndex: tabIndex) }), for: .touchUpInside) - tabView.closeButton.addAction(UIAction(handler: { [unowned self, tabView] _ in + tabView.closeButton.addAction(UIAction(handler: { [unowned self, unowned tabView] _ in guard let delegate = self.delegate else { return } guard let tabIndex = self.tabViews.firstIndex(of: tabView) else { return } diff --git a/App/Tabs/TabBarViewController.swift b/App/Tabs/TabBarViewController.swift index eb8eae8..b120256 100644 --- a/App/Tabs/TabBarViewController.swift +++ b/App/Tabs/TabBarViewController.swift @@ -29,11 +29,11 @@ class TabBarViewController: UIViewController, TabBarViewDataSource, TabBarViewDe tabBarView.delegate = self tabBarView.reloadTabs() - tabObserver = tabController.$tabs.sink(receiveValue: { [tabBarView] (newTabs: [Tab]) in + tabObserver = tabController.$tabs.sink(receiveValue: { [unowned self] (newTabs: [Tab]) in DispatchQueue.main.async { tabBarView.reloadTabs() } }) - activeTabIndexObserver = tabController.$activeTabIndex.sink(receiveValue: { [tabBarView] (activeIndex: Int) in + activeTabIndexObserver = tabController.$activeTabIndex.sink(receiveValue: { [unowned self] (activeIndex: Int) in tabBarView.activateTab(atIndex: activeIndex) }) }