Skip to content

Commit

Permalink
For mozilla-mobile#6758 - Part 7: Add menu for opening a top site in …
Browse files Browse the repository at this point in the history
…private tab and removing a top site
  • Loading branch information
gabrielluong committed Jan 23, 2020
1 parent 9cc02ef commit b8d60f9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import mozilla.components.feature.media.ext.pauseIfPlaying
import mozilla.components.feature.media.ext.playIfPaused
import mozilla.components.feature.media.state.MediaStateMachine
import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.feature.tab.collections.Tab as ComponentTab
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.collections.SaveCollectionStep
import org.mozilla.fenix.components.TabCollectionStorage
import org.mozilla.fenix.components.TopSiteStorage
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
Expand Down Expand Up @@ -82,6 +85,11 @@ interface SessionControlController {
*/
fun handleDeleteCollectionTapped(collection: TabCollection)

/**
* @see [TopSiteInteractor.onOpenInPrivateTabClicked]
*/
fun handleOpenInPrivateTabClicked(topSite: TopSite)

/**
* @see [TabSessionInteractor.onPauseMediaClicked]
*/
Expand All @@ -97,6 +105,11 @@ interface SessionControlController {
*/
fun handlePrivateBrowsingLearnMoreClicked()

/**
* @see [TopSiteInteractor.onRemoveTopSiteClicked]
*/
fun handleRemoveTopSiteClicked(topSite: TopSite)

/**
* @see [CollectionInteractor.onRenameCollectionTapped]
*/
Expand Down Expand Up @@ -133,7 +146,7 @@ interface SessionControlController {
fun handleToggleCollectionExpanded(collection: TabCollection, expand: Boolean)
}

@SuppressWarnings("TooManyFunctions")
@SuppressWarnings("TooManyFunctions", "LargeClass")
class DefaultSessionControlController(
private val activity: HomeActivity,
private val store: HomeFragmentStore,
Expand All @@ -156,6 +169,8 @@ class DefaultSessionControlController(
get() = activity.components.core.sessionManager
private val tabCollectionStorage: TabCollectionStorage
get() = activity.components.core.tabCollectionStorage
private val topSiteStorage: TopSiteStorage
get() = activity.components.core.topSiteStorage

override fun handleCloseTab(sessionId: String) {
closeTab.invoke(sessionId)
Expand Down Expand Up @@ -244,6 +259,17 @@ class DefaultSessionControlController(
showDeleteCollectionPrompt(collection)
}

override fun handleOpenInPrivateTabClicked(topSite: TopSite) {
with(activity) {
browsingModeManager.mode = BrowsingMode.Private
openToBrowserAndLoad(
searchTermOrURL = topSite.url,
newTab = true,
from = BrowserDirection.FromHome
)
}
}

override fun handlePauseMediaClicked() {
MediaStateMachine.state.pauseIfPlaying()
}
Expand All @@ -261,6 +287,12 @@ class DefaultSessionControlController(
)
}

override fun handleRemoveTopSiteClicked(topSite: TopSite) {
lifecycleScope.launch(Dispatchers.IO) {
topSiteStorage.removeTopSite(topSite)
}
}

override fun handleRenameCollectionTapped(collection: TabCollection) {
showCollectionCreationFragment(
step = SaveCollectionStep.RenameCollection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.mozilla.fenix.home.sessioncontrol
import android.view.View
import mozilla.components.feature.tab.collections.Tab
import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.top.sites.TopSite

/**
* Interface for collection related actions in the [SessionControlInteractor].
Expand Down Expand Up @@ -153,6 +154,21 @@ interface TabSessionInteractor {
* Interface for top site related actions in the [SessionControlInteractor].
*/
interface TopSiteInteractor {
/**
* Opens the given top site in private mode. Called when an user clicks on the "Open in private
* tab" top site menu item.
*
* @param topSite The top site that will be open in private mode.
*/
fun onOpenInPrivateTabClicked(topSite: TopSite)

/**
* Removes the given top site. Called when an user clicks on the "Remove" top site menu item.
*
* @param topSite The top site that will be removed.
*/
fun onRemoveTopSiteClicked(topSite: TopSite)

/**
* Selects the given top site. Called when a user clicks on a top site.
*
Expand Down Expand Up @@ -202,6 +218,10 @@ class SessionControlInteractor(
controller.handleDeleteCollectionTapped(collection)
}

override fun onOpenInPrivateTabClicked(topSite: TopSite) {
controller.handleOpenInPrivateTabClicked(topSite)
}

override fun onPauseMediaClicked() {
controller.handlePauseMediaClicked()
}
Expand All @@ -214,6 +234,10 @@ class SessionControlInteractor(
controller.handlePrivateBrowsingLearnMoreClicked()
}

override fun onRemoveTopSiteClicked(topSite: TopSite) {
controller.handleRemoveTopSiteClicked(topSite)
}

override fun onRenameCollectionTapped(collection: TabCollection) {
controller.handleRenameCollectionTapped(collection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

package org.mozilla.fenix.home.sessioncontrol.viewholders.topsites

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.top_site_item.view.*
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.loadIntoView
Expand All @@ -18,11 +21,26 @@ class TopSiteItemViewHolder(
private val interactor: TopSiteInteractor
) : RecyclerView.ViewHolder(view) {
private lateinit var topSite: TopSite
private var topSiteMenu: TopSiteItemMenu

init {
topSiteMenu = TopSiteItemMenu(view.context) {
when (it) {
is TopSiteItemMenu.Item.OpenInPrivateTab -> interactor.onOpenInPrivateTabClicked(
topSite
)
is TopSiteItemMenu.Item.RemoveTopSite -> interactor.onRemoveTopSiteClicked(topSite)
}
}

view.top_site_item.setOnClickListener {
interactor.onSelectTopSite(topSite.url)
}

view.top_site_item.setOnLongClickListener() {
topSiteMenu.menuBuilder.build(view.context).show(anchor = it)
return@setOnLongClickListener true
}
}

fun bind(topSite: TopSite) {
Expand All @@ -35,3 +53,31 @@ class TopSiteItemViewHolder(
const val LAYOUT_ID = R.layout.top_site_item
}
}

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)
}
)
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@
<string name="collection_rename">Rename collection</string>
<!-- Text for the button to open tabs of the selected collection -->
<string name="collection_open_tabs">Open tabs</string>
<!-- Text for the menu button to remove a top site -->
<string name="remove_top_site">Remove</string>

<!-- History -->
<!-- Text for the button to clear all history -->
Expand Down

0 comments on commit b8d60f9

Please sign in to comment.