Skip to content

Commit

Permalink
Merge branch 'simba909/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpatrickmorgan committed Mar 29, 2024
2 parents 55a3718 + fbc03de commit add71eb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Sources/NavigationBackport/NBNavigationLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct NBNavigationLink<P: Hashable, Label: View>: View {
var value: P?
var label: Label

@EnvironmentObject var pathAppender: PathAppender
@EnvironmentObject var pathHolder: Unobserved<NavigationPathHolder>

public init(value: P?, @ViewBuilder label: () -> Label) {
self.value = value
Expand All @@ -20,7 +20,7 @@ public struct NBNavigationLink<P: Hashable, Label: View>: View {
Button(
action: {
guard let value = value else { return }
pathAppender.append?(value)
pathHolder.object.path.append(value)
},
label: { label }
)
Expand Down
26 changes: 10 additions & 16 deletions Sources/NavigationBackport/NBNavigationStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public struct NBNavigationStack<Root: View, Data: Hashable>: View {
@Binding var externalTypedPath: [Data]
@State var internalTypedPath: [Data] = []
@StateObject var path: NavigationPathHolder
@StateObject var pathAppender = PathAppender()
@StateObject var destinationBuilder = DestinationBuilderHolder()
@Environment(\.useNavigationStack) var useNavigationStack
var root: Root
Expand All @@ -21,33 +20,28 @@ public struct NBNavigationStack<Root: View, Data: Hashable>: View {
}
}

@ViewBuilder
var content: some View {
pathAppender.append = { [weak path] newElement in
path?.path.append(newElement)
}
if #available(iOS 16.0, *, macOS 13.0, *, watchOS 9.0, *, tvOS 16.0, *), useNavigationStack == .whenAvailable {
return AnyView(
NavigationStack(path: $path.path) {
root
.navigationDestination(for: AnyHashable.self, destination: { destinationBuilder.build($0) })
.navigationDestination(for: LocalDestinationID.self, destination: { destinationBuilder.build($0) })
}
.environment(\.isWithinNavigationStack, true)
)
}
return AnyView(
NavigationStack(path: $path.path) {
root
.navigationDestination(for: AnyHashable.self, destination: { destinationBuilder.build($0) })
.navigationDestination(for: LocalDestinationID.self, destination: { destinationBuilder.build($0) })
}
.environment(\.isWithinNavigationStack, true)
} else {
NavigationView {
Router(rootView: root, screens: $path.path)
}
.navigationViewStyle(supportedNavigationViewStyle)
.environment(\.isWithinNavigationStack, false)
)
}
}

public var body: some View {
content
.environmentObject(path)
.environmentObject(pathAppender)
.environmentObject(Unobserved(object: path))
.environmentObject(destinationBuilder)
.environmentObject(Navigator(useInternalTypedPath ? $internalTypedPath : $externalTypedPath))
.onFirstAppear {
Expand Down
7 changes: 0 additions & 7 deletions Sources/NavigationBackport/PathAppender.swift

This file was deleted.

10 changes: 10 additions & 0 deletions Sources/NavigationBackport/Unobserved.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftUI

/// A wrapper that allows access to an observable object without publishing its changes.
class Unobserved<Object: ObservableObject>: ObservableObject {
let object: Object

init(object: Object) {
self.object = object
}
}

0 comments on commit add71eb

Please sign in to comment.