title: Why this function is called multiple times in Jetpack Compose?
body: <p>I'm currently trying out Android Compose. I have a Text that shows price of a crypto coin. If a price goes up the color of a text should be green, but if a price goes down it should be red. The function is called when a user clicks a button. The problem is that the function <code>showPrice()</code> is called multiple times (sometimes just once, sometimes 2-4 times). And because of that the user can see the wrong color.  What can I do to ensure that it's only called once?</p>
  <p><strong>MainActivity:</strong></p>
  <pre><code>@Composable
  fun MainScreen() {
      val priceLiveData by viewModel.trackLiveData.observeAsState()
      val price = priceLiveData ?: return
  
      when (price) {
                  is ViewState.Response -&gt; showPrice(price = price.data)
                  is ViewState.Error -&gt; showError(price.text)
              }
  
      Button(onClick = {viewModel.post()} )
  }
  
  @Composable
  private fun showPrice(price: Double) {
      lastPrice = sharedPref.getFloat(&quot;eth&quot;, 0f).toDouble()
      val color by animateColorAsState(if (price &gt;= (lastPrice)) Color.Green else 
  Color.Red)
      Log.v(&quot;TAG&quot;, &quot;last=$lastPrice new = $price&quot;)
      editor.putFloat(&quot;eth&quot;, price.toFloat()).apply()
      Text(
            text = price.toString(),
            color = color,
            fontSize = 28.sp,
            fontFamily = fontFamily,
            fontWeight = FontWeight.Bold
        )
   }
  </code></pre>
  <p><strong>ViewModel:</strong></p>
  <pre><code>  @HiltViewModel
    class MyViewModel @Inject constructor(
      private val repository: Repository
  ): ViewModel() {
  
     private val _trackLiveData: MutableLiveData&lt;ViewState&lt;Double&gt;&gt; = MutableLiveData()
     val trackLiveData: LiveData&lt;ViewState&lt;Double&gt;&gt;
         get() = _trackLiveData
  
     fun post(
     ) = viewModelScope.launch(Dispatchers.Default) {
         try {
             val response = repository.post()
             _trackLiveData.postValue(ViewState.Response(response.rate.round(7)))
         } catch (e: Exception) {
             _trackLiveData.postValue(ViewState.Error())
             Log.v(&quot;TAG: viewmodelPost&quot;, e.message.toString())
      }
    }
  }
  </code></pre>
  <p><strong>ViewState:</strong></p>
  <pre><code>sealed class ViewState&lt;out T : Any&gt; {
      class Response&lt;out T : Any&gt;(val data: T): ViewState&lt;T&gt;()
      class Error(val text:String = &quot;Unknown error&quot;): ViewState&lt;Nothing&gt;()
  }
  </code></pre>
  <p>So when I press Button to call showPrice(). I can see these lines on Log:</p>
  <pre><code>2021-06-10 16:39:18.407 16781-16781/com.myapp.myapp V/TAG: last=2532.375732421875 new = 2532.7403716
  2021-06-10 16:39:18.438 16781-16781/com.myapp.myapp V/TAG: last=2532.740478515625 new = 2532.7403716
  2021-06-10 16:39:18.520 16781-16781/com.myapp.myapp V/TAG: last=2532.740478515625 new = 2532.7403716
  </code></pre>