Skip to content

Commit a560431

Browse files
author
Prem Suman
committed
Added dedicated callbacks for different Swapi list network calls
1 parent 95ed263 commit a560431

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

swapi-sample-components/src/main/java/com/prembros/swapi/sample_components/ui/SwapiListActivity.kt

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.prembros.swapi.sample_components.*
1414
import com.prembros.swapi.sample_components.adapter.*
1515
import com.prembros.swapi.sample_components.data.EndlessRecyclerViewScrollListener
1616
import com.swapi.StarWarsSdk
17-
import com.swapi.models.SWList
17+
import com.swapi.models.*
1818
import com.swapi.network.RetrofitCallBack
1919
import retrofit2.Response
2020
import timber.log.Timber
@@ -48,7 +48,7 @@ abstract class SwapiListActivity(@LayoutRes contentLayoutId: Int = R.layout.acti
4848
const val LIST_TYPE = "list_type"
4949
}
5050

51-
@Suppress("DEPRECATION") override fun onCreate(savedInstanceState: Bundle?) {
51+
override fun onCreate(savedInstanceState: Bundle?) {
5252
super.onCreate(savedInstanceState)
5353
WindowCompat.setDecorFitsSystemWindows(window, false)
5454
page = 1
@@ -96,10 +96,19 @@ abstract class SwapiListActivity(@LayoutRes contentLayoutId: Int = R.layout.acti
9696
}
9797
}
9898

99+
@Suppress("UNCHECKED_CAST")
99100
private fun <DATA> retrofitCallBack(): RetrofitCallBack<SWList<DATA>>.() -> Unit = {
100101
onResponse {
101102
loaderProgressBar?.visibility = View.GONE
102103
onDataResponse(it)
104+
when (listType) {
105+
LIST_FILMS -> onAllFilmsResponse(it as Response<SWList<Film>>)
106+
LIST_PEOPLE -> onAllPeopleResponse(it as Response<SWList<People>>)
107+
LIST_PLANETS -> onAllPlanetsResponse(it as Response<SWList<Planet>>)
108+
LIST_SPECIES -> onAllSpeciesResponse(it as Response<SWList<Species>>)
109+
LIST_STARSHIPS -> onAllStarshipsResponse(it as Response<SWList<Starship>>)
110+
LIST_VEHICLES -> onAllVehiclesResponse(it as Response<SWList<Vehicle>>)
111+
}
103112
}
104113
// Called on failed response from server
105114
onFailure {
@@ -115,8 +124,41 @@ abstract class SwapiListActivity(@LayoutRes contentLayoutId: Int = R.layout.acti
115124

116125
abstract fun initRecyclerView()
117126

127+
/**
128+
* Common callback for all Swapi responses
129+
*/
118130
abstract fun <DATA> onDataResponse(it: Response<SWList<DATA>>)
119131

132+
/**
133+
* Dedicated callback for all [Film] response
134+
*/
135+
open fun onAllFilmsResponse(response: Response<SWList<Film>>) {}
136+
137+
/**
138+
* Dedicated callback for all [People] response
139+
*/
140+
open fun onAllPeopleResponse(response: Response<SWList<People>>) {}
141+
142+
/**
143+
* Dedicated callback for all [Planet] response
144+
*/
145+
open fun onAllPlanetsResponse(response: Response<SWList<Planet>>) {}
146+
147+
/**
148+
* Dedicated callback for all [Species] response
149+
*/
150+
open fun onAllSpeciesResponse(response: Response<SWList<Species>>) {}
151+
152+
/**
153+
* Dedicated callback for all [Starship] response
154+
*/
155+
open fun onAllStarshipsResponse(response: Response<SWList<Starship>>) {}
156+
157+
/**
158+
* Dedicated callback for all [Vehicle] response
159+
*/
160+
open fun onAllVehiclesResponse(response: Response<SWList<Vehicle>>) {}
161+
120162
open fun onDataFailure(throwable: Throwable) {
121163
Timber.e(throwable)
122164
}

0 commit comments

Comments
 (0)