Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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