Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.core.ui.fragment
- import android.databinding.ObservableBoolean
- import android.databinding.ObservableInt
- import android.databinding.ViewDataBinding
- import android.support.annotation.CallSuper
- import android.support.v7.widget.RecyclerView
- import android.view.View
- import android.view.ViewGroup
- import com.orderpost.core.R
- import com.orderpost.core.ui.binding.ViewModelInflater
- import com.orderpost.core.ui.binding.setViewModel
- abstract class ListFragment : BaseFragment() {
- override val layoutId get() = R.layout.fragment_recycler_view
- val refreshing = ObservableBoolean(false)
- val progressVisibility = ObservableInt(View.INVISIBLE)
- open val refresherColorId get() = android.R.color.black
- protected open val itemLayoutId get() = 0
- private var adapter: RecyclerView.Adapter<*>? =
- null
- protected abstract val itemCount: Int
- abstract fun refresh()
- protected abstract fun onCreateItemViewModel(index: Int): Any
- fun createRecyclerViewAdapter(): RecyclerView.Adapter<*> {
- if (itemLayoutId == 0) {
- throw UnsupportedOperationException()
- }
- return AdapterImpl().also { adapter = it }
- }
- override fun onDestroyView() {
- adapter = null
- super.onDestroyView()
- }
- protected fun notifyPaginationStart() {
- refreshing.set(true)
- progressVisibility.set(View.INVISIBLE)
- adapter?.notifyDataSetChanged()
- }
- protected fun notifyNextPageDelivered(positionStart: Int, itemCount: Int) {
- refreshing.set(false)
- progressVisibility.set(View.VISIBLE)
- adapter?.notifyItemRangeInserted(positionStart + 1, itemCount)
- }
- protected fun notifyPaginationComplete() {
- refreshing.set(false)
- progressVisibility.set(View.INVISIBLE)
- }
- private class BindableViewHolder(private val binding: ViewDataBinding)
- : RecyclerView.ViewHolder(binding.root) {
- var viewModel: Any? = null
- set(value) {
- field = value
- if (value == null) {
- binding.unbind()
- } else {
- binding.setViewModel(value)
- }
- }
- }
- private inner class AdapterImpl : RecyclerView.Adapter<BindableViewHolder>() {
- override fun getItemCount() = 1 + this@ListFragment.itemCount
- override fun getItemViewType(position: Int) =
- CONTENT_ITEM.takeIf { position < this@ListFragment.itemCount }
- ?: PROGRESS_BAR
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
- when (viewType) {
- CONTENT_ITEM -> itemLayoutId
- PROGRESS_BAR -> R.layout.item_progress_bar
- else -> throw UnsupportedOperationException()
- }.let(::ViewModelInflater)
- .inflate(parent)
- .let(::BindableViewHolder)
- @CallSuper
- override fun onBindViewHolder(holder: BindableViewHolder, position: Int) {
- with(holder) {
- viewModel = createItemViewModel(itemViewType, position)
- }
- }
- override fun onViewRecycled(holder: BindableViewHolder) {
- holder.viewModel = null
- }
- private fun createItemViewModel(viewType: Int, position: Int) =
- when (viewType) {
- CONTENT_ITEM -> onCreateItemViewModel(position)
- PROGRESS_BAR -> this@ListFragment
- else -> throw UnsupportedOperationException()
- }
- }
- companion object {
- private val CONTENT_ITEM = 0
- private val PROGRESS_BAR = 1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement