Last active
November 25, 2022 14:26
-
-
Save filimo/a3b5a92c6a0c7b670b97604cd70c521b to your computer and use it in GitHub Desktop.
With case detail(num: Int, items: Binding<[DebugItem]>)
import SwiftUI
extension Binding: Equatable where Value: Hashable {
public static func == (lhs: Binding<Value>, rhs: Binding<Value>) -> Bool {
lhs.wrappedValue == rhs.wrappedValue
}
}
extension Binding: Hashable where Value: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(wrappedValue)
}
}
struct DebugItem: Hashable {
let id: Int
let name: String
var selected: Bool = true
}
enum NavTestRoute: Hashable {
case child(DebugItem)
case detail(num: Int, items: Binding<[DebugItem]>)
}
struct NavTestChildView: View {
let item: DebugItem
var body: some View {
Text("NavTestChildView \(String(describing: item))")
}
}
struct NavTestMainView_Control: View {
let num: Int
let isDetailMode: Bool
@Binding var items: [DebugItem]
var body: some View {
VStack {
if isDetailMode {
Text("Long mode \(num)")
ForEach($items, id: \.self) { $item in
NavigationLink(value: NavTestRoute.child(item)) {
HStack {
Text("Go to \(item.name)")
Toggle("", isOn: $item.selected)
}
}
}
} else {
Text("Short mode \(num)")
}
}
}
}
struct NavTestMainView2: View {
let num: Int
@State var items: [DebugItem] = []
var body: some View {
NavigationLink(value: NavTestRoute.detail(num: num, items: $items)) {
NavTestMainView_Control(num: num, isDetailMode: false, items: $items)
}
.task {
guard items.isEmpty else { return }
items = [
.init(id: 1, name: "item1"),
.init(id: 2, name: "item2"),
.init(id: 3, name: "item3")
]
}
}
}
struct NavTestMainView: View {
var body: some View {
NavigationStack {
ForEach(1 ..< 5) { num in
NavTestMainView2(num: num)
}
.navigationDestination(for: NavTestRoute.self) { route in
switch route {
case let .detail(num, items):
NavTestMainView_Control(num: num, isDetailMode: true, items: items)
case let .child(item):
NavTestChildView(item: item)
}
}
}
}
}
поменял на extension Binding: Equatable where Value: Hashable {
иначе ломается .searchable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Я не уверен насчет этого, но вроде бы прекрасно работает, правда ломается биндинг внутри чайлда, но это уже совершенно другая задача(не подтягивает изменения на лету)