2019-12-30 20:39:04 +01:00
|
|
|
/* 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.home.sessioncontrol.viewholders.topsites
|
|
|
|
|
2020-06-24 14:21:50 +02:00
|
|
|
import android.annotation.SuppressLint
|
2020-01-03 17:29:37 +01:00
|
|
|
import android.content.Context
|
2020-06-24 14:21:50 +02:00
|
|
|
import android.view.MotionEvent
|
2019-12-30 20:39:04 +01:00
|
|
|
import android.view.View
|
2020-06-24 14:21:50 +02:00
|
|
|
import android.widget.PopupWindow
|
2020-07-14 19:31:50 +02:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources.getDrawable
|
2020-05-12 23:05:08 +02:00
|
|
|
import kotlinx.android.synthetic.main.top_site_item.*
|
2020-01-03 17:29:37 +01:00
|
|
|
import mozilla.components.browser.menu.BrowserMenuBuilder
|
|
|
|
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
|
2020-02-14 05:08:05 +01:00
|
|
|
import mozilla.components.feature.top.sites.TopSite
|
2019-12-30 20:39:04 +01:00
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.ext.components
|
|
|
|
import org.mozilla.fenix.ext.loadIntoView
|
|
|
|
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
|
2020-02-14 05:08:05 +01:00
|
|
|
import org.mozilla.fenix.settings.SupportUtils
|
2020-07-14 19:31:50 +02:00
|
|
|
import org.mozilla.fenix.utils.view.ViewHolder
|
2019-12-30 20:39:04 +01:00
|
|
|
|
|
|
|
class TopSiteItemViewHolder(
|
2020-05-12 23:05:08 +02:00
|
|
|
view: View,
|
2019-12-30 20:39:04 +01:00
|
|
|
private val interactor: TopSiteInteractor
|
2020-05-12 23:05:08 +02:00
|
|
|
) : ViewHolder(view) {
|
2019-12-30 20:39:04 +01:00
|
|
|
private lateinit var topSite: TopSite
|
2020-01-03 17:29:37 +01:00
|
|
|
private var topSiteMenu: TopSiteItemMenu
|
2019-12-30 20:39:04 +01:00
|
|
|
|
|
|
|
init {
|
2020-01-03 17:29:37 +01:00
|
|
|
topSiteMenu = TopSiteItemMenu(view.context) {
|
|
|
|
when (it) {
|
|
|
|
is TopSiteItemMenu.Item.OpenInPrivateTab -> interactor.onOpenInPrivateTabClicked(
|
|
|
|
topSite
|
|
|
|
)
|
|
|
|
is TopSiteItemMenu.Item.RemoveTopSite -> interactor.onRemoveTopSiteClicked(topSite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:05:08 +02:00
|
|
|
top_site_item.setOnClickListener {
|
2020-05-22 22:10:09 +02:00
|
|
|
interactor.onSelectTopSite(topSite.url, topSite.isDefault)
|
2019-12-30 20:39:04 +01:00
|
|
|
}
|
2020-01-03 17:29:37 +01:00
|
|
|
|
2020-07-14 19:31:50 +02:00
|
|
|
top_site_item.setOnLongClickListener {
|
2020-07-30 20:21:17 +02:00
|
|
|
val menu = topSiteMenu.menuBuilder.build(view.context).show(anchor = it)
|
2020-06-24 14:21:50 +02:00
|
|
|
it.setOnTouchListener @SuppressLint("ClickableViewAccessibility") { v, event ->
|
|
|
|
onTouchEvent(v, event, menu)
|
|
|
|
}
|
2020-07-14 19:31:50 +02:00
|
|
|
true
|
2020-01-03 17:29:37 +01:00
|
|
|
}
|
2019-12-30 20:39:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun bind(topSite: TopSite) {
|
|
|
|
this.topSite = topSite
|
2020-05-12 23:05:08 +02:00
|
|
|
top_site_title.text = topSite.title
|
|
|
|
when (topSite.url) {
|
|
|
|
SupportUtils.POCKET_TRENDING_URL -> {
|
2020-07-14 19:31:50 +02:00
|
|
|
favicon_image.setImageDrawable(getDrawable(itemView.context, R.drawable.ic_pocket))
|
2020-02-14 05:08:05 +01:00
|
|
|
}
|
|
|
|
else -> {
|
2020-05-12 23:05:08 +02:00
|
|
|
itemView.context.components.core.icons.loadIntoView(favicon_image, topSite.url)
|
2020-02-14 05:08:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 20:39:04 +01:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:21:50 +02:00
|
|
|
private fun onTouchEvent(
|
|
|
|
v: View,
|
|
|
|
event: MotionEvent,
|
|
|
|
menu: PopupWindow
|
|
|
|
): Boolean {
|
|
|
|
if (event.action == MotionEvent.ACTION_CANCEL) {
|
|
|
|
menu.dismiss()
|
|
|
|
}
|
|
|
|
return v.onTouchEvent(event)
|
|
|
|
}
|
|
|
|
|
2019-12-30 20:39:04 +01:00
|
|
|
companion object {
|
|
|
|
const val LAYOUT_ID = R.layout.top_site_item
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 17:29:37 +01:00
|
|
|
|
|
|
|
class TopSiteItemMenu(
|
|
|
|
private val context: Context,
|
|
|
|
private val onItemTapped: (Item) -> Unit = {}
|
|
|
|
) {
|
|
|
|
sealed class Item {
|
|
|
|
object OpenInPrivateTab : Item()
|
|
|
|
object RemoveTopSite : Item()
|
|
|
|
}
|
|
|
|
|
|
|
|
val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }
|
|
|
|
|
|
|
|
private val menuItems by lazy {
|
|
|
|
listOf(
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
context.getString(R.string.bookmark_menu_open_in_private_tab_button)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(Item.OpenInPrivateTab)
|
|
|
|
},
|
|
|
|
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
context.getString(R.string.remove_top_site)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(Item.RemoveTopSite)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|