Home > Mobile >  How change List<Unit> type to proper one in kotlin
How change List<Unit> type to proper one in kotlin

Time:05-17

I have an issue with getting only information with passed coinList values. To begin with, I want to make coinList to List<Coin> instead of List<Unit> and I am out of ideas on how to change it. I want to compare it.id to the coinList if it has the same values then pass it to the function coin. Thank you in advance!

class GetCoinsUseCase @Inject constructor(
    private val repository: CoinRepository
){
    private val coinList = listOf("btc-bitcoin", "usdt-tether", "eth-ethereum")

     operator fun invoke(): Flow<Resource<List<Coin>>> = flow{
        try{
            emit(Resource.Loading<List<Coin>>())
            val coins = repository.getCoins().map { if (it.id.equals(coinList)){ it.toCoin() }}
            emit(Resource.Success<List<Coin>>(coins))
        }catch (e: HttpException){
            emit(Resource.Error<List<Coin>>(e.localizedMessage ?: "Error occurred"))
        }
     }
}

CodePudding user response:

maybe something like this would work, first doing a filter to only retrieve relevant items, then mapping

    val coinsList = arrayListOf<String>("foo", "bar")
    val coins = repository.getCoins().filter { it.id in coinsList }.map {
        it.toCoin()
    }
  • Related