title: How to disable / modify a button using SwiftUI?
body: <p>I want to create a POC using SwiftUI and CoreML. I use a simple button to call some function (called <code>test</code> here). This function is pretty heavy as it performs a CoreML inference, and it can take up to a minute to compute.</p>
  
  <p>I have several problems:</p>
  
  <ul>
  <li><p>The button is still active even when the computation is ongoing. In other words, if I click the button several times before the processing of the first click is finished, the processing will be performed several times. I want to disable the button as long as the processing is ongoing.</p></li>
  <li><p>I tried to modify the button's appearance to signify the user that the processing is ongoing. In the example bellow, I change the button color to red before calling the <code>test</code> function, and I change it back to blue when the processing is over. But it doesn't work.</p></li>
  </ul>
  
  <p>In the code bellow, the <code>test</code> function is just sleeping for 5 seconds to simulate the CoreML inference.</p>
  
  <pre class="lang-swift prettyprint-override"><code>func test() -&gt; Void {
      print("Start processing")
      sleep(5)
      print("End processing")
  }
  
  struct ContentView: View {
  
      @State private var buttonColor : Color = .blue
  
      var body: some View {
          VStack {
  
              Button(action: {
                  self.buttonColor = .red
                  test()
                  self.buttonColor = .blue
              }) {
                  Text("Start")
                      .font(.title)
                      .padding(.horizontal, 40)
                      .padding(.vertical, 5)
                      .background(self.buttonColor)
                      .foregroundColor(.white)
              }
          }
      }
  }
  </code></pre>
  
  <p>I guess this problem is very straight forward for most of you. I just can't find the correct search keywords to solve it by myself. Thanks for your help.</p>