Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.77 KB | None | 0 0
  1. package com.example.core.ui.fragment
  2.  
  3. import android.databinding.ObservableBoolean
  4. import android.databinding.ObservableInt
  5. import android.databinding.ViewDataBinding
  6. import android.support.annotation.CallSuper
  7. import android.support.v7.widget.RecyclerView
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import com.orderpost.core.R
  11. import com.orderpost.core.ui.binding.ViewModelInflater
  12. import com.orderpost.core.ui.binding.setViewModel
  13.  
  14. abstract class ListFragment : BaseFragment() {
  15.  
  16.     override val layoutId get() = R.layout.fragment_recycler_view
  17.  
  18.     val refreshing = ObservableBoolean(false)
  19.     val progressVisibility = ObservableInt(View.INVISIBLE)
  20.  
  21.     open val refresherColorId get() = android.R.color.black
  22.  
  23.     protected open val itemLayoutId get() = 0
  24.  
  25.     private var adapter: RecyclerView.Adapter<*>? =
  26.             null
  27.  
  28.     protected abstract val itemCount: Int
  29.  
  30.     abstract fun refresh()
  31.  
  32.     protected abstract fun onCreateItemViewModel(index: Int): Any
  33.  
  34.     fun createRecyclerViewAdapter(): RecyclerView.Adapter<*> {
  35.         if (itemLayoutId == 0) {
  36.             throw UnsupportedOperationException()
  37.         }
  38.  
  39.         return AdapterImpl().also { adapter = it }
  40.     }
  41.  
  42.     override fun onDestroyView() {
  43.         adapter = null
  44.         super.onDestroyView()
  45.     }
  46.  
  47.     protected fun notifyPaginationStart() {
  48.         refreshing.set(true)
  49.         progressVisibility.set(View.INVISIBLE)
  50.         adapter?.notifyDataSetChanged()
  51.     }
  52.  
  53.     protected fun notifyNextPageDelivered(positionStart: Int, itemCount: Int) {
  54.         refreshing.set(false)
  55.         progressVisibility.set(View.VISIBLE)
  56.         adapter?.notifyItemRangeInserted(positionStart + 1, itemCount)
  57.     }
  58.  
  59.     protected fun notifyPaginationComplete() {
  60.         refreshing.set(false)
  61.         progressVisibility.set(View.INVISIBLE)
  62.     }
  63.  
  64.     private class BindableViewHolder(private val binding: ViewDataBinding)
  65.         : RecyclerView.ViewHolder(binding.root) {
  66.  
  67.         var viewModel: Any? = null
  68.             set(value) {
  69.                 field = value
  70.  
  71.                 if (value == null) {
  72.                     binding.unbind()
  73.                 } else {
  74.                     binding.setViewModel(value)
  75.                 }
  76.             }
  77.     }
  78.  
  79.     private inner class AdapterImpl : RecyclerView.Adapter<BindableViewHolder>() {
  80.  
  81.         override fun getItemCount() = 1 + this@ListFragment.itemCount
  82.  
  83.         override fun getItemViewType(position: Int) =
  84.                 CONTENT_ITEM.takeIf { position < this@ListFragment.itemCount }
  85.                         ?: PROGRESS_BAR
  86.  
  87.         override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
  88.                 when (viewType) {
  89.                     CONTENT_ITEM -> itemLayoutId
  90.                     PROGRESS_BAR -> R.layout.item_progress_bar
  91.                     else -> throw UnsupportedOperationException()
  92.                 }.let(::ViewModelInflater)
  93.                         .inflate(parent)
  94.                         .let(::BindableViewHolder)
  95.  
  96.         @CallSuper
  97.         override fun onBindViewHolder(holder: BindableViewHolder, position: Int) {
  98.             with(holder) {
  99.                 viewModel = createItemViewModel(itemViewType, position)
  100.             }
  101.         }
  102.  
  103.         override fun onViewRecycled(holder: BindableViewHolder) {
  104.             holder.viewModel = null
  105.         }
  106.  
  107.         private fun createItemViewModel(viewType: Int, position: Int) =
  108.                 when (viewType) {
  109.                     CONTENT_ITEM -> onCreateItemViewModel(position)
  110.                     PROGRESS_BAR -> this@ListFragment
  111.                     else -> throw UnsupportedOperationException()
  112.                 }
  113.     }
  114.  
  115.     companion object {
  116.         private val CONTENT_ITEM = 0
  117.         private val PROGRESS_BAR = 1
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement