1
0
Fork 0

For #3554 - Add DiffUtil to HistoryAdapter

master
ekager 2019-07-11 20:47:39 -07:00 committed by Emily Kager
parent cf9ae87d50
commit 706f7d5961
4 changed files with 51 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import android.content.Context
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Observer
import org.mozilla.fenix.R
@ -101,11 +102,48 @@ class HistoryAdapter(
var selected = listOf<HistoryItem>()
fun updateData(items: List<HistoryItem>, mode: HistoryState.Mode) {
val diffUtil = DiffUtil.calculateDiff(
HistoryDiffUtil(
this.historyList,
HistoryList(items),
HistoryList(selected),
HistoryList((mode as? HistoryState.Mode.Editing)?.selectedItems ?: listOf()),
this.mode,
mode
)
)
this.historyList = HistoryList(items)
this.mode = mode
this.selected = if (mode is HistoryState.Mode.Editing) mode.selectedItems else listOf()
notifyDataSetChanged()
diffUtil.dispatchUpdatesTo(this)
}
private class HistoryDiffUtil(
val old: HistoryList,
val new: HistoryList,
val oldSelected: HistoryList,
val newSelected: HistoryList,
val oldMode: HistoryState.Mode,
val newMode: HistoryState.Mode
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
old.items[oldItemPosition] == new.items[newItemPosition]
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val modesEqual = oldMode::class == newMode::class
val isStillSelected =
oldSelected.items.contains(old.items[oldItemPosition]) &&
newSelected.items.contains(new.items[newItemPosition])
val isStillNotSelected =
!oldSelected.items.contains(old.items[oldItemPosition]) &&
!newSelected.items.contains(new.items[newItemPosition])
return modesEqual && (isStillSelected || isStillNotSelected)
}
override fun getOldListSize(): Int = old.items.size
override fun getNewListSize(): Int = new.items.size
}
override fun getItemCount(): Int = historyList.items.size

View File

@ -32,6 +32,7 @@ class HistoryDeleteButtonViewHolder(
}
}
}
fun bind(mode: HistoryState.Mode) {
this.mode = mode

View File

@ -103,7 +103,7 @@ class HistoryListItemViewHolder(
if (selected) {
favicon.setImageResource(R.drawable.mozac_ic_check)
} else {
favicon.setImageResource(0)
updateFavIcon(item.url)
}
} else {
val backgroundTint = ThemeManager.resolveAttribute(R.attr.neutral, itemView.context)

View File

@ -2,12 +2,16 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<com.google.android.material.button.MaterialButton xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/delete_history_button"
style="@style/ThemeIndependentMaterialGreyButtonDestructive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/history_delete_all"
app:rippleColor="?secondaryText" />
android:layout_margin="16dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/delete_history_button"
style="@style/ThemeIndependentMaterialGreyButtonDestructive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/history_delete_all"
app:rippleColor="?secondaryText" />
</FrameLayout>