Skip to content

Commit

Permalink
fix: search now works due to navstack not properly deeply linking
Browse files Browse the repository at this point in the history
  • Loading branch information
ErrorErrorError committed Dec 27, 2023
1 parent 83cf953 commit ccf1004
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Sources/Features/Search/SearchFeature+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ extension SearchFeature.View {
Image(systemName: "chevron.up.chevron.down")
.font(.footnote.weight(.semibold))
}
.foregroundColor(selectedOptions.isEmpty ? nil : .white)
.foregroundColor(selectedOptions.isEmpty ? theme.textColor : .white)
.lineLimit(1)
.font(.footnote)
.padding(.horizontal, 12)
Expand Down
103 changes: 68 additions & 35 deletions Sources/Shared/Styling/NavStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
@_spi(Presentation)
import ComposableArchitecture
import Foundation
import FoundationHelpers
import OrderedCollections
import SwiftUI
import ViewComponents

extension Animation {
public static var navStackTransion: Animation { .timingCurve(0.31, 0.47, 0.31, 1, duration: 0.4) }
}

// MARK: - NavStack

public struct NavStack<State: Equatable, Action, Root: View, Destination: View>: View {
Expand Down Expand Up @@ -50,42 +47,30 @@ public struct NavStack<State: Equatable, Action, Root: View, Destination: View>:
} else {
#if os(iOS)
NavigationView {
ZStack {
root()
.themeable()

Group {
root()
.themeable()
.background {
WithViewStore(store, observe: \.ids, removeDuplicates: areOrderedSetsDuplicates) { viewStore in
ForEach(viewStore.state, id: \.self) { id in
NavigationLink(
isActive: .init(
get: { viewStore.state.contains(id) },
set: { isActive, transaction in
if !isActive, viewStore.state.contains(id) {
viewStore.send(.popFrom(id: id), transaction: transaction)
}
}
// Simulate "drilling down" for iOS 15
DrilledView(set: viewStore.state, index: viewStore.startIndex) { id, transaction in
if viewStore.state.contains(id) {
viewStore.send(.popFrom(id: id), transaction: transaction)
}
} destination: { id in
IfLetStore(
store.scope(
state: returningLastNonNilValue { $0[id: id] },
action: { .element(id: id, action: $0 as Action) }
)
) {
IfLetStore(
store.scope(
state: returningLastNonNilValue { $0[id: id] },
action: { .element(id: id, action: $0 as Action) }
)
) { store in
destination(store)
.themeable()
}
} label: {
EmptyView()
) { store in
destination(store)
.themeable()
}
.isDetailLink(false)
.hidden()
}
.hidden()
}
.hidden()
}
.hidden()
}
}
.navigationViewStyle(.stack)
#elseif os(macOS)
Expand Down Expand Up @@ -119,6 +104,54 @@ public struct NavStack<State: Equatable, Action, Root: View, Destination: View>:
}
}

@MainActor
private struct DrilledView<Destination: View>: View {
typealias Elements = OrderedSet<StackElementID>
let set: Elements
let index: Elements.Index
let popped: (Elements.Element, Transaction) -> Void
let destination: (Elements.Element) -> Destination

var id: Elements.Element? {
if set.startIndex <= index && index < set.endIndex {
set[index]
} else {
nil
}
}

@MainActor
var body: some View {
NavigationLink(
isActive: .init(
get: { id.flatMap(set.contains) ?? false },
set: { isActive, transaction in
if let id, !isActive {
popped(id, transaction)
}
}
)
) {
if let id {
destination(id)
.background(
Self(
set: set,
index: set.index(after: index),
popped: popped,
destination: destination
)
.hidden()
)
}
} label: {
EmptyView()
}
.isDetailLink(false)
.hidden()
}
}

extension NavStack {
public init(
_ store: Store<StackState<State>, StackAction<State, Action>>,
Expand Down Expand Up @@ -158,7 +191,7 @@ extension View {
@ViewBuilder destination: @escaping (_ store: Store<State, Action>) -> some View
) -> some View {
presentation(store: store) { `self`, $item, destinationContent in
if #available(iOS 16.0, macOS 13.0, *) {
if #available(iOS 18.0, macOS 13.0, *) {
self.navigationDestination(isPresented: $item.isPresent()) {
destinationContent(destination)
.themeable()
Expand Down

0 comments on commit ccf1004

Please sign in to comment.