From daac1a0e27a12ad1e3ae41e9e8a1acec065f8880 Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Tue, 18 Jul 2023 22:29:43 +0300 Subject: [PATCH] Break retain cycle between MySiteViewController and CompliancePopoverCoordinator (#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 --- .../Blog/My Site/MySiteViewController.swift | 4 ++-- .../CompliancePopoverCoordinator.swift | 23 ++++++++++--------- .../CompliancePopoverViewModelTests.swift | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Blog/My Site/MySiteViewController.swift b/WordPress/Classes/ViewRelated/Blog/My Site/MySiteViewController.swift index 83a56adb5d26..7dd461df1034 100644 --- a/WordPress/Classes/ViewRelated/Blog/My Site/MySiteViewController.swift +++ b/WordPress/Classes/ViewRelated/Blog/My Site/MySiteViewController.swift @@ -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() { diff --git a/WordPress/Classes/ViewRelated/EEUUSCompliance/CompliancePopoverCoordinator.swift b/WordPress/Classes/ViewRelated/EEUUSCompliance/CompliancePopoverCoordinator.swift index e66504e7da63..7738b29c9283 100644 --- a/WordPress/Classes/ViewRelated/EEUUSCompliance/CompliancePopoverCoordinator.swift +++ b/WordPress/Classes/ViewRelated/EEUUSCompliance/CompliancePopoverCoordinator.swift @@ -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 } @@ -26,20 +25,20 @@ 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 { @@ -47,7 +46,7 @@ final class CompliancePopoverCoordinator: CompliancePopoverCoordinatorProtocol { return isCountryInEU && !defaults.didShowCompliancePopup } - private func presentPopover() { + private func presentPopover(on viewController: UIViewController) { let complianceViewModel = CompliancePopoverViewModel( defaults: defaults, contextManager: ContextManager.shared @@ -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 } } diff --git a/WordPress/WordPressTest/EUUSCompliance/CompliancePopoverViewModelTests.swift b/WordPress/WordPressTest/EUUSCompliance/CompliancePopoverViewModelTests.swift index 01ea91c29e6f..d791573662ec 100644 --- a/WordPress/WordPressTest/EUUSCompliance/CompliancePopoverViewModelTests.swift +++ b/WordPress/WordPressTest/EUUSCompliance/CompliancePopoverViewModelTests.swift @@ -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 }