Commit a4439ff3 authored by Denys M's avatar Denys M Committed by Emily Kager
Browse files

For #3563. Use `ListAdapter` for App Share list.

parent e55eda30
......@@ -157,7 +157,7 @@ class ShareFragment : AppCompatDialogFragment() {
val devicesShareOptions = devicesListDeferred.await()
shareToAccountDevicesView.setSharetargets(devicesShareOptions)
val appsToShareTo = appsListDeferred.await()
shareToAppsView.setSharetargets(appsToShareTo)
shareToAppsView.setShareTargets(appsToShareTo)
}
}
......
......@@ -8,10 +8,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.extensions.LayoutContainer
import org.mozilla.fenix.R
import org.mozilla.fenix.share.listadapters.AppShareOption
import kotlinx.android.synthetic.main.share_to_apps.*
import org.mozilla.fenix.R
import org.mozilla.fenix.share.listadapters.AppShareAdapter
import org.mozilla.fenix.share.listadapters.AppShareOption
/**
* Callbacks for possible user interactions on the [ShareCloseView]
......@@ -24,19 +24,20 @@ class ShareToAppsView(
override val containerView: ViewGroup,
interactor: ShareToAppsInteractor
) : LayoutContainer {
private val adapter = AppShareAdapter(interactor)
init {
LayoutInflater.from(containerView.context)
.inflate(R.layout.share_to_apps, containerView, true)
appsList.adapter = AppShareAdapter(interactor)
appsList.adapter = adapter
}
fun setSharetargets(targets: List<AppShareOption>) {
fun setShareTargets(targets: List<AppShareOption>) {
progressBar.visibility = View.GONE
appsList.visibility = View.VISIBLE
with(appsList.adapter as AppShareAdapter) {
updateData(targets)
}
adapter.submitList(targets)
}
}
......@@ -7,14 +7,14 @@ package org.mozilla.fenix.share.listadapters
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import org.mozilla.fenix.share.ShareToAppsInteractor
import org.mozilla.fenix.share.viewholders.AppViewHolder
class AppShareAdapter(
private val interactor: ShareToAppsInteractor,
private val applications: MutableList<AppShareOption> = mutableListOf()
) : RecyclerView.Adapter<AppViewHolder>() {
private val interactor: ShareToAppsInteractor
) : ListAdapter<AppShareOption, AppViewHolder>(DiffCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppViewHolder {
val view = LayoutInflater.from(parent.context)
......@@ -23,17 +23,18 @@ class AppShareAdapter(
return AppViewHolder(view, interactor)
}
override fun getItemCount(): Int = applications.size
override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
holder.bind(applications[position])
holder.bind(getItem(position))
}
}
fun updateData(applications: List<AppShareOption>) {
this.applications.clear()
this.applications.addAll(applications)
notifyDataSetChanged()
}
private object DiffCallback : DiffUtil.ItemCallback<AppShareOption>() {
override fun areItemsTheSame(oldItem: AppShareOption, newItem: AppShareOption) =
oldItem.packageName == newItem.packageName
override fun areContentsTheSame(oldItem: AppShareOption, newItem: AppShareOption) =
oldItem == newItem
}
data class AppShareOption(
......
......@@ -28,26 +28,25 @@ import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(application = TestApplication::class)
class AppShareAdapterTest {
private val appOptions = mutableListOf(
AppShareOption("App 0", mockk(), "package 0", "activity 0"),
AppShareOption("App 1", mockk(), "package 1", "activity 1")
)
private val appOptionsEmpty = mutableListOf<AppShareOption>()
private val appOptionsEmpty = emptyList<AppShareOption>()
private val interactor: ShareInteractor = mockk(relaxed = true)
@Test
fun `updateData should replace all previous data with argument and call notifyDataSetChanged()`() {
// Used AppShareAdapter as a spy to ease testing of notifyDataSetChanged()
fun `updateData should call submitList()`() {
// Used AppShareAdapter as a spy to ease testing of submitList()
// and appOptionsEmpty to be able to record them being called
val adapter = spyk(AppShareAdapter(mockk(), appOptionsEmpty))
every { adapter.notifyDataSetChanged() } just Runs
val adapter = spyk(AppShareAdapter(mockk()).apply { submitList(appOptionsEmpty) })
every { adapter.submitList(any()) } just Runs
adapter.updateData(appOptions)
adapter.submitList(appOptions)
verifyOrder {
appOptionsEmpty.clear()
appOptionsEmpty.addAll(appOptions)
adapter.notifyDataSetChanged()
adapter.submitList(appOptions)
}
}
......@@ -60,7 +59,7 @@ class AppShareAdapterTest {
@Test
fun `getItemCount after updateData() call should return the the passed in list's size`() {
val adapter = AppShareAdapter(mockk(), appOptions)
val adapter = AppShareAdapter(mockk()).apply { submitList(appOptions) }
assertThat(adapter.itemCount).isEqualTo(2)
}
......@@ -89,7 +88,7 @@ class AppShareAdapterTest {
@Test
fun `the adapter binds the right item to a ViewHolder`() {
val adapter = AppShareAdapter(interactor, appOptions)
val adapter = AppShareAdapter(interactor).apply { submitList(appOptions) }
val parentView: ViewGroup = mockk(relaxed = true)
val itemView: ViewGroup = mockk(relaxed = true)
every { parentView.context } returns testContext
......
Supports Markdown
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