From ba5dbf90d82719c11dbf3e94816bcbd702373f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 2 Jun 2020 01:04:41 +0200 Subject: [PATCH] Fixes in updating adapter items (#854) --- .../grade/details/GradeDetailsAdapter.kt | 59 +++++++++++-------- .../modules/message/tab/MessageTabAdapter.kt | 5 +- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt index d5e05f3b..c129d948 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.NO_POSITION import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.databinding.HeaderGradeDetailsBinding @@ -23,7 +24,7 @@ class GradeDetailsAdapter @Inject constructor() : BaseExpandableAdapter() - private var expandedPosition = RecyclerView.NO_POSITION + private var expandedPosition = NO_POSITION private var isExpandable = false @@ -35,7 +36,7 @@ class GradeDetailsAdapter @Inject constructor() : BaseExpandableAdapter 1) { - Timber.e("Header with subject $subject found ${candidates.size} times! Items: $candidates, expanded: $expandedPosition") + Timber.e("Header with subject $subject found ${candidates.size} times! Expanded: $expandedPosition. Items: $candidates") } return candidates.first() } fun updateHeaderItem(item: GradeDetailsItem) { - headers[headers.indexOf(item)] = item - items[items.indexOf(item)] = item - notifyItemChanged(items.indexOf(item)) + val headerPosition = headers.indexOf(item) + val itemPosition = items.indexOf(item) + + if (headerPosition == NO_POSITION || itemPosition == NO_POSITION) { + Timber.e("Invalid update header positions! Header: $headerPosition, item: $itemPosition") + } + + headers[headerPosition] = item + items[itemPosition] = item + notifyItemChanged(itemPosition) } fun collapseAll() { if (expandedPosition != -1) { refreshList(headers) - expandedPosition = RecyclerView.NO_POSITION + expandedPosition = NO_POSITION } } @Synchronized - private fun refreshList(newItems: List) { + private fun refreshList(newItems: MutableList) { val diffCallback = GradeDetailsDiffUtil(items, newItems) val diffResult = DiffUtil.calculateDiff(diffCallback) - items = newItems.toMutableList() + items = newItems diffResult.dispatchUpdatesTo(this) } @@ -99,23 +103,24 @@ class GradeDetailsAdapter @Inject constructor() : BaseExpandableAdapter bindHeaderViewHolder( - binding = holder.binding, + holder = holder, header = items[position].value as GradeDetailsHeader, - headerPosition = headers.indexOf(items[position]), - adapterPosition = position + position = position ) is ItemViewHolder -> bindItemViewHolder( - binding = holder.binding, - grade = items[position].value as Grade, - position = holder.adapterPosition + holder = holder, + grade = items[position].value as Grade ) } } - private fun bindHeaderViewHolder(binding: HeaderGradeDetailsBinding, header: GradeDetailsHeader, headerPosition: Int, adapterPosition: Int) { - with(binding) { + private fun bindHeaderViewHolder(holder: HeaderViewHolder, header: GradeDetailsHeader, position: Int) { + val headerPosition = headers.indexOf(items[position]) + val adapterPosition = holder.adapterPosition + + with(holder.binding) { gradeHeaderDivider.visibility = if (adapterPosition == 0) View.GONE else View.VISIBLE - gradeHeaderSubject.apply { + with(gradeHeaderSubject) { text = header.subject maxLines = if (headerPosition == expandedPosition) 2 else 1 } @@ -130,7 +135,7 @@ class GradeDetailsAdapter @Inject constructor() : BaseExpandableAdapter