ShareTabsAdapter.kt 1.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 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.share.listadapters

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 kotlinx.android.synthetic.main.share_tab_item.view.*
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.loadIntoView
import org.mozilla.fenix.share.ShareTab

19
20
21
/**
 * Adapter for a list of tabs to be shared.
 */
22
class ShareTabsAdapter :
23
    ListAdapter<ShareTab, ShareTabsAdapter.ShareTabViewHolder>(ShareTabDiffCallback) {
24
25
26
27
28
29
30
31
32

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ShareTabViewHolder(
        LayoutInflater.from(parent.context)
            .inflate(R.layout.share_tab_item, parent, false)
    )

    override fun onBindViewHolder(holder: ShareTabViewHolder, position: Int) =
        holder.bind(getItem(position))

33
    class ShareTabViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
34
35
36
37
38
39
40
41

        fun bind(item: ShareTab) = with(itemView) {
            context.components.core.icons.loadIntoView(itemView.share_tab_favicon, item.url)
            itemView.share_tab_title.text = item.title
            itemView.share_tab_url.text = item.url
        }
    }

42
43
44
    private object ShareTabDiffCallback : DiffUtil.ItemCallback<ShareTab>() {
        override fun areItemsTheSame(oldItem: ShareTab, newItem: ShareTab) =
            oldItem.url == newItem.url
45

46
47
        override fun areContentsTheSame(oldItem: ShareTab, newItem: ShareTab) =
            oldItem == newItem
48
49
    }
}