2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-05 21:20:45 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/23424 - Part 1: Refactor TopSiteItemMenu into its own file

This commit is contained in:
Gabriel Luong 2022-01-26 12:12:45 -05:00 committed by mergify[bot]
parent b6c347c065
commit 2533bf1431
2 changed files with 48 additions and 41 deletions

View File

@ -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)
}
)
}
}

View File

@ -5,7 +5,6 @@
package org.mozilla.fenix.home.topsites package org.mozilla.fenix.home.topsites
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import android.widget.PopupWindow import android.widget.PopupWindow
@ -15,8 +14,6 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext 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 mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.Event
@ -130,41 +127,3 @@ class TopSiteItemViewHolder(
const val LAYOUT_ID = R.layout.top_site_item 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)
}
)
}
}