Skip to content

Commit

Permalink
Break retain cycle between MySiteViewController and CompliancePopover…
Browse files Browse the repository at this point in the history
…Coordinator (#21111)

* Break retain cycle between MySiteViewController and CompliancePopoverCoordinator

MySiteViewController holds CompliancePopoverCoordinator, and CompliancePopoverCoordinator holds MySiteViewController. Break retain cycle by setting viewController in CompliancePopoverCoordinator as weak

* Revert removing didShowCompliancePopup check in presentIfNeeded
  • Loading branch information
staskus authored Jul 18, 2023
1 parent a0c859b commit daac1a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ class MySiteViewController: UIViewController, NoResultsViewHost {
createFABIfNeeded()
fetchPrompt(for: blog)

complianceCoordinator = CompliancePopoverCoordinator(viewController: self)
complianceCoordinator?.presentIfNeeded()
complianceCoordinator = CompliancePopoverCoordinator()
complianceCoordinator?.presentIfNeeded(on: self)
}

override func viewDidLayoutSubviews() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import UIKit

protocol CompliancePopoverCoordinatorProtocol {
func presentIfNeeded()
protocol CompliancePopoverCoordinatorProtocol: AnyObject {
func presentIfNeeded(on viewController: UIViewController)
func navigateToSettings()
func dismiss()
}

final class CompliancePopoverCoordinator: CompliancePopoverCoordinatorProtocol {
private let viewController: UIViewController
private weak var presentingViewController: UIViewController?
private let complianceService = ComplianceLocationService()
private let defaults: UserDefaults

init(viewController: UIViewController, defaults: UserDefaults = UserDefaults.standard) {
self.viewController = viewController
init(defaults: UserDefaults = UserDefaults.standard) {
self.defaults = defaults
}

func presentIfNeeded() {
func presentIfNeeded(on viewController: UIViewController) {
guard FeatureFlag.compliancePopover.enabled, !defaults.didShowCompliancePopup else {
return
}
Expand All @@ -26,28 +25,28 @@ final class CompliancePopoverCoordinator: CompliancePopoverCoordinatorProtocol {
return
}
DispatchQueue.main.async {
self.presentPopover()
self.presentPopover(on: viewController)
}
}
}
}

func navigateToSettings() {
viewController.dismiss(animated: true) {
presentingViewController?.dismiss(animated: true) {
RootViewCoordinator.sharedPresenter.navigateToPrivacySettings()
}
}

func dismiss() {
viewController.dismiss(animated: true)
presentingViewController?.dismiss(animated: true)
}

private func shouldShowPrivacyBanner(countryCode: String) -> Bool {
let isCountryInEU = Self.gdprCountryCodes.contains(countryCode)
return isCountryInEU && !defaults.didShowCompliancePopup
}

private func presentPopover() {
private func presentPopover(on viewController: UIViewController) {
let complianceViewModel = CompliancePopoverViewModel(
defaults: defaults,
contextManager: ContextManager.shared
Expand All @@ -56,7 +55,9 @@ final class CompliancePopoverCoordinator: CompliancePopoverCoordinatorProtocol {
let complianceViewController = CompliancePopoverViewController(viewModel: complianceViewModel)
let bottomSheetViewController = BottomSheetViewController(childViewController: complianceViewController, customHeaderSpacing: 0)

bottomSheetViewController.show(from: self.viewController)
bottomSheetViewController.show(from: viewController)

self.presentingViewController = viewController
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private class MockCompliancePopoverCoordinator: CompliancePopoverCoordinatorProt
private(set) var presentIfNeededCallCount = 0
private(set) var dismissCallCount = 0

func presentIfNeeded() {
func presentIfNeeded(on viewController: UIViewController) {
presentIfNeededCallCount += 1
}

Expand Down

0 comments on commit daac1a0

Please sign in to comment.