Skip to content

Commit

Permalink
feat: popTo
Browse files Browse the repository at this point in the history
  • Loading branch information
plajdo committed Nov 15, 2024
1 parent c20cc50 commit 3b5fc8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Sources/GoodCoordinator/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ private extension AnyReactor {
navigationPath.cleanup()
}

// public func popTo<R: Reactor>(_ reactor: R.Type) {
// let lastReactorIndex = navigationPath.count - 1
// let poppingToReactorAtIndex = navigationPath.keys.lastIndex(where: { $0 is R }) ?? lastReactorIndex
// navigationPath.removeLast(lastReactorIndex - poppingToReactorAtIndex)
// }
public func popTo<R: Reactor>(_ reactor: R.Type) {
var currentNode = navigationPath.lastActiveNode
while var parent = currentNode.parent, !parent.value.isTabs {
let parentReactor = parent.value.reactor
if parentReactor?.is(ofType: reactor) ?? false {
parent.value.mutator?(nil)
return
} else {
currentNode = parent
}
}
}

}

Expand Down
8 changes: 8 additions & 0 deletions Sources/GoodCoordinator/Structures/Tree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Collections

public typealias Tree<T> = TreeNode<T>

// MARK: - Class

public class TreeNode<T> {
public var value: T

Expand All @@ -29,6 +31,8 @@ public class TreeNode<T> {
}
}

// MARK: - Depth

extension TreeNode {

var depth: Int {
Expand All @@ -43,6 +47,8 @@ extension TreeNode {

}

// MARK: - String description

extension TreeNode: CustomStringConvertible {

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

}

// MARK: - Search

extension TreeNode {

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

0 comments on commit 3b5fc8c

Please sign in to comment.