GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit d306e37a authored by Tiger Oakes's avatar Tiger Oakes Committed by liuche

For #12887: Use ListAdapter to diff history

parent bc9000ce
...@@ -67,6 +67,8 @@ class LibrarySiteItemView @JvmOverloads constructor( ...@@ -67,6 +67,8 @@ class LibrarySiteItemView @JvmOverloads constructor(
val overflowView: ImageButton get() = overflow_menu val overflowView: ImageButton get() = overflow_menu
private var iconUrl: String? = null
init { init {
LayoutInflater.from(context).inflate(R.layout.library_site_item, this, true) LayoutInflater.from(context).inflate(R.layout.library_site_item, this, true)
...@@ -94,6 +96,9 @@ class LibrarySiteItemView @JvmOverloads constructor( ...@@ -94,6 +96,9 @@ class LibrarySiteItemView @JvmOverloads constructor(
} }
fun loadFavicon(url: String) { fun loadFavicon(url: String) {
if (iconUrl == url) return
iconUrl = url
context.components.core.icons.loadIntoView(favicon, url) context.components.core.icons.loadIntoView(favicon, url)
} }
......
...@@ -6,7 +6,8 @@ package org.mozilla.fenix.tabhistory ...@@ -6,7 +6,8 @@ package org.mozilla.fenix.tabhistory
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import org.mozilla.fenix.R import org.mozilla.fenix.R
data class TabHistoryItem( data class TabHistoryItem(
...@@ -18,23 +19,23 @@ data class TabHistoryItem( ...@@ -18,23 +19,23 @@ data class TabHistoryItem(
class TabHistoryAdapter( class TabHistoryAdapter(
private val interactor: TabHistoryViewInteractor private val interactor: TabHistoryViewInteractor
) : RecyclerView.Adapter<TabHistoryViewHolder>() { ) : ListAdapter<TabHistoryItem, TabHistoryViewHolder>(DiffCallback) {
var historyList: List<TabHistoryItem> = emptyList()
set(value) {
field = value
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabHistoryViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabHistoryViewHolder {
val view = val view = LayoutInflater.from(parent.context)
LayoutInflater.from(parent.context).inflate(R.layout.history_list_item, parent, false) .inflate(R.layout.history_list_item, parent, false)
return TabHistoryViewHolder(view, interactor) return TabHistoryViewHolder(view, interactor)
} }
override fun onBindViewHolder(holder: TabHistoryViewHolder, position: Int) { override fun onBindViewHolder(holder: TabHistoryViewHolder, position: Int) {
holder.bind(historyList[position]) holder.bind(getItem(position))
} }
override fun getItemCount(): Int = historyList.size internal object DiffCallback : DiffUtil.ItemCallback<TabHistoryItem>() {
override fun areItemsTheSame(oldItem: TabHistoryItem, newItem: TabHistoryItem) =
oldItem.url == newItem.url
override fun areContentsTheSame(oldItem: TabHistoryItem, newItem: TabHistoryItem) =
oldItem == newItem
}
} }
...@@ -24,19 +24,16 @@ interface TabHistoryViewInteractor { ...@@ -24,19 +24,16 @@ interface TabHistoryViewInteractor {
} }
class TabHistoryView( class TabHistoryView(
private val container: ViewGroup, container: ViewGroup,
private val expandDialog: () -> Unit, private val expandDialog: () -> Unit,
interactor: TabHistoryViewInteractor interactor: TabHistoryViewInteractor
) : LayoutContainer { ) : LayoutContainer {
override val containerView: View? override val containerView: View = LayoutInflater.from(container.context)
get() = container
val view: View = LayoutInflater.from(container.context)
.inflate(R.layout.component_tabhistory, container, true) .inflate(R.layout.component_tabhistory, container, true)
private val adapter = TabHistoryAdapter(interactor) private val adapter = TabHistoryAdapter(interactor)
private val layoutManager = object : LinearLayoutManager(view.context) { private val layoutManager = object : LinearLayoutManager(containerView.context) {
override fun onLayoutCompleted(state: RecyclerView.State?) { override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state) super.onLayoutCompleted(state)
currentIndex?.let { index -> currentIndex?.let { index ->
...@@ -60,6 +57,7 @@ class TabHistoryView( ...@@ -60,6 +57,7 @@ class TabHistoryView(
init { init {
tabHistoryRecyclerView.adapter = adapter tabHistoryRecyclerView.adapter = adapter
tabHistoryRecyclerView.layoutManager = layoutManager tabHistoryRecyclerView.layoutManager = layoutManager
tabHistoryRecyclerView.itemAnimator = null
} }
fun updateState(state: BrowserState) { fun updateState(state: BrowserState) {
...@@ -73,7 +71,7 @@ class TabHistoryView( ...@@ -73,7 +71,7 @@ class TabHistoryView(
isSelected = index == historyState.currentIndex isSelected = index == historyState.currentIndex
) )
} }
adapter.historyList = items adapter.submitList(items)
} }
} }
} }
...@@ -7,28 +7,34 @@ package org.mozilla.fenix.tabhistory ...@@ -7,28 +7,34 @@ package org.mozilla.fenix.tabhistory
import android.view.View import android.view.View
import androidx.core.text.bold import androidx.core.text.bold
import androidx.core.text.buildSpannedString import androidx.core.text.buildSpannedString
import androidx.core.view.isVisible import kotlinx.android.synthetic.main.history_list_item.*
import androidx.recyclerview.widget.RecyclerView import org.mozilla.fenix.library.LibrarySiteItemView
import kotlinx.android.synthetic.main.history_list_item.view.* import org.mozilla.fenix.utils.view.ViewHolder
class TabHistoryViewHolder( class TabHistoryViewHolder(
private val view: View, view: View,
private val interactor: TabHistoryViewInteractor private val interactor: TabHistoryViewInteractor
) : RecyclerView.ViewHolder(view) { ) : ViewHolder(view) {
private lateinit var item: TabHistoryItem
init {
itemView.setOnClickListener { interactor.goToHistoryItem(item) }
}
fun bind(item: TabHistoryItem) { fun bind(item: TabHistoryItem) {
view.history_layout.overflowView.isVisible = false this.item = item
view.history_layout.urlView.text = item.url
view.history_layout.loadFavicon(item.url)
view.history_layout.titleView.text = if (item.isSelected) { history_layout.displayAs(LibrarySiteItemView.ItemType.SITE)
history_layout.urlView.text = item.url
history_layout.loadFavicon(item.url)
history_layout.titleView.text = if (item.isSelected) {
buildSpannedString { buildSpannedString {
bold { append(item.title) } bold { append(item.title) }
} }
} else { } else {
item.title item.title
} }
view.setOnClickListener { interactor.goToHistoryItem(item) }
} }
} }
Markdown is supported
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