Skip to content

Commit

Permalink
For mozilla-mobile#2251 - Add bookmark adapter diffutil (mozilla-mobi…
Browse files Browse the repository at this point in the history
…le#3936)

* For mozilla-mobile#2251 - Add bookmark adapter diffutil

* For mozilla-mobile#2251 - Fix unit tests for bookmark adapter diffutil
  • Loading branch information
colintheshots authored Jul 8, 2019
1 parent c92eabd commit 15d887e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Observer
import kotlinx.android.extensions.LayoutContainer
Expand Down Expand Up @@ -39,13 +40,46 @@ class BookmarkAdapter(val emptyView: View, val actionEmitter: Observer<BookmarkA
private var isFirstRun = true

fun updateData(tree: BookmarkNode?, mode: BookmarkState.Mode) {
val diffUtil = DiffUtil.calculateDiff(
BookmarkDiffUtil(
this.tree,
tree?.children ?: listOf(),
this.mode,
mode
)
)

this.tree = tree?.children ?: listOf()
isFirstRun = if (isFirstRun) false else {
emptyView.visibility = if (this.tree.isEmpty()) View.VISIBLE else View.GONE
false
}
this.mode = mode
notifyDataSetChanged()

diffUtil.dispatchUpdatesTo(this)
}

private class BookmarkDiffUtil(
val old: List<BookmarkNode>,
val new: List<BookmarkNode>,
val oldMode: BookmarkState.Mode,
val newMode: BookmarkState.Mode
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
old[oldItemPosition].guid == new[newItemPosition].guid

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldSelected = (oldMode as? BookmarkState.Mode.Selecting)?.selectedItems ?: setOf()
val newSelected = (newMode as? BookmarkState.Mode.Selecting)?.selectedItems ?: setOf()
val modesEqual = oldMode::class == newMode::class
val selectedEqual =
((oldSelected.contains(old[oldItemPosition]) && newSelected.contains(new[newItemPosition])) ||
(!oldSelected.contains(old[oldItemPosition]) && !newSelected.contains(new[newItemPosition])))
return modesEqual && selectedEqual
}

override fun getOldListSize(): Int = old.size
override fun getNewListSize(): Int = new.size
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookmarkNodeViewHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verifyOrder
import io.mockk.verifySequence
import io.reactivex.Observer
import io.reactivex.observers.TestObserver
import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.concept.storage.BookmarkNodeType
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.TestApplication
import org.mozilla.fenix.TestUtils.setRxSchedulers
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(application = TestApplication::class)
internal class BookmarkAdapterTest {

private lateinit var bookmarkAdapter: BookmarkAdapter
Expand All @@ -26,7 +33,7 @@ internal class BookmarkAdapterTest {
@Before
fun setup() {
setRxSchedulers()
emitter = TestObserver<BookmarkAction>()
emitter = TestObserver()
bookmarkAdapter = spyk(
BookmarkAdapter(mockk(), emitter), recordPrivateCalls = true
)
Expand All @@ -51,11 +58,11 @@ internal class BookmarkAdapterTest {
)
)
bookmarkAdapter.updateData(tree, BookmarkState.Mode.Normal)
verifySequence {
verifyOrder {
bookmarkAdapter.updateData(tree, BookmarkState.Mode.Normal)
bookmarkAdapter setProperty "tree" value tree.children
bookmarkAdapter setProperty "mode" value BookmarkState.Mode.Normal
bookmarkAdapter.notifyDataSetChanged()
bookmarkAdapter.notifyItemRangeInserted(0, 3)
}
}

Expand All @@ -66,7 +73,6 @@ internal class BookmarkAdapterTest {
bookmarkAdapter.updateData(null, BookmarkState.Mode.Normal)
bookmarkAdapter setProperty "tree" value listOf<BookmarkNode?>()
bookmarkAdapter setProperty "mode" value BookmarkState.Mode.Normal
bookmarkAdapter.notifyDataSetChanged()
}
}
}

0 comments on commit 15d887e

Please sign in to comment.