title: SwiftUI Dependency injection with environment object
body: <p>I'm trying to create som kind of solution for injecting dependencies to SwiftUI view models and I have no idea whether I'm on to something or if I need to take this back to the drawing table.</p>
  <pre><code>struct MyAppName: App {
  
  private static var serviceToInject = ServiceToInject()
  
  @StateObject var viewModel1 = ViewModel1(service: serviceToInject)
  @StateObject var viewModel2 = ViewModel2(service: serviceToInject)
  
  var body: some Scene {
      WindowGroup {
          if authenticationViewModel.isLoggedIn {
              HomeView()
                  .environmentObject(viewModel1)
                  .environmentObject(viewModel2)
          } else {
              LoginView()
                  .environmentObject(viewModel2)
          }
      }
  }
  </code></pre>
  <p>Is this a possible to solution? I'm concerned about the use of static on the service. Is this maybee a better way to do it:</p>
  <pre><code>struct MyAppName: App {
  
  @StateObject var viewModel1 = ViewModel1(service: ServiceToInject())
  @StateObject var viewModel2 = ViewModel2(service: ServiceToInject())
  
  var body: some Scene {
      WindowGroup {
          if authenticationViewModel.isLoggedIn {
              HomeView()
                  .environmentObject(viewModel1)
                  .environmentObject(viewModel2)
          } else {
              LoginView()
                  .environmentObject(viewModel2)
          }
      }
  }
  </code></pre>