You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.1 KiB
Kotlin
125 lines
4.1 KiB
Kotlin
/* 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.tabstray.browser
|
|
|
|
import android.content.Context
|
|
import android.view.ViewGroup
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import kotlinx.android.synthetic.main.tab_tray_item.view.*
|
|
import mozilla.components.browser.tabstray.TabsAdapter.Companion.PAYLOAD_DONT_HIGHLIGHT_SELECTED_ITEM
|
|
import mozilla.components.browser.tabstray.TabsAdapter.Companion.PAYLOAD_HIGHLIGHT_SELECTED_ITEM
|
|
import mozilla.components.browser.thumbnails.loader.ThumbnailLoader
|
|
import mozilla.components.concept.tabstray.Tab
|
|
import mozilla.components.concept.tabstray.TabsTray
|
|
import mozilla.components.support.base.observer.Observable
|
|
import mozilla.components.support.base.observer.ObserverRegistry
|
|
import org.mozilla.fenix.ext.components
|
|
import org.mozilla.fenix.selection.SelectionHolder
|
|
import org.mozilla.fenix.tabstray.TabsTrayStore
|
|
import org.mozilla.fenix.tabstray.TabsTrayViewHolder
|
|
|
|
/**
|
|
* A [RecyclerView.Adapter] for browser tabs.
|
|
*/
|
|
class BrowserTabsAdapter(
|
|
private val context: Context,
|
|
private val interactor: BrowserTrayInteractor,
|
|
private val store: TabsTrayStore,
|
|
delegate: Observable<TabsTray.Observer> = ObserverRegistry()
|
|
) : TabsAdapter<TabsTrayViewHolder>(delegate) {
|
|
|
|
/**
|
|
* The layout types for the tabs.
|
|
*/
|
|
enum class ViewType {
|
|
LIST,
|
|
GRID
|
|
}
|
|
|
|
/**
|
|
* Tracks the selected tabs in multi-select mode.
|
|
*/
|
|
var selectionHolder: SelectionHolder<Tab>? = null
|
|
|
|
private val selectedItemAdapterBinding = SelectedItemAdapterBinding(store, this)
|
|
private val imageLoader = ThumbnailLoader(context.components.core.thumbnailStorage)
|
|
|
|
override fun getItemViewType(position: Int): Int {
|
|
return if (context.components.settings.gridTabView) {
|
|
ViewType.GRID.ordinal
|
|
} else {
|
|
ViewType.LIST.ordinal
|
|
}
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabsTrayViewHolder {
|
|
return when (viewType) {
|
|
ViewType.GRID.ordinal -> TabsTrayGridViewHolder(
|
|
parent,
|
|
imageLoader,
|
|
interactor,
|
|
store,
|
|
selectionHolder
|
|
)
|
|
else -> TabsTrayListViewHolder(
|
|
parent,
|
|
imageLoader,
|
|
interactor,
|
|
store,
|
|
selectionHolder
|
|
)
|
|
}
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: TabsTrayViewHolder, position: Int) {
|
|
super.onBindViewHolder(holder, position)
|
|
|
|
holder.tab?.let { tab ->
|
|
holder.itemView.mozac_browser_tabstray_close.setOnClickListener {
|
|
interactor.close(tab)
|
|
}
|
|
|
|
selectionHolder?.let {
|
|
holder.showTabIsMultiSelectEnabled(it.selectedItems.contains(tab))
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Over-ridden [onBindViewHolder] that uses the payloads to notify the selected tab how to
|
|
* display itself.
|
|
*/
|
|
override fun onBindViewHolder(holder: TabsTrayViewHolder, position: Int, payloads: List<Any>) {
|
|
val tabs = tabs ?: return
|
|
|
|
if (tabs.list.isEmpty()) return
|
|
|
|
if (payloads.isEmpty()) {
|
|
onBindViewHolder(holder, position)
|
|
return
|
|
}
|
|
|
|
if (position == tabs.selectedIndex) {
|
|
if (payloads.contains(PAYLOAD_HIGHLIGHT_SELECTED_ITEM)) {
|
|
holder.updateSelectedTabIndicator(true)
|
|
} else if (payloads.contains(PAYLOAD_DONT_HIGHLIGHT_SELECTED_ITEM)) {
|
|
holder.updateSelectedTabIndicator(false)
|
|
}
|
|
}
|
|
|
|
selectionHolder?.let {
|
|
holder.showTabIsMultiSelectEnabled(it.selectedItems.contains(holder.tab))
|
|
}
|
|
}
|
|
|
|
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
|
selectedItemAdapterBinding.start()
|
|
}
|
|
|
|
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
|
selectedItemAdapterBinding.start()
|
|
}
|
|
}
|