Skip to content

Commit 3b5fc8c

Browse files
committed
feat: popTo
1 parent c20cc50 commit 3b5fc8c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Sources/GoodCoordinator/Router.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ private extension AnyReactor {
6565
navigationPath.cleanup()
6666
}
6767

68-
// public func popTo<R: Reactor>(_ reactor: R.Type) {
69-
// let lastReactorIndex = navigationPath.count - 1
70-
// let poppingToReactorAtIndex = navigationPath.keys.lastIndex(where: { $0 is R }) ?? lastReactorIndex
71-
// navigationPath.removeLast(lastReactorIndex - poppingToReactorAtIndex)
72-
// }
68+
public func popTo<R: Reactor>(_ reactor: R.Type) {
69+
var currentNode = navigationPath.lastActiveNode
70+
while var parent = currentNode.parent, !parent.value.isTabs {
71+
let parentReactor = parent.value.reactor
72+
if parentReactor?.is(ofType: reactor) ?? false {
73+
parent.value.mutator?(nil)
74+
return
75+
} else {
76+
currentNode = parent
77+
}
78+
}
79+
}
7380

7481
}
7582

Sources/GoodCoordinator/Structures/Tree.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Collections
99

1010
public typealias Tree<T> = TreeNode<T>
1111

12+
// MARK: - Class
13+
1214
public class TreeNode<T> {
1315
public var value: T
1416

@@ -29,6 +31,8 @@ public class TreeNode<T> {
2931
}
3032
}
3133

34+
// MARK: - Depth
35+
3236
extension TreeNode {
3337

3438
var depth: Int {
@@ -43,6 +47,8 @@ extension TreeNode {
4347

4448
}
4549

50+
// MARK: - String description
51+
4652
extension TreeNode: CustomStringConvertible {
4753

4854
public var description: String {
@@ -55,6 +61,8 @@ extension TreeNode: CustomStringConvertible {
5561

5662
}
5763

64+
// MARK: - Search
65+
5866
extension TreeNode {
5967

6068
public func depthFirstSearch(_ value: T, predicate: (T, T) -> Bool) -> TreeNode? {

0 commit comments

Comments
 (0)