title: SwiftUI: How to show next view after button click + API call
body: <p>It might sound like a trivial task but I can't find a proper solution for this problem. Possibly I haven't internalized the "SwiftUI-ish" way of thinking yet.</p>
  
  <p>I have a view with a button. When the view loads, there is a condition (already logged in?) under which the view should directly go to the next view. If the button is clicked, an API call is triggered (login) and if it was successful, the redirect to the next view should also happen.</p>
  
  <p>My attempt was to have a model (ObservableObject) that holds the variable "shouldRedirectToUploadView" which is a PassThroughObject. Once the condition onAppear in the view is met or the button is clicked (and the API call is successful), the variable flips to true and tells the observer to change the view.</p>
  
  <p>Flipping the "shouldRedirectToUploadView" in the model seems to work but I can't make the view re-evaluate that variable so the new view won't open.</p>
  
  <p>Here is my implementation so far:</p>
  
  <p><strong>The model</strong></p>
  
  <pre><code>import SwiftUI
  import Combine
  
  class SboSelectorModel: ObservableObject {
  
      var didChange = PassthroughSubject&lt;Void, Never&gt;()
  
      var shouldRedirectToUpdateView = false {
          didSet {
              didChange.send()
          }
      }
  
      func fetch(_ text: String) {
          DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
              self.shouldRedirectToUpdateView = true
          }
      }
  }
  </code></pre>
  
  <p><strong>The view</strong></p>
  
  <pre><code>import SwiftUI
  
  struct SboSelectorView: View {
      @State var text: String = ""
      @ObservedObject var model: SboSelectorModel
  
      var body: some View {
          return ZStack {
              if (model.shouldRedirectToUpdateView) {
                  UpdateView()
              }
              else {
                  Button(action: {
                      self.reactOnButtonClick()
                  }) {
                      Text("Start")
                  }
              }
          }.onAppear(perform: initialActions)
      }
  
      public func initialActions() {
          self.model.shouldRedirectToUpdateView = true
      }
  
      private func reactOnButtonClick() {
          self.model.fetch()
      }
  }
  </code></pre>
  
  <p>In good old UIKit I would have just used a ViewController to catch the action of button click and then put the new view on the navigation stack. How would I do it in SwiftUI?</p>
  
  <p>In the above example I would expect the view to load, execute the <strong>onAppear()</strong> function which executes <strong>initialActions()</strong> to flip the model variable what would make the view react to that change and present the <strong>UploadView</strong>. Why doesn't it happen that way?</p>
  
  <p>There are SO examples like <a href="https://stackoverflow.com/questions/56754117/programatically-navigate-to-new-view-in-swiftui?rq=1">Programatically navigate to new view in SwiftUI</a> or <a href="https://stackoverflow.com/questions/56966739/show-a-new-view-from-button-press-swift-ui">Show a new View from Button press Swift UI</a> or <a href="https://stackoverflow.com/questions/56527840/how-to-present-a-view-after-a-request-with-urlsession-in-swiftui?rq=1">How to present a view after a request with URLSession in SwiftUI?</a> that suggest the same procedure. However it does not seem to work for me. Am I missing something?</p>
  
  <p>Thank you in advance!</p>