From 2533bf14313d77e5387cd97eb35dc20d4f34ca22 Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Wed, 26 Jan 2022 12:12:45 -0500 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/23424 - Part 1: Refactor TopSiteItemMenu into its own file --- .../fenix/home/topsites/TopSiteItemMenu.kt | 48 +++++++++++++++++++ .../home/topsites/TopSiteItemViewHolder.kt | 41 ---------------- 2 files changed, 48 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemMenu.kt diff --git a/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemMenu.kt b/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemMenu.kt new file mode 100644 index 0000000000..cda98e5124 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemMenu.kt @@ -0,0 +1,48 @@ +/* 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.topsites + +import android.content.Context +import mozilla.components.browser.menu.BrowserMenuBuilder +import mozilla.components.browser.menu.item.SimpleBrowserMenuItem +import org.mozilla.fenix.R + +class TopSiteItemMenu( + private val context: Context, + private val isPinnedSite: Boolean, + private val onItemTapped: (Item) -> Unit = {} +) { + sealed class Item { + object OpenInPrivateTab : Item() + object RenameTopSite : Item() + object RemoveTopSite : Item() + } + + val menuBuilder by lazy { BrowserMenuBuilder(menuItems) } + + private val menuItems by lazy { + listOfNotNull( + SimpleBrowserMenuItem( + context.getString(R.string.bookmark_menu_open_in_private_tab_button) + ) { + onItemTapped.invoke(Item.OpenInPrivateTab) + }, + if (isPinnedSite) SimpleBrowserMenuItem( + context.getString(R.string.rename_top_site) + ) { + onItemTapped.invoke(Item.RenameTopSite) + } else null, + SimpleBrowserMenuItem( + if (isPinnedSite) { + context.getString(R.string.remove_top_site) + } else { + context.getString(R.string.delete_from_history) + } + ) { + onItemTapped.invoke(Item.RemoveTopSite) + } + ) + } +} diff --git a/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemViewHolder.kt b/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemViewHolder.kt index 45a40a24d9..915bb3b2f5 100644 --- a/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemViewHolder.kt @@ -5,7 +5,6 @@ package org.mozilla.fenix.home.topsites import android.annotation.SuppressLint -import android.content.Context import android.view.MotionEvent import android.view.View import android.widget.PopupWindow @@ -15,8 +14,6 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import mozilla.components.browser.menu.BrowserMenuBuilder -import mozilla.components.browser.menu.item.SimpleBrowserMenuItem import mozilla.components.feature.top.sites.TopSite import org.mozilla.fenix.R import org.mozilla.fenix.components.metrics.Event @@ -130,41 +127,3 @@ class TopSiteItemViewHolder( const val LAYOUT_ID = R.layout.top_site_item } } - -class TopSiteItemMenu( - private val context: Context, - private val isPinnedSite: Boolean, - private val onItemTapped: (Item) -> Unit = {} -) { - sealed class Item { - object OpenInPrivateTab : Item() - object RenameTopSite : Item() - object RemoveTopSite : Item() - } - - val menuBuilder by lazy { BrowserMenuBuilder(menuItems) } - - private val menuItems by lazy { - listOfNotNull( - SimpleBrowserMenuItem( - context.getString(R.string.bookmark_menu_open_in_private_tab_button) - ) { - onItemTapped.invoke(Item.OpenInPrivateTab) - }, - if (isPinnedSite) SimpleBrowserMenuItem( - context.getString(R.string.rename_top_site) - ) { - onItemTapped.invoke(Item.RenameTopSite) - } else null, - SimpleBrowserMenuItem( - if (isPinnedSite) { - context.getString(R.string.remove_top_site) - } else { - context.getString(R.string.delete_from_history) - } - ) { - onItemTapped.invoke(Item.RemoveTopSite) - } - ) - } -}