Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard Cards: Activity Card Foundation #18270

Merged
merged 14 commits into from
Apr 18, 2023
Merged
Prev Previous commit
Next Next commit
Add activity card items adapter
  • Loading branch information
zwarm committed Apr 13, 2023
commit 269fa0a3712f6e937211c4224d4da5def4bbf9c7
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.wordpress.android.ui.mysite.cards.dashboard.activity

import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import org.wordpress.android.ui.utils.UiHelpers
import androidx.recyclerview.widget.RecyclerView.Adapter
import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.ActivityCard.ActivityCardWithItems.ActivityItem

class ActivityItemsAdapter(
private val uiHelpers: UiHelpers
) : Adapter<ActivityItemViewHolder>() {
private val items = mutableListOf<ActivityItem>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ActivityItemViewHolder(
parent,
uiHelpers
)

override fun getItemCount(): Int = items.size

override fun onBindViewHolder(holder: ActivityItemViewHolder, position: Int) {
holder.bind(items[position])
}

fun update(newItems: List<ActivityItem>) {
val diffResult = DiffUtil.calculateDiff(ActivityItemDiffUtil(items, newItems))
items.clear()
items.addAll(newItems)
diffResult.dispatchUpdatesTo(this)
}

class ActivityItemDiffUtil(
private val oldList: List<ActivityItem>,
private val newList: List<ActivityItem>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val newItem = newList[newItemPosition]
val oldItem = oldList[oldItemPosition]

return (oldItem == newItem)
}

override fun getOldListSize(): Int = oldList.size

override fun getNewListSize(): Int = newList.size

override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean = oldList[oldItemPosition] == newList[newItemPosition]
}
}