I have the following JSON:
{
"Response": "Success",
"Message": "Coin list succesfully returned!",
"BaseImageUrl": "https://www.cryptocompare.com",
"BaseLinkUrl": "https://www.cryptocompare.com",
"Data": {
"LTC": {
"Id": "3808",
"Url": "/coins/ltc/overview",
"ImageUrl": "/media/19782/ltc.png",
"Name": "LTC",
"CoinName": "Litecoin",
"FullName": "Litecoin (LTC)",
"Algorithm": "Scrypt",
"ProofType": "PoW",
"SortOrder": "2"
}
...
},
"Type": 100
}
In order for Moshi to parse the data
JSON object, since it's a dynamic list, I have created a class:
data class Data(@Json(name = "Data") val cryptoCoinMap: Map<String, CryptoCoin>)
The problem is that I want to show a list of CryptoCoin
using a RecyclerView
adapter, so I would rather just show a List
, and not a Map
.
I have been trying to come up with a solution in RxJava, to be able to modify in the CryptoCoin
model an have the Data
val be a List
rather than a Map
, but unfortunately I'm stuck.
Does anybody have a better approach or idea about how to improve this?
This is what I have come up with so far:
var cryptoCoinList = mutableListOf<CryptoCoin>()
coinRepository
.getCoinList()
.concatMapIterable { t: CryptoCoinList -> t.data.cryptoCoinMap.toList() }
.map { t: Pair<String, CryptoCoin> -> cryptoCoinList.add(t.second) }
// INSERT TRANSFORMATION HERE
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onNext = {
coinListData.postValue(it.second)
})
Thanks a lot in advance!