This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwapiAdapter(diffCallback: DiffUtil.ItemCallback<Person?>) | |
: PagedListAdapter<Person, SwapiPersonViewHolder>(diffCallback) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SwapiPersonViewHolder { | |
val inflater = LayoutInflater.from(parent.context) | |
val binding = DataBindingUtil.inflate<ItemSwapiPersonBinding>( | |
inflater, R.layout.item_swapi_person, parent, false) | |
return SwapiPersonViewHolder(binding) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaginationViewModel(pagedListProvider: PagedListProvider<Person?>) : ViewModel() { | |
val pagedListData = pagedListProvider.provide() | |
val adapter = SwapiAdapter() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindingAdapter("android:adapterSetup") | |
fun setupRecyclerViewAdapter(view: RecyclerView, viewModel: PaginationViewModel) { | |
view.layoutManager = LinearLayoutManager(view.context) | |
view.adapter = viewModel.adapter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable | |
name="viewModel" | |
type="pl.marchuck.pagingexample.pagination.PaginationViewModel" /> | |
</data> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PaginationItemCallback : DiffUtil.ItemCallback<Person?>() { | |
override fun areItemsTheSame(oldItem: Person?, newItem: Person?): Boolean { | |
if (oldItem == null || newItem == null) return false | |
return oldItem == newItem | |
} | |
override fun areContentsTheSame(oldItem: Person?, newItem: Person?): Boolean { | |
if (oldItem == null || newItem == null) return false | |
return oldItem.name == newItem.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PagedListProvider<Value> { | |
fun provide() : LiveData<PagedList<Value>> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwapiPeopleDataSource(val useCase: GetPeopleUseCase) : PageKeyedDataSource<Int, Person?>() { | |
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Person?>) { | |
async { | |
val items = useCase.execute(page = 1).await() | |
callback.onResult(items.orEmpty(), null, 2) | |
} | |
} | |
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Person?>) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetPeopleUseCase(private val api: StarWarsApi) { | |
fun execute(page: Int): Deferred<List<Person?>?> { | |
return async { transform(page) } | |
} | |
private suspend fun transform(page: Int): List<Person?> { | |
val response = api.getPeople(page).await() | |
return response.results ?: emptyList() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface StarWarsApi { | |
@GET("people/") | |
fun getPeople(@Query("page") page: Int): Deferred<PeopleResponse> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<TextView | |
style="@style/textview_bold" | |
android:layout_marginBottom="@dimen/activity_small_padding" | |
android:layout_marginLeft="@dimen/activity_default_padding" | |
android:layout_marginRight="@dimen/activity_default_padding" |