Skip to content

Commit

Permalink
Merge pull request #57 from skiptools/postpathviews
Browse files Browse the repository at this point in the history
Maintain nav stack properly when views are appended past path binding
  • Loading branch information
aabewhite authored Sep 17, 2024
2 parents da90316 + 9c38bb8 commit ba340e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
25 changes: 24 additions & 1 deletion Sources/SkipUI/SkipUI/Containers/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,12 @@ struct NavigationDestination {

/// Pop the back stack.
func navigateBack() {
if let path {
// Check for a view destination before we pop our path bindings, because the user could push arbitrary views
// that are not represented in the bound path
let viewDestinationPrefix = Self.route(for: viewDestinationIndex, valueString: "")
if navController.currentBackStackEntry?.destination.route?.hasPrefix(viewDestinationPrefix) == true {
navController.popBackStack()
} else if let path {
path.wrappedValue.popLast()
} else if let navigationPath {
navigationPath.wrappedValue.removeLast()
Expand Down Expand Up @@ -678,6 +683,24 @@ struct NavigationDestination {
pathIndex += 1
backStackIndex += 1
}

// If we exhausted the path and the back stack contains only post-path views, keep them in place. This allows
// users to have a path binding but then append arbitrary views as leaves
var hasOnlyTrailingViews = false
if pathIndex == path.count {
hasOnlyTrailingViews = true
let viewDestinationPrefix = Self.route(for: viewDestinationIndex, valueString: "")
for i in 0..<(backStack.count() - backStackIndex) {
if backStack[backStackIndex + i].destination.route?.hasPrefix(viewDestinationPrefix) != true {
hasOnlyTrailingViews = false
break
}
}
}
guard !hasOnlyTrailingViews else {
return
}

// Pop back to last common value
for _ in 0..<(backStack.count() - backStackIndex) {
navController.popBackStack()
Expand Down
2 changes: 1 addition & 1 deletion Tests/SkipUITests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ImageTests: XCSnapshotTestCase {
.frame(width: 16.0, height: 16.0)
}

func DISABLEDtestSystemImageStar() throws {
func testSystemImageStar() throws {
let macOSStar: String
if #available(macOS 14, *) {
macOSStar = """
Expand Down
4 changes: 2 additions & 2 deletions Tests/SkipUITests/SkipUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ final class SkipUITests: SkipUITestCase {

throw XCTSkip("UI testing not yet supported on Darwin platforms")
#else
//rule.onNodeWithTag(id).assert(hasTextExactly(text))
rule.onNodeWithTag(id).assert(hasTextExactly(text))
#endif
}

Expand Down Expand Up @@ -272,7 +272,7 @@ final class SkipUITests: SkipUITestCase {
moveTo(Offset(Float(1000.0), Float(20.0)))
up()
}
//rule.onNodeWithTag("label").assert(hasTextExactly("100%"))
rule.onNodeWithTag("label").assert(hasTextExactly("100%"))
#endif
try check(rule, id: "label", hasText: "100%")
})
Expand Down

0 comments on commit ba340e9

Please sign in to comment.