Skip to content

Instantly share code, notes, and snippets.

@mbahgojol
Created August 11, 2020 07:09
Show Gist options
  • Save mbahgojol/c2618e0dda96795ec5bfc4d3f2620920 to your computer and use it in GitHub Desktop.
Save mbahgojol/c2618e0dda96795ec5bfc4d3f2620920 to your computer and use it in GitHub Desktop.
// PagingSource
private const val MOVIES_STARTING_PAGE_INDEX = 1
class MoviesPagingSource(
private val apiHelper: ApiHelper
) : RxPagingSource<Int, ResultsItem>() {
override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, ResultsItem>> {
val position = params.key ?: MOVIES_STARTING_PAGE_INDEX
Log.d("POSITION", position.toString())
return try {
apiHelper
.getPopularMovies(position)
?.map {
LoadResult.Page(
it,
if (position == MOVIES_STARTING_PAGE_INDEX) null else position,
if (it.isEmpty()) null else position + 1
)
}
?.compose(singleIo())
?.doOnError {
it.printStackTrace()
LoadResult.Error<Int, ResultsItem>(it)
} as Single<LoadResult<Int, ResultsItem>>
} catch (e: Exception) {
e.printStackTrace()
Single.just(LoadResult.Error(e))
}
}
}
// ViewModel
class PagingViewModel @Inject constructor(private val appDataManager: AppDataManager) :
BaseObservableViewModel() {
companion object {
private const val NETWORK_PAGE_SIZE = 20
}
private var currentMovieResult: Flowable<PagingData<ResultsItem>> = Pager(
config = PagingConfig(
pageSize = NETWORK_PAGE_SIZE,
enablePlaceholders = false
), pagingSourceFactory = { MoviesPagingSource(appDataManager) }
).flowable.cachedIn(viewModelScope)
val mutableLiveDataItems = MutableLiveData<PagingData<ResultsItem>>()
fun getMovies() {
compositeDisposable.add(currentMovieResult
.subscribe {
mutableLiveDataItems.value = it
})
}
}
// PagingAdapter
class PagingAdapter : PagingDataAdapter<ResultsItem, PagingAdapter.ViewHolder>(REPO_COMPARATOR) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
getItem(position).takeIf { it != null }?.let {
holder.bind(it)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder.create(parent)
companion object {
private val REPO_COMPARATOR = object : DiffUtil.ItemCallback<ResultsItem>() {
override fun areItemsTheSame(oldItem: ResultsItem, newItem: ResultsItem): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: ResultsItem, newItem: ResultsItem): Boolean =
oldItem == newItem
}
}
class ViewHolder(private val binding: ItemMovieBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(movie: ResultsItem) {
binding.tvMoviesName.text = movie.title
}
companion object {
fun create(parent: ViewGroup) = ViewHolder(
ItemMovieBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}
}
}
// Observer Data di Fragmentnya
viewModel.getMovies()
observe(viewModel.mutableLiveDataItems) {
adapter.submitData(lifecycle, it)
}
adapter.addLoadStateListener { loadState ->
/*binding.progressBar.isVisible =
(loadState.source.refresh is LoadState.Loading *//*|| loadState.source.append is LoadState.Loading*//*)
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.append as? LoadState.Error
?: loadState.prepend as? LoadState.Error
if (errorState != null) {
Toast.makeText(
requireContext(),
"\uD83D\uDE28 Wooops ${errorState.error}",
Toast.LENGTH_LONG
).show()
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment