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 import SwiftUI
@MainActor @MainActor
protocol CoordinatorProtocol { protocol CoordinatorProtocol: AnyObject {
func start() func start()
func stop() func stop()
func toPresentable() -> AnyView 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 /// For added complexity, the NavigationSplitCoordinator has an internal compact layout NavigationStack for which we need to manually nil things again
private func releaseAllCoordinatorReferences() { private func releaseAllCoordinatorReferences() {
sidebarModule?.coordinator = nil sidebarModule?.tearDown()
detailModule?.coordinator = nil detailModule?.tearDown()
sheetModule?.coordinator = nil sheetModule?.tearDown()
fullScreenCoverModule?.coordinator = nil fullScreenCoverModule?.tearDown()
compactLayoutRootModule?.coordinator = nil compactLayoutRootModule?.tearDown()
compactLayoutStackModules.forEach { module in compactLayoutStackModules.forEach { module in
module.coordinator = nil module.tearDown()
} }
} }
@@ -594,6 +594,19 @@ class NavigationStackCoordinator: ObservableObject, CoordinatorProtocol, CustomS
.presentationDetents(presentationDetents)) .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 // MARK: - CustomStringConvertible
var description: String { 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 /// 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/ /// https://stackoverflow.com/questions/73885353/found-a-strange-behaviour-of-state-when-combined-to-the-new-navigation-stack/
var coordinator: (any CoordinatorProtocol)? var coordinator: (any CoordinatorProtocol)?
let dismissalCallback: (() -> Void)? var dismissalCallback: (() -> Void)?
init(_ coordinator: any CoordinatorProtocol, dismissalCallback: (() -> Void)? = nil) { init(_ coordinator: any CoordinatorProtocol, dismissalCallback: (() -> Void)? = nil) {
self.coordinator = coordinator self.coordinator = coordinator
@@ -38,6 +38,7 @@ class NavigationModule: Identifiable, Hashable {
coordinator?.stop() coordinator?.stop()
dismissalCallback?() dismissalCallback?()
coordinator = nil coordinator = nil
dismissalCallback = nil
} }
nonisolated static func == (lhs: NavigationModule, rhs: NavigationModule) -> Bool { nonisolated static func == (lhs: NavigationModule, rhs: NavigationModule) -> Bool {
@@ -16,7 +16,7 @@
import Foundation import Foundation
struct MockUserNotificationController: UserNotificationControllerProtocol { class MockUserNotificationController: UserNotificationControllerProtocol {
func submitNotification(_ notification: UserNotification) { } func submitNotification(_ notification: UserNotification) { }
func retractNotificationWithId(_ id: String) { } func retractNotificationWithId(_ id: String) { }
@@ -23,7 +23,7 @@ enum BugReportCoordinatorResult {
struct BugReportCoordinatorParameters { struct BugReportCoordinatorParameters {
let bugReportService: BugReportServiceProtocol let bugReportService: BugReportServiceProtocol
let userNotificationController: UserNotificationControllerProtocol weak var userNotificationController: UserNotificationControllerProtocol?
let screenshot: UIImage? let screenshot: UIImage?
let isModallyPresented: Bool let isModallyPresented: Bool
} }
@@ -76,17 +76,17 @@ final class BugReportCoordinator: CoordinatorProtocol {
static let loadingIndicatorIdentifier = "BugReportLoading" static let loadingIndicatorIdentifier = "BugReportLoading"
private func startLoading(label: String = ElementL10n.loading) { private func startLoading(label: String = ElementL10n.loading) {
parameters.userNotificationController.submitNotification(UserNotification(id: Self.loadingIndicatorIdentifier, parameters.userNotificationController?.submitNotification(UserNotification(id: Self.loadingIndicatorIdentifier,
type: .modal, type: .modal,
title: label, title: label,
persistent: true)) persistent: true))
} }
private func stopLoading() { private func stopLoading() {
parameters.userNotificationController.retractNotificationWithId(Self.loadingIndicatorIdentifier) parameters.userNotificationController?.retractNotificationWithId(Self.loadingIndicatorIdentifier)
} }
private func showError(label: String) { private func showError(label: String) {
parameters.userNotificationController.submitNotification(UserNotification(title: label)) parameters.userNotificationController?.submitNotification(UserNotification(title: label))
} }
} }
@@ -16,9 +16,13 @@
import SwiftUI import SwiftUI
struct ActivityCoordinator: CoordinatorProtocol { class ActivityCoordinator: CoordinatorProtocol {
let items: [Any] let items: [Any]
init(items: [Any]) {
self.items = items
}
func toPresentable() -> AnyView { func toPresentable() -> AnyView {
AnyView(UIActivityViewControllerWrapper(activityItems: items) AnyView(UIActivityViewControllerWrapper(activityItems: items)
.presentationDetents([.medium]) .presentationDetents([.medium])
@@ -16,9 +16,13 @@
import SwiftUI import SwiftUI
struct InviteFriendsCoordinator: CoordinatorProtocol { class InviteFriendsCoordinator: CoordinatorProtocol {
let userId: String let userId: String
init(userId: String) {
self.userId = userId
}
func toPresentable() -> AnyView { func toPresentable() -> AnyView {
guard let permalink = try? PermalinkBuilder.permalinkTo(userIdentifier: userId).absoluteString else { guard let permalink = try? PermalinkBuilder.permalinkTo(userIdentifier: userId).absoluteString else {
return AnyView(EmptyView()) return AnyView(EmptyView())
@@ -16,7 +16,7 @@
import SwiftUI import SwiftUI
struct SplashScreenCoordinator: CoordinatorProtocol { class SplashScreenCoordinator: CoordinatorProtocol {
func toPresentable() -> AnyView { func toPresentable() -> AnyView {
AnyView( AnyView(
ZStack { ZStack {
@@ -17,8 +17,8 @@
import SwiftUI import SwiftUI
struct SettingsScreenCoordinatorParameters { struct SettingsScreenCoordinatorParameters {
let navigationStackCoordinator: NavigationStackCoordinator weak var navigationStackCoordinator: NavigationStackCoordinator?
let userNotificationController: UserNotificationControllerProtocol weak var userNotificationController: UserNotificationControllerProtocol?
let userSession: UserSessionProtocol let userSession: UserSessionProtocol
let bugReportService: BugReportServiceProtocol let bugReportService: BugReportServiceProtocol
} }
@@ -88,10 +88,10 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
break break
} }
self?.parameters.navigationStackCoordinator.pop() self?.parameters.navigationStackCoordinator?.pop()
} }
parameters.navigationStackCoordinator.push(coordinator) parameters.navigationStackCoordinator?.push(coordinator)
} }
private func verifySession() { private func verifySession() {
@@ -103,15 +103,15 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
let coordinator = SessionVerificationCoordinator(parameters: verificationParameters) let coordinator = SessionVerificationCoordinator(parameters: verificationParameters)
coordinator.callback = { [weak self] in coordinator.callback = { [weak self] in
self?.parameters.navigationStackCoordinator.setSheetCoordinator(nil) self?.parameters.navigationStackCoordinator?.setSheetCoordinator(nil)
} }
parameters.navigationStackCoordinator.setSheetCoordinator(coordinator) { [weak self] in parameters.navigationStackCoordinator?.setSheetCoordinator(coordinator) { [weak self] in
self?.parameters.navigationStackCoordinator.setSheetCoordinator(nil) self?.parameters.navigationStackCoordinator?.setSheetCoordinator(nil)
} }
} }
private func showSuccess(label: String) { 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: case .settings:
let navigationStackCoordinator = NavigationStackCoordinator() let navigationStackCoordinator = NavigationStackCoordinator()
let coordinator = SettingsScreenCoordinator(parameters: .init(navigationStackCoordinator: navigationStackCoordinator, let coordinator = SettingsScreenCoordinator(parameters: .init(navigationStackCoordinator: navigationStackCoordinator,
userNotificationController: MockUserNotificationController(), userNotificationController: nil,
userSession: MockUserSession(clientProxy: MockClientProxy(userID: "@mock:client.com"), userSession: MockUserSession(clientProxy: MockClientProxy(userID: "@mock:client.com"),
mediaProvider: MockMediaProvider()), mediaProvider: MockMediaProvider()),
bugReportService: MockBugReportService())) bugReportService: MockBugReportService()))
@@ -128,7 +128,7 @@ class MockScreen: Identifiable {
case .bugReport: case .bugReport:
let navigationStackCoordinator = NavigationStackCoordinator() let navigationStackCoordinator = NavigationStackCoordinator()
let coordinator = BugReportCoordinator(parameters: .init(bugReportService: MockBugReportService(), let coordinator = BugReportCoordinator(parameters: .init(bugReportService: MockBugReportService(),
userNotificationController: MockUserNotificationController(), userNotificationController: nil,
screenshot: nil, screenshot: nil,
isModallyPresented: true)) isModallyPresented: true))
navigationStackCoordinator.setRootCoordinator(coordinator) navigationStackCoordinator.setRootCoordinator(coordinator)
@@ -136,7 +136,7 @@ class MockScreen: Identifiable {
case .bugReportWithScreenshot: case .bugReportWithScreenshot:
let navigationStackCoordinator = NavigationStackCoordinator() let navigationStackCoordinator = NavigationStackCoordinator()
let coordinator = BugReportCoordinator(parameters: .init(bugReportService: MockBugReportService(), let coordinator = BugReportCoordinator(parameters: .init(bugReportService: MockBugReportService(),
userNotificationController: MockUserNotificationController(), userNotificationController: nil,
screenshot: Asset.Images.appLogo.image, screenshot: Asset.Images.appLogo.image,
isModallyPresented: false)) isModallyPresented: false))
navigationStackCoordinator.setRootCoordinator(coordinator) navigationStackCoordinator.setRootCoordinator(coordinator)
@@ -16,18 +16,23 @@
import SwiftUI import SwiftUI
struct UITestsRootCoordinator: CoordinatorProtocol { class UITestsRootCoordinator: CoordinatorProtocol {
let mockScreens: [MockScreen] let mockScreens: [MockScreen]
var selectionCallback: ((UITestScreenIdentifier) -> Void)? var selectionCallback: ((UITestScreenIdentifier) -> Void)?
init(mockScreens: [MockScreen], selectionCallback: ((UITestScreenIdentifier) -> Void)? = nil) {
self.mockScreens = mockScreens
self.selectionCallback = selectionCallback
}
func toPresentable() -> AnyView { func toPresentable() -> AnyView {
AnyView(body) AnyView(body)
} }
private var body: some View { private var body: some View {
List(mockScreens) { coordinator in List(mockScreens) { coordinator in
Button(coordinator.id.description) { Button(coordinator.id.description) { [weak self] in
selectionCallback?(coordinator.id) self?.selectionCallback?(coordinator.id)
} }
.accessibilityIdentifier(coordinator.id.rawValue) .accessibilityIdentifier(coordinator.id.rawValue)
} }
@@ -67,6 +67,6 @@ class NavigationRootCoordinatorTests: XCTestCase {
} }
} }
private struct SomeTestCoordinator: CoordinatorProtocol { private class SomeTestCoordinator: CoordinatorProtocol {
let id = UUID() let id = UUID()
} }
@@ -269,6 +269,6 @@ class NavigationSplitCoordinatorTests: XCTestCase {
} }
} }
private struct SomeTestCoordinator: CoordinatorProtocol { private class SomeTestCoordinator: CoordinatorProtocol {
let id = UUID() let id = UUID()
} }
@@ -212,6 +212,6 @@ class NavigationStackCoordinatorTests: XCTestCase {
} }
} }
private struct SomeTestCoordinator: CoordinatorProtocol { private class SomeTestCoordinator: CoordinatorProtocol {
let id = UUID() let id = UUID()
} }