Commit 685a0a64 authored by Artem Zanin's avatar Artem Zanin

ProjectsAdapter separated from ProjectsListFragment (#129)

parent 8abf7c68
package ru.terrakok.gitlabclient.ui.projects
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import com.hannesdorfmann.adapterdelegates3.ListDelegationAdapter
import ru.terrakok.gitlabclient.entity.Project
import ru.terrakok.gitlabclient.ui.global.list.ProgressAdapterDelegate
import ru.terrakok.gitlabclient.ui.global.list.ProgressItem
import ru.terrakok.gitlabclient.ui.global.list.ProjectAdapterDelegate
class ProjectsAdapter(
private val nextPageListener: () -> Unit,
private val onClickListener: (Long) -> Unit
) : ListDelegationAdapter<MutableList<Any>>() {
init {
items = mutableListOf()
delegatesManager.addDelegate(ProjectAdapterDelegate { onClickListener(it.id) })
delegatesManager.addDelegate(ProgressAdapterDelegate())
}
fun setData(projects: List<Project>) {
val progress = isProgress()
val oldItems = items.toList()
items.clear()
items.addAll(projects)
if (progress) items.add(ProgressItem())
DiffUtil
.calculateDiff(DiffCallback(items, oldItems), false)
.dispatchUpdatesTo(this)
}
fun showProgress(isVisible: Boolean) {
val progress = isProgress()
if (isVisible && !progress) {
items.add(ProgressItem())
notifyItemInserted(items.lastIndex)
} else if (!isVisible && progress) {
items.remove(items.last())
notifyItemRemoved(items.size)
}
}
private fun isProgress() = items.isNotEmpty() && items.last() is ProgressItem
override fun onBindViewHolder(
holder: RecyclerView.ViewHolder,
position: Int,
payloads: MutableList<Any?>
) {
super.onBindViewHolder(holder, position, payloads)
if (position == items.size - 10) nextPageListener()
}
private class DiffCallback(
private val newItems: List<Any>,
private val oldItems: List<Any>
) : DiffUtil.Callback() {
override fun getOldListSize() = oldItems.size
override fun getNewListSize() = newItems.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return if (newItem is Project && oldItem is Project) {
newItem.id == oldItem.id
} else {
newItem is ProgressItem && oldItem is ProgressItem
}
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return if (newItem is Project && oldItem is Project) {
newItem == oldItem
} else {
true
}
}
}
}
\ No newline at end of file
...@@ -2,10 +2,8 @@ package ru.terrakok.gitlabclient.ui.projects ...@@ -2,10 +2,8 @@ package ru.terrakok.gitlabclient.ui.projects
import android.os.Bundle import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.arellomobile.mvp.presenter.InjectPresenter import com.arellomobile.mvp.presenter.InjectPresenter
import com.arellomobile.mvp.presenter.ProvidePresenter import com.arellomobile.mvp.presenter.ProvidePresenter
import com.hannesdorfmann.adapterdelegates3.ListDelegationAdapter
import kotlinx.android.synthetic.main.layout_base_list.* import kotlinx.android.synthetic.main.layout_base_list.*
import kotlinx.android.synthetic.main.layout_zero.* import kotlinx.android.synthetic.main.layout_zero.*
import ru.terrakok.gitlabclient.R import ru.terrakok.gitlabclient.R
...@@ -18,9 +16,6 @@ import ru.terrakok.gitlabclient.toothpick.PrimitiveWrapper ...@@ -18,9 +16,6 @@ import ru.terrakok.gitlabclient.toothpick.PrimitiveWrapper
import ru.terrakok.gitlabclient.toothpick.qualifier.ProjectListMode import ru.terrakok.gitlabclient.toothpick.qualifier.ProjectListMode
import ru.terrakok.gitlabclient.ui.global.BaseFragment import ru.terrakok.gitlabclient.ui.global.BaseFragment
import ru.terrakok.gitlabclient.ui.global.ZeroViewHolder import ru.terrakok.gitlabclient.ui.global.ZeroViewHolder
import ru.terrakok.gitlabclient.ui.global.list.ProgressAdapterDelegate
import ru.terrakok.gitlabclient.ui.global.list.ProgressItem
import ru.terrakok.gitlabclient.ui.global.list.ProjectAdapterDelegate
import toothpick.Scope import toothpick.Scope
import toothpick.config.Module import toothpick.config.Module
...@@ -29,7 +24,7 @@ import toothpick.config.Module ...@@ -29,7 +24,7 @@ import toothpick.config.Module
*/ */
class ProjectsListFragment : BaseFragment(), ProjectsListView { class ProjectsListFragment : BaseFragment(), ProjectsListView {
private val adapter = ProjectsAdapter() private val adapter = ProjectsAdapter({ presenter.loadNextProjectsPage() }, { presenter.onProjectClicked(it) })
private var zeroViewHolder: ZeroViewHolder? = null private var zeroViewHolder: ZeroViewHolder? = null
override val layoutRes = R.layout.fragment_projects override val layoutRes = R.layout.fragment_projects
...@@ -99,41 +94,6 @@ class ProjectsListFragment : BaseFragment(), ProjectsListView { ...@@ -99,41 +94,6 @@ class ProjectsListFragment : BaseFragment(), ProjectsListView {
override fun onBackPressed() = presenter.onBackPressed() override fun onBackPressed() = presenter.onBackPressed()
inner class ProjectsAdapter : ListDelegationAdapter<MutableList<Any>>() {
init {
items = mutableListOf()
delegatesManager.addDelegate(ProjectAdapterDelegate({ presenter.onProjectClicked(it.id) }))
delegatesManager.addDelegate(ProgressAdapterDelegate())
}
fun setData(projects: List<Project>) {
val progress = isProgress()
items.clear()
items.addAll(projects)
if (progress) items.add(ProgressItem())
notifyDataSetChanged()
}
fun showProgress(isVisible: Boolean) {
val currentProgress = isProgress()
if (isVisible && !currentProgress) items.add(ProgressItem())
else if (!isVisible && currentProgress) items.remove(items.last())
notifyDataSetChanged()
}
private fun isProgress() = items.isNotEmpty() && items.last() is ProgressItem
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any?>) {
super.onBindViewHolder(holder, position, payloads)
if (position == items.size - 10) presenter.loadNextProjectsPage()
}
}
companion object { companion object {
private const val ARG_MODE = "plf_mode" private const val ARG_MODE = "plf_mode"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment