[fenix] For https://github.com/mozilla-mobile/fenix/issues/12856: Add save to collections button to Tabs Tray
Using the ConcatAdapter, we're now able to insert multiple data sources of information into one RecyclerView and preserve layout/scrolling in addition to adding the 'Save to Collection' button.pull/600/head
parent
e15b895fd9
commit
a48f4282b3
@ -0,0 +1,67 @@
|
||||
/* 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/. */
|
||||
|
||||
package org.mozilla.fenix.tabtray
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.tabtray.SaveToCollectionsButtonAdapter.Item
|
||||
import org.mozilla.fenix.tabtray.SaveToCollectionsButtonAdapter.ViewHolder
|
||||
|
||||
/**
|
||||
* An adapter to display a single 'Save to Collections' button that can be used to display between
|
||||
* multiple [RecyclerView.Adapter] in one [RecyclerView].
|
||||
*/
|
||||
class SaveToCollectionsButtonAdapter(
|
||||
private val interactor: TabTrayInteractor
|
||||
) : ListAdapter<Item, ViewHolder>(DiffCallback) {
|
||||
|
||||
init {
|
||||
submitList(listOf(Item))
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val itemView = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
|
||||
return ViewHolder(itemView, interactor)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) = Unit
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return ViewHolder.LAYOUT_ID
|
||||
}
|
||||
|
||||
private object DiffCallback : DiffUtil.ItemCallback<Item>() {
|
||||
override fun areItemsTheSame(oldItem: Item, newItem: Item) = true
|
||||
|
||||
override fun areContentsTheSame(oldItem: Item, newItem: Item) = true
|
||||
}
|
||||
|
||||
/**
|
||||
* An object to identify the data type.
|
||||
*/
|
||||
object Item
|
||||
|
||||
class ViewHolder(
|
||||
itemView: View,
|
||||
private val interactor: TabTrayInteractor
|
||||
) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
|
||||
init {
|
||||
itemView.setOnClickListener(this)
|
||||
}
|
||||
|
||||
override fun onClick(v: View?) {
|
||||
interactor.onEnterMultiselect()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LAYOUT_ID = R.layout.tabs_tray_save_to_collections_item
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
style="@style/NeutralButton"
|
||||
android:layout_margin="8dp"
|
||||
android:text="@string/save_to_collection"
|
||||
app:icon="@drawable/ic_tab_collection" />
|
@ -0,0 +1,53 @@
|
||||
/* 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/. */
|
||||
|
||||
package org.mozilla.fenix.tabtray
|
||||
|
||||
import android.widget.FrameLayout
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.mozilla.fenix.tabtray.SaveToCollectionsButtonAdapter.Item
|
||||
import org.mozilla.fenix.tabtray.SaveToCollectionsButtonAdapter.ViewHolder
|
||||
import kotlin.random.Random
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class SaveToCollectionsButtonAdapterTest {
|
||||
|
||||
private lateinit var adapter: SaveToCollectionsButtonAdapter
|
||||
private lateinit var interactor: TabTrayInteractor
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
interactor = mockk(relaxed = true)
|
||||
adapter = SaveToCollectionsButtonAdapter(interactor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create adapter only has one item in it`() {
|
||||
assertEquals(1, adapter.itemCount)
|
||||
assertTrue(adapter.currentList.first() is Item)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `viewholder click invokes interactor`() {
|
||||
val itemView = FrameLayout(testContext)
|
||||
val viewHolder = ViewHolder(itemView, interactor)
|
||||
|
||||
viewHolder.onClick(itemView)
|
||||
|
||||
verify { interactor.onEnterMultiselect() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `always use the same layout`() {
|
||||
assertEquals(ViewHolder.LAYOUT_ID, adapter.getItemViewType(Random.nextInt()))
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/* 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/. */
|
||||
|
||||
package org.mozilla.fenix.tabtray
|
||||
|
||||
import android.widget.FrameLayout
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.ItemTouchHelper.ACTION_STATE_IDLE
|
||||
import androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import io.mockk.mockk
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class TabsTouchHelperTest {
|
||||
|
||||
@Test
|
||||
fun `movement flags remain unchanged if onSwipeToDelete is true`() {
|
||||
val recyclerView = RecyclerView(testContext)
|
||||
val layout = FrameLayout(testContext)
|
||||
val interactor: TabTrayInteractor = mockk(relaxed = true)
|
||||
val viewHolder = SaveToCollectionsButtonAdapter.ViewHolder(layout, interactor)
|
||||
val callback = TouchCallback(mockk()) { true }
|
||||
|
||||
assertEquals(0, callback.getDragDirs(recyclerView, viewHolder))
|
||||
assertEquals(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT, callback.getSwipeDirs(recyclerView, viewHolder))
|
||||
|
||||
val actual = callback.getMovementFlags(recyclerView, viewHolder)
|
||||
val expected = makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `movement flags remain unchanged if onSwipeToDelete is false`() {
|
||||
val recyclerView = RecyclerView(testContext)
|
||||
val layout = FrameLayout(testContext)
|
||||
val interactor: TabTrayInteractor = mockk(relaxed = true)
|
||||
val viewHolder = SaveToCollectionsButtonAdapter.ViewHolder(layout, interactor)
|
||||
val callback = TouchCallback(mockk()) { false }
|
||||
|
||||
assertEquals(0, callback.getDragDirs(recyclerView, viewHolder))
|
||||
assertEquals(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT, callback.getSwipeDirs(recyclerView, viewHolder))
|
||||
|
||||
val actual = callback.getMovementFlags(recyclerView, viewHolder)
|
||||
val expected = ItemTouchHelper.Callback.makeFlag(ACTION_STATE_IDLE, 0)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue