@ -13,8 +13,8 @@ import androidx.recyclerview.widget.RecyclerView
import org.mozilla.fenix.R
import org.mozilla.fenix.tabstray.TabsTrayInteractor
import org.mozilla.fenix.tabstray.TabsTrayStore
import org.mozilla.fenix.tabstray.TrayPagerAdapter
import org.mozilla.fenix.tabstray.browser.AbstractBrowserTrayList
import org.mozilla.fenix.tabstray.ext.observeFirstInsert
/ * *
* A shared view holder for browser tabs tray list .
@ -27,6 +27,9 @@ abstract class AbstractBrowserPageViewHolder(
private val trayList : AbstractBrowserTrayList = itemView . findViewById ( R . id . tray _list _item )
private val emptyList : TextView = itemView . findViewById ( R . id . tab _tray _empty _view )
private var adapterObserver : RecyclerView . AdapterDataObserver ? = null
private var adapterRef : RecyclerView . Adapter < out RecyclerView . ViewHolder > ? = null
abstract val emptyStringText : String
init {
@ -48,14 +51,53 @@ abstract class AbstractBrowserPageViewHolder(
adapter : RecyclerView . Adapter < out RecyclerView . ViewHolder > ,
layoutManager : RecyclerView . LayoutManager
) {
adapter . observeFirstInsert {
updateTrayVisibility ( adapter . itemCount )
}
adapterRef = adapter
scrollToTab ( adapter , layoutManager )
trayList . layoutManager = layoutManager
trayList . adapter = adapter
}
/ * *
* When the [ RecyclerView . Adapter ] is attached to the window we register a data observer to
* always check whether to call [ updateTrayVisibility ] .
*
* We keep a constant observer instead of using [ RecyclerView . Adapter . observeFirstInsert ] ,
* because some adapters can insert empty lists and trigger the one - shot observer too soon .
*
* See also [ AbstractPageViewHolder . attachedToWindow ] .
* /
override fun attachedToWindow ( ) {
adapterRef ?. let { adapter ->
adapterObserver = object : RecyclerView . AdapterDataObserver ( ) {
override fun onItemRangeInserted ( positionStart : Int , itemCount : Int ) {
updateTrayVisibility ( adapter . itemCount )
}
override fun onItemRangeRemoved ( positionstart : Int , itemcount : Int ) {
updateTrayVisibility ( adapter . itemCount )
}
}
adapterObserver ?. let {
adapter . registerAdapterDataObserver ( it )
}
}
}
/ * *
* [ RecyclerView . AdapterDataObserver ] s are responsible to be unregistered when they are done ,
* so we do that here when our [ TrayPagerAdapter ] page is detached from the window .
*
* See also [ AbstractPageViewHolder . detachedFromWindow ] .
* /
override fun detachedFromWindow ( ) {
adapterObserver ?. let {
adapterRef ?. unregisterAdapterDataObserver ( it )
adapterObserver = null
}
}
private fun updateTrayVisibility ( size : Int ) {
if ( size == 0 ) {
trayList . visibility = GONE