Skip to content

Commit

Permalink
Fix navigationStackCoordinator getting torn down when being reset on … (
Browse files Browse the repository at this point in the history
#613)

* Fix navigationStackCoordinator getting torn down when being reset on the navigationSplitCoordinator, added unit tests for it

* Guard against using the same coordinator more than once
  • Loading branch information
stefanceriu authored Feb 23, 2023
1 parent 99d6cae commit ad06e81
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class NavigationSplitCoordinator: CoordinatorProtocol, ObservableObject, CustomS
return
}

if sidebarModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

sidebarModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
}

Expand All @@ -151,6 +155,10 @@ class NavigationSplitCoordinator: CoordinatorProtocol, ObservableObject, CustomS
return
}

if detailModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

detailModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
}

Expand All @@ -164,6 +172,10 @@ class NavigationSplitCoordinator: CoordinatorProtocol, ObservableObject, CustomS
return
}

if sheetModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

sheetModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
}

Expand All @@ -177,6 +189,10 @@ class NavigationSplitCoordinator: CoordinatorProtocol, ObservableObject, CustomS
return
}

if fullScreenCoverModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

fullScreenCoverModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
}

Expand Down Expand Up @@ -509,6 +525,10 @@ class NavigationStackCoordinator: ObservableObject, CoordinatorProtocol, CustomS
return
}

if rootModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

popToRoot(animated: false)

rootModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
Expand Down Expand Up @@ -565,6 +585,10 @@ class NavigationStackCoordinator: ObservableObject, CoordinatorProtocol, CustomS
return
}

if sheetModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

sheetModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
}

Expand All @@ -584,6 +608,10 @@ class NavigationStackCoordinator: ObservableObject, CoordinatorProtocol, CustomS
return
}

if fullScreenCoverModule?.coordinator === coordinator {
fatalError("Cannot use the same coordinator more than once")
}

fullScreenCoverModule = NavigationModule(coordinator, dismissalCallback: dismissalCallback)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
emojiProvider: emojiProvider)
let coordinator = RoomScreenCoordinator(parameters: parameters)

detailNavigationStackCoordinator.setRootCoordinator(coordinator)
navigationSplitCoordinator.setDetailCoordinator(detailNavigationStackCoordinator) { [weak self, roomIdentifier] in
detailNavigationStackCoordinator.setRootCoordinator(coordinator) { [weak self, roomIdentifier] in
guard let self else { return }

// Move the state machine to no room selected if the room currently being dimissed
Expand All @@ -180,6 +179,10 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
self.detailNavigationStackCoordinator.setRootCoordinator(nil)
}
}

if navigationSplitCoordinator.detailCoordinator == nil {
navigationSplitCoordinator.setDetailCoordinator(detailNavigationStackCoordinator)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion UnitTests/Sources/NavigationSplitCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,13 @@ class NavigationSplitCoordinatorTests: XCTestCase {
// MARK: - Private

private func assertCoordinatorsEqual(_ lhs: CoordinatorProtocol?, _ rhs: CoordinatorProtocol?) {
if lhs == nil, rhs == nil {
return
}

guard let lhs = lhs as? SomeTestCoordinator,
let rhs = rhs as? SomeTestCoordinator else {
XCTFail("Coordinators are not the same")
XCTFail("Coordinators are not the same: \(String(describing: lhs)) != \(String(describing: rhs))")
return
}

Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-613.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix broken split layout room navigation

0 comments on commit ad06e81

Please sign in to comment.