Reputation: 21
When we open multiple windows in our app, kill the app, and relaunch it, shouldn't the previously opened windows pop automatically based on state restoration principles?
Here is my main:
@main
struct WindowTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
WindowGroup("Title", for: Person.ID.self){ $id in
TestView(personID: $id)
}
}
}
My window open button:
struct OpenWindowButton: View {
@Environment(\.openWindow) var openWindow
let person: Person
var body: some View {
Button("Open new window"){
openWindow(value: person.id)
}
}
}
My model:
struct Person:Identifiable, Hashable{
let name: String
let age: Int
var id: UUID
}
let people:[Person] = [
Person(name: "Jack", age: 41, id: UUID()),
Person(name: "Mary", age: 32, id: UUID()),
Person(name: "Linda", age: 18, id: UUID()),
Person(name: "Joe", age: 13, id: UUID()),
Person(name: "Kid", age: 1, id: UUID())
]
My ContentView:
struct ContentView: View {
var body: some View {
List{
ForEach(people, id:\.self){person in
VStack{
Text(person.name)
}.contextMenu{
OpenWindowButton(person: person)
}
}
}
}
}
My TestView
struct TestView: View {
@Binding var personID: Person.ID?
var body: some View {
if let personFromID = people.first(where: {$0.id == personID}){
VStack {
Text(personFromID.name)
Text("\(personFromID.age)")
}
}else{
Text("no person for you")
}
}
}
So, according to the documentation, SwiftUI should enable state restoration based on the binding value passed to the view. But when I relaunch the app, the popped windows show 'no person for you' instead of previously populated person info.
Any ideas are muchly appreciated!
Upvotes: 1
Views: 255