|
// |
|
// ContentView.swift |
|
// nav-repro |
|
// |
|
// Created by Willis Plummer on 3/31/20. |
|
// Copyright © 2020 Willis Plummer. All rights reserved. |
|
// |
|
|
|
import SwiftUI |
|
|
|
struct Thing2: Hashable { |
|
let id: Int |
|
var name: String |
|
|
|
init(id: Int, name: String) { |
|
self.id = id |
|
self.name = name |
|
} |
|
} |
|
|
|
struct Thing: Hashable { |
|
let id: Int |
|
var arr: [Thing2] |
|
|
|
init(id: Int, arr: [Thing2]) { |
|
self.id = id |
|
self.arr = arr |
|
} |
|
} |
|
|
|
final class Store: ObservableObject { |
|
@Published private(set) var value: [Thing] |
|
|
|
init( |
|
initialValue: [Thing] |
|
) { |
|
self.value = initialValue |
|
} |
|
|
|
func changeInnerValue() { |
|
self.value = self.value.map {thing1 -> Thing in |
|
var copyThing1 = thing1 |
|
copyThing1.arr = copyThing1.arr.map {x -> Thing2 in |
|
var c = x |
|
c.name = "blahhh \(NSDate().timeIntervalSince1970)" |
|
print(c.name) |
|
return c |
|
} |
|
return copyThing1 |
|
} |
|
} |
|
} |
|
|
|
|
|
struct ContentView: View { |
|
@ObservedObject var store: Store |
|
|
|
init(store: Store) { |
|
self.store = store |
|
} |
|
|
|
func nestedView(idx: Int) -> some View { |
|
List { |
|
ForEach(self.store.value[idx].arr, id: \.self) { thing2 in |
|
NavigationLink(destination: ThirdView()) { |
|
Text("\(thing2.name)") |
|
} |
|
} |
|
}.navigationBarItems(trailing: |
|
Button("update") { |
|
self.store.changeInnerValue() |
|
} |
|
).onAppear { |
|
print("APPEARS") |
|
self.store.changeInnerValue() |
|
} |
|
|
|
} |
|
|
|
|
|
var body: some View { |
|
NavigationView{ |
|
List { |
|
ForEach(0..<self.store.value.count) { idx in |
|
NavigationLink(destination: self.nestedView(idx: idx)) { |
|
Text("\(self.store.value[idx].id)") |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
struct ThirdView: View { |
|
var body: some View { |
|
Text("Some Stuff To See") |
|
} |
|
} |
|
|
|
struct ContentView_Previews: PreviewProvider { |
|
static var previews: some View { |
|
ContentView(store: Store(initialValue: [Thing(id: 1, arr: [Thing2(id: 12, name: "BLAH")])])) |
|
} |
|
} |