Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active April 18, 2021 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skyyo/1a22100995cf05a08973f0b5d3d5be14 to your computer and use it in GitHub Desktop.
Save Skyyo/1a22100995cf05a08973f0b5d3d5be14 to your computer and use it in GitHub Desktop.
private suspend fun observePriceUpdateFlow(index: Int, currencyPriceUpdateFlow: Flow<Int>) {
currencyPriceUpdateFlow.collect { newPrice ->
val newFluctuation = when {
newPrice > _currencyPrices.value[index].price -> PriceFluctuation.UP
else -> PriceFluctuation.DOWN
}
val updatedCurrencyPrice = _currencyPrices.value[index].copy(
price = newPrice,
priceFluctuation = newFluctuation
)
val mutableCurrencyPrices = _currencyPrices.value.toMutableList()
mutableCurrencyPrices[index] = updatedCurrencyPrice
_currencyPrices.value = mutableCurrencyPrices.toList()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment