Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Created April 18, 2021 19:02
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/f937bbdb963eed0d914add83cd7a1106 to your computer and use it in GitHub Desktop.
Save Skyyo/f937bbdb963eed0d914add83cd7a1106 to your computer and use it in GitHub Desktop.
class CurrenciesViewModel : ViewModel() {
private val _currencyPrices = MutableStateFlow(listOf<CurrencyPrice>())
val currencyPrices: StateFlow<List<CurrencyPrice>> get() = _currencyPrices
private val producers: MutableMap<Int, Job> = mutableMapOf()
init {
getCurrencyPrices()
}
private fun getCurrencyPrices() {
viewModelScope.launch(Dispatchers.Default) {
val initialCurrencyPrices = arrayListOf<CurrencyPrice>()
run loop@{
Currency.getAvailableCurrencies().forEachIndexed { index, currency ->
if (index == 100) return@loop
initialCurrencyPrices += CurrencyPrice(
id = index,
name = "1 USD to ${currency.currencyCode}",
price = Random.nextInt(0, 100),
priceFluctuation = PriceFluctuation.UNKNOWN
)
}
}
_currencyPrices.emit(initialCurrencyPrices)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment