Skip to content

Commit

Permalink
4.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 14, 2024
1 parent c204824 commit 8a7dbab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDFlow.git", from: "4.8.0")
.package(url: "https://github.com/dankinsoid/VDFlow.git", from: "4.9.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDFlow"])
Expand Down
55 changes: 41 additions & 14 deletions Sources/VDFlow/NavigationSteps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import SwiftUI
public struct NavigationSteps<Selection: Hashable, Content: View>: View {

let content: Content
@StateOrBinding var selection: Selection
@StateOrBinding var selection: Selection?
@State private var ids: [Selection] = []

public init(selection: Binding<Selection>, @ViewBuilder content: () -> Content) {
public init(selection: Binding<Selection?>, @ViewBuilder content: () -> Content) {
self.content = content()
self._selection = .binding(selection)
}
Expand All @@ -27,14 +27,28 @@ public struct NavigationSteps<Selection: Hashable, Content: View>: View {
}
}

public extension NavigationSteps where Selection == Int {
public extension NavigationSteps {

init(@ViewBuilder content: () -> Content) {
self._selection = StateOrBinding(wrappedValue: 0)
self._selection = StateOrBinding(wrappedValue: nil)
self.content = content()
}
}

public extension NavigationSteps where Selection == Int {

init(selection: Binding<Selection>, @ViewBuilder content: () -> Content) {
self.init(
selection: Binding<Selection?> {
selection.wrappedValue
} set: {
selection.wrappedValue = $0 ?? 0
},
content: content
)
}
}

private struct StepTag: _ViewTraitKey {

static var defaultValue: AnyHashable = Optional<Int>.none
Expand Down Expand Up @@ -70,7 +84,7 @@ private final class UINavigationStackDelegate: NSObject, UINavigationControllerD

private struct MyNavigationStack<Selection: Hashable>: UIViewControllerRepresentable {

@Binding var selection: Selection
@Binding var selection: Selection?
let children: _VariadicView.Children
@State private var delegate = UINavigationStackDelegate()

Expand All @@ -86,11 +100,13 @@ private struct MyNavigationStack<Selection: Hashable>: UIViewControllerRepresent
) {
guard !delegate.notUpdate else { return }
delegate.didShow = { [weak delegate, $selection] controller in
delegate?.notUpdate = true
if let tag = controller.stackTag?.id.base as? Selection, tag != $selection.wrappedValue {
delegate?.notUpdate = true
$selection.wrappedValue = tag
delegate?.notUpdate = false
} else if controller === stack.viewControllers.first {
$selection.wrappedValue = nil
}
delegate?.notUpdate = false
}
guard let selectedIndex else { return }
if stack.view.window != nil {
Expand Down Expand Up @@ -121,19 +137,30 @@ private struct MyNavigationStack<Selection: Hashable>: UIViewControllerRepresent
}
return controller
}

func tag(of child: _VariadicView.Children.Element, _ i: Int) -> Selection? {
(child.stepTag.base as? Selection) ?? (i as? Selection)
}

func pop(offset: Int) {
guard let selectedIndex else { return }
let newIndex = max(0, min(selectedIndex - offset, children.count - 1))
guard let tag = tag(of: children[newIndex], newIndex) else { return }
guard let tag = tag(of: children[newIndex], newIndex) else {
if newIndex == 0 {
selection = nil
}
return
}
selection = tag
}

var selectedIndex: Int? {
guard !children.isEmpty else {
return nil
}
guard let selection else {
return 0
}
let tags = children.enumerated().map {
(tag(of: $0.element, $0.offset), $0.offset)
}
Expand Down Expand Up @@ -210,7 +237,7 @@ enum NavStackPreview: PreviewProvider {

struct Previews: View {

@State var selection = 0
@State var selection: Int = 0

var body: some View {
NavigationSteps(selection: $selection) {
Expand All @@ -221,12 +248,12 @@ enum NavStackPreview: PreviewProvider {
.previewOverlay()
}
}

struct Page: View {
@Environment(\.pop) var pop
let i: Int
@Binding var selection: Int

var body: some View {
HStack {
if selection > 0 {
Expand Down

0 comments on commit 8a7dbab

Please sign in to comment.