StarWars SDK integrates can be used to get Planet, People, & Films list of Star Wars.
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
dependencies {
implementation 'com.github.debojyoti452:stars_war_demo_sdk:${version}'
}
- Response has three Types
-> Response.Success // will return success model. Typed Request Model will be returned.
-> Response.Error // will return if any error found. String message will be returned.
-> Response.Loading // will return Loading State. Show any progress dialog.
- People & Planet Model
-> getEyeColors() // is prebuilt function available in People Model which will return an List<eyecolor: String?>? of first 5 people.
-> getFiveClimateAndPopulation() // is prebuilt function available in Planet Model which will return an List<Pair<climate: String?, population: String?>>? of first 5 planets.
- Initiate StarWars SDK
private val initiateStarWars: StarWars by lazy {
StarWars.Builder()
.setContext(requireContext())
.create()
}
initiateStarWars.getPeople(peopleOnResponseListener)
private val peopleOnResponseListener = object : ResponseOnListener<Response<People>> {
override fun onResult(result: Response<People>) {
when (result) {
is Response.Success -> {
_loading.postValue(false)
_peopleEyeData.postValue(result.data.getEyeColors())
}
is Response.Error -> {
_loading.postValue(false)
_errorMessage.postValue(Pair(true, result.exception.toString()))
}
is Response.Loading -> {
_loading.postValue(true)
}
}
}
}
initiateStarWars.getPlanets(planetOnResponseListener)
private val planetOnResponseListener = object : ResponseOnListener<Response<Planet>> {
override fun onResult(result: Response<Planet>) {
when (result) {
is Response.Success -> {
_loading.postValue(false)
_populationData.postValue(result.data.getFiveClimateAndPopulation())
}
is Response.Error -> {
_loading.postValue(false)
_errorMessage.postValue(Pair(true, result.exception.toString()))
}
is Response.Loading -> {
_loading.postValue(true)
}
}
}
}
initiateStarWars.getFilm(filmOnResponseListener)
private val filmOnResponseListener = object : ResponseOnListener<Response<Film>> {
override fun onResult(result: Response<Film>) {
when (result) {
is Response.Success -> {
_filmData.postValue(result.data.dataList)
_loading.postValue(false)
}
is Response.Error -> {
_loading.postValue(false)
_errorMessage.postValue(Pair(true, result.exception.toString()))
}
is Response.Loading -> {
_loading.postValue(true)
}
}
}
}
-
If you're using MVC or MVP architecture or Java without Viewmodel then you need to use it like this:
starWars.getPlanets(planetResponse -> { requireActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.d("TEST:: ", planetResponse.toString()); } }); });
-
is Response.Success -> { _loading.postValue(false) _populationData.postValue(result.data.getFiveClimateAndPopulation()) } is Response.Error -> { _loading.postValue(false) _errorMessage.postValue(Pair(true, result.exception.toString())) } is Response.Loading -> { _loading.postValue(true) }
Library | README |
---|---|
Retrofit | [https://square.github.io/retrofit/] |
Kotlin Coroutines | [https://kotlinlang.org/docs/coroutines-overview.html] |
GSON Serializer | [https://github.com/google/gson] |
Mockk | [https://mockk.io/] |
Android STD Libs | [https://developer.android.com/kotlin/first] |