This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 SwapiPeoplePagedListProvider(private val factory: DataSource.Factory<Int, Person?>) : PagedListProvider<Person?> { | |
| override fun provide(): LiveData<PagedList<Person?>> { | |
| return LivePagedListBuilder(factory, PagedList.Config.Builder() | |
| .setEnablePlaceholders(false) | |
| .setPageSize(20) | |
| .setInitialLoadSizeHint(20) | |
| .build() | |
| ).setFetchExecutor(createFetchExecutor()) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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" |