This is made to try out :
- okhttp3 & parse adapter by moshi with Kotlin
- ConstraintLayout
- Splash Screen implementation
Basic : | Searching functionality : |
---|---|
(the troll face is a placeholder when there is no image source for that card in the JSON response)
Using moshi with data class allows me to write a least amount of code to parse the JSON response from this API
Data Class :
class Models {
data class MpApiJsonResponse(
@Json(name = "Basic") val Cards: List<Card> = listOf()
)
data class Card(
val cardId: String = "",
val name: String = "",
val img: String = "",
val type: String = "",
val playerClass: String = ""
)
}
Conversion using moshi :
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val responseAdapter = moshi.adapter(Models.MpApiJsonResponse::class.java)
...
...
...
// when the request is successful
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) callback.onFail(IOException("Unexpected code $response"))
callback.onSuccess(responseAdapter.fromJson(response.body()!!.source()))
}
(THEY ARE REALLY AWESOME !!!)