Fix remaining retain cycles, allow coordinators to be weakly referenced and make sure the NavigationStackCoordinator cleans up after itself

This commit is contained in:
Stefan Ceriu
2023-02-02 10:15:31 +02:00
committed by Stefan Ceriu
parent 93997efef9
commit 594f6f85dd
14 changed files with 63 additions and 36 deletions
@@ -17,7 +17,7 @@
import SwiftUI
@MainActor
protocol CoordinatorProtocol {
protocol CoordinatorProtocol: AnyObject {
func start()
func stop()
func toPresentable() -> AnyView
@@ -213,14 +213,14 @@ class NavigationSplitCoordinator: CoordinatorProtocol, ObservableObject, CustomS
///
/// For added complexity, the NavigationSplitCoordinator has an internal compact layout NavigationStack for which we need to manually nil things again
private func releaseAllCoordinatorReferences() {
sidebarModule?.coordinator = nil
detailModule?.coordinator = nil
sheetModule?.coordinator = nil
fullScreenCoverModule?.coordinator = nil
sidebarModule?.tearDown()
detailModule?.tearDown()
sheetModule?.tearDown()
fullScreenCoverModule?.tearDown()
compactLayoutRootModule?.coordinator = nil
compactLayoutRootModule?.tearDown()
compactLayoutStackModules.forEach { module in
module.coordinator = nil
module.tearDown()
}
}
@@ -594,6 +594,19 @@ class NavigationStackCoordinator: ObservableObject, CoordinatorProtocol, CustomS
.presentationDetents(presentationDetents))
}
/// The NavigationStack has a tendency to hold on to path items for longer than needed. We work around that by manually nilling the coordinator
/// when a NavigationModule is dismissed. As the NavigationModule is just a wrapper multiple instances of it continuing living is of no consequence
/// https://stackoverflow.com/questions/73885353/found-a-strange-behaviour-of-state-when-combined-to-the-new-navigation-stack/
func stop() {
rootModule?.tearDown()
sheetModule?.tearDown()
fullScreenCoverModule?.tearDown()
stackModules.forEach { module in
module.tearDown()
}
}
// MARK: - CustomStringConvertible
var description: String {
@@ -27,7 +27,7 @@ class NavigationModule: Identifiable, Hashable {
/// As the NavigationModule is just a wrapper multiple instances of it continuing living is of no consequence
/// https://stackoverflow.com/questions/73885353/found-a-strange-behaviour-of-state-when-combined-to-the-new-navigation-stack/
var coordinator: (any CoordinatorProtocol)?
let dismissalCallback: (() -> Void)?
var dismissalCallback: (() -> Void)?
init(_ coordinator: any CoordinatorProtocol, dismissalCallback: (() -> Void)? = nil) {
self.coordinator = coordinator
@@ -38,6 +38,7 @@ class NavigationModule: Identifiable, Hashable {
coordinator?.stop()
dismissalCallback?()
coordinator = nil
dismissalCallback = nil
}
nonisolated static func == (lhs: NavigationModule, rhs: NavigationModule) -> Bool {
@@ -16,7 +16,7 @@
import Foundation
struct MockUserNotificationController: UserNotificationControllerProtocol {
class MockUserNotificationController: UserNotificationControllerProtocol {
func submitNotification(_ notification: UserNotification) { }
func retractNotificationWithId(_ id: String) { }
@@ -23,7 +23,7 @@ enum BugReportCoordinatorResult {
struct BugReportCoordinatorParameters {
let bugReportService: BugReportServiceProtocol
let userNotificationController: UserNotificationControllerProtocol
weak var userNotificationController: UserNotificationControllerProtocol?
let screenshot: UIImage?
let isModallyPresented: Bool
}
@@ -76,17 +76,17 @@ final class BugReportCoordinator: CoordinatorProtocol {
static let loadingIndicatorIdentifier = "BugReportLoading"
private func startLoading(label: String = ElementL10n.loading) {
parameters.userNotificationController.submitNotification(UserNotification(id: Self.loadingIndicatorIdentifier,
type: .modal,
title: label,
persistent: true))
parameters.userNotificationController?.submitNotification(UserNotification(id: Self.loadingIndicatorIdentifier,
type: .modal,
title: label,
persistent: true))
}
private func stopLoading() {
parameters.userNotificationController.retractNotificationWithId(Self.loadingIndicatorIdentifier)
parameters.userNotificationController?.retractNotificationWithId(Self.loadingIndicatorIdentifier)
}
private func showError(label: String) {
parameters.userNotificationController.submitNotification(UserNotification(title: label))
parameters.userNotificationController?.submitNotification(UserNotification(title: label))
}
}
@@ -16,9 +16,13 @@
import SwiftUI
struct ActivityCoordinator: CoordinatorProtocol {
class ActivityCoordinator: CoordinatorProtocol {
let items: [Any]
init(items: [Any]) {
self.items = items
}
func toPresentable() -> AnyView {
AnyView(UIActivityViewControllerWrapper(activityItems: items)
.presentationDetents([.medium])
@@ -16,9 +16,13 @@
import SwiftUI
struct InviteFriendsCoordinator: CoordinatorProtocol {
class InviteFriendsCoordinator: CoordinatorProtocol {
let userId: String
init(userId: String) {
self.userId = userId
}
func toPresentable() -> AnyView {
guard let permalink = try? PermalinkBuilder.permalinkTo(userIdentifier: userId).absoluteString else {
return AnyView(EmptyView())
@@ -16,7 +16,7 @@
import SwiftUI
struct SplashScreenCoordinator: CoordinatorProtocol {
class SplashScreenCoordinator: CoordinatorProtocol {
func toPresentable() -> AnyView {
AnyView(
ZStack {
@@ -17,8 +17,8 @@
import SwiftUI
struct SettingsScreenCoordinatorParameters {
let navigationStackCoordinator: NavigationStackCoordinator
let userNotificationController: UserNotificationControllerProtocol
weak var navigationStackCoordinator: NavigationStackCoordinator?
weak var userNotificationController: UserNotificationControllerProtocol?
let userSession: UserSessionProtocol
let bugReportService: BugReportServiceProtocol
}
@@ -88,10 +88,10 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
break
}
self?.parameters.navigationStackCoordinator.pop()
self?.parameters.navigationStackCoordinator?.pop()
}
parameters.navigationStackCoordinator.push(coordinator)
parameters.navigationStackCoordinator?.push(coordinator)
}
private func verifySession() {
@@ -103,15 +103,15 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
let coordinator = SessionVerificationCoordinator(parameters: verificationParameters)
coordinator.callback = { [weak self] in
self?.parameters.navigationStackCoordinator.setSheetCoordinator(nil)
self?.parameters.navigationStackCoordinator?.setSheetCoordinator(nil)
}
parameters.navigationStackCoordinator.setSheetCoordinator(coordinator) { [weak self] in
self?.parameters.navigationStackCoordinator.setSheetCoordinator(nil)
parameters.navigationStackCoordinator?.setSheetCoordinator(coordinator) { [weak self] in
self?.parameters.navigationStackCoordinator?.setSheetCoordinator(nil)
}
}
private func showSuccess(label: String) {
parameters.userNotificationController.submitNotification(UserNotification(title: label))
parameters.userNotificationController?.submitNotification(UserNotification(title: label))
}
}
@@ -119,7 +119,7 @@ class MockScreen: Identifiable {
case .settings:
let navigationStackCoordinator = NavigationStackCoordinator()
let coordinator = SettingsScreenCoordinator(parameters: .init(navigationStackCoordinator: navigationStackCoordinator,
userNotificationController: MockUserNotificationController(),
userNotificationController: nil,
userSession: MockUserSession(clientProxy: MockClientProxy(userID: "@mock:client.com"),
mediaProvider: MockMediaProvider()),
bugReportService: MockBugReportService()))
@@ -128,7 +128,7 @@ class MockScreen: Identifiable {
case .bugReport:
let navigationStackCoordinator = NavigationStackCoordinator()
let coordinator = BugReportCoordinator(parameters: .init(bugReportService: MockBugReportService(),
userNotificationController: MockUserNotificationController(),
userNotificationController: nil,
screenshot: nil,
isModallyPresented: true))
navigationStackCoordinator.setRootCoordinator(coordinator)
@@ -136,7 +136,7 @@ class MockScreen: Identifiable {
case .bugReportWithScreenshot:
let navigationStackCoordinator = NavigationStackCoordinator()
let coordinator = BugReportCoordinator(parameters: .init(bugReportService: MockBugReportService(),
userNotificationController: MockUserNotificationController(),
userNotificationController: nil,
screenshot: Asset.Images.appLogo.image,
isModallyPresented: false))
navigationStackCoordinator.setRootCoordinator(coordinator)
@@ -16,18 +16,23 @@
import SwiftUI
struct UITestsRootCoordinator: CoordinatorProtocol {
class UITestsRootCoordinator: CoordinatorProtocol {
let mockScreens: [MockScreen]
var selectionCallback: ((UITestScreenIdentifier) -> Void)?
init(mockScreens: [MockScreen], selectionCallback: ((UITestScreenIdentifier) -> Void)? = nil) {
self.mockScreens = mockScreens
self.selectionCallback = selectionCallback
}
func toPresentable() -> AnyView {
AnyView(body)
}
private var body: some View {
List(mockScreens) { coordinator in
Button(coordinator.id.description) {
selectionCallback?(coordinator.id)
Button(coordinator.id.description) { [weak self] in
self?.selectionCallback?(coordinator.id)
}
.accessibilityIdentifier(coordinator.id.rawValue)
}
@@ -67,6 +67,6 @@ class NavigationRootCoordinatorTests: XCTestCase {
}
}
private struct SomeTestCoordinator: CoordinatorProtocol {
private class SomeTestCoordinator: CoordinatorProtocol {
let id = UUID()
}
@@ -269,6 +269,6 @@ class NavigationSplitCoordinatorTests: XCTestCase {
}
}
private struct SomeTestCoordinator: CoordinatorProtocol {
private class SomeTestCoordinator: CoordinatorProtocol {
let id = UUID()
}
@@ -212,6 +212,6 @@ class NavigationStackCoordinatorTests: XCTestCase {
}
}
private struct SomeTestCoordinator: CoordinatorProtocol {
private class SomeTestCoordinator: CoordinatorProtocol {
let id = UUID()
}