What it's the recommended way to support multiple windows without sharing state?

Im currently working with iOS14b4 and Xcode 12b4 and given this basic setup, it can be seen that any change on AppState it's propagated to every scene instances.
So my question is if anybody knows a solution that avoids the unwanted store sharing on the multi window scenario.

struct AppState: Equatable {
  var searchQuery = ""
}

enum AppAction: Equatable {
  case searchQueryChanged(String)
}

struct AppEnvironment {
  var mainQueue: AnySchedulerOf<DispatchQueue>
}

let appReducer = Reducer<AppState, AppAction, AppEnvironment> { state, action, environment in
  switch action {
  case let .searchQueryChanged(query):
    state.searchQuery = query
    return .none
  }
}
struct ContentView: View {
  
  let store: Store<AppState, AppAction>
  
  var body: some View {
    WithViewStore(store) { viewStore in
      TextField(
        "Berlin, Amsterdam, ...",
        text: viewStore.binding(
          get: { $0.searchQuery },
          send: AppAction.searchQueryChanged
        )
      ).padding()
    }
  }
}
@main
struct MultiWindowApp: App {
  
  let store = Store(
    initialState: AppState(),
    reducer: appReducer,
    environment: AppEnvironment(mainQueue: DispatchQueue.main.eraseToAnyScheduler())
  )
  
  var body: some Scene {
    WindowGroup {
      ContentView(store: store)
    }
  }
}
1 Like

Did you ever find the solution? I would have thought moving the store initialization to the ContentView itself it would cause two states to be created.

Could we try passing a scoped store using a forEach from an IdentifiedArray as a parameter to the content view? Keyed somehow to the tag or id of the instance of the WindowGroup?

Terms of Service

Privacy Policy

Cookie Policy