Skip to content

HarryChanLongChung/hearthStoneCardViewer

Repository files navigation

HearthStoneCardViewer - Kotlin

This is made to try out :

  • okhttp3 & parse adapter by moshi with Kotlin
  • ConstraintLayout
  • Splash Screen implementation

Demo

Basic : Searching functionality :

(the troll face is a placeholder when there is no image source for that card in the JSON response)

Code

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()))
}

Libraries

(THEY ARE REALLY AWESOME !!!)

About

Created to try out okHttp3 and moshi with Kotlin

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages