From 71e551357403d8d9204b51b54922b3445b0cd340 Mon Sep 17 00:00:00 2001 From: Lorenzo Stanco Date: Mon, 26 Oct 2020 21:29:13 +0100 Subject: [PATCH] Ability to rename top sites, closes #9548 --- .../SessionControlController.kt | 38 +++++++++++++++++++ .../SessionControlInteractor.kt | 11 ++++++ .../topsites/TopSiteItemViewHolder.kt | 12 +++++- .../res/layout/rename_top_site_dialog.xml | 23 +++++++++++ app/src/main/res/values/strings.xml | 8 +++- 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/layout/rename_top_site_dialog.xml diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt index 8b0ff89406c4..5289b800c9bf 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt @@ -4,6 +4,9 @@ package org.mozilla.fenix.home.sessioncontrol +import android.view.LayoutInflater +import android.widget.EditText +import androidx.appcompat.app.AlertDialog import androidx.navigation.NavController import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -15,6 +18,7 @@ import mozilla.components.feature.tab.collections.TabCollection import mozilla.components.feature.tab.collections.ext.restore import mozilla.components.feature.tabs.TabsUseCases import mozilla.components.feature.top.sites.TopSite +import mozilla.components.support.ktx.android.view.showKeyboard import mozilla.components.support.ktx.kotlin.isUrl import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.HomeActivity @@ -85,6 +89,11 @@ interface SessionControlController { */ fun handlePrivateBrowsingLearnMoreClicked() + /** + * @see [TopSiteInteractor.onRenameTopSiteClicked] + */ + fun handleRenameTopSiteClicked(topSite: TopSite) + /** * @see [TopSiteInteractor.onRemoveTopSiteClicked] */ @@ -281,6 +290,35 @@ class DefaultSessionControlController( ) } + override fun handleRenameTopSiteClicked(topSite: TopSite) { + if (topSite.id != null) { + activity.let { + val customLayout = + LayoutInflater.from(it).inflate(R.layout.rename_top_site_dialog, null) + val topSiteLabelEditText: EditText = + customLayout.findViewById(R.id.top_site_title) + topSiteLabelEditText.setText(topSite.title) + + AlertDialog.Builder(it).setTitle(R.string.rename_top_site) + .setView(customLayout).setPositiveButton(android.R.string.ok) { _, _ -> + val newTitle = topSiteLabelEditText.text.toString() + if (newTitle.isNotBlank()) { + // TODO: it.metrics.track(Event.TopSiteRenamed) + viewLifecycleScope.launch(Dispatchers.IO) { + with(activity.components.useCases.topSitesUseCase) { + renameTopSites(topSite, newTitle) + } + } + } + }.setNegativeButton(android.R.string.cancel, null) + .create().show().also { + topSiteLabelEditText.setSelection(0, topSiteLabelEditText.text.length) + topSiteLabelEditText.showKeyboard() + } + } + } + } + override fun handleRemoveTopSiteClicked(topSite: TopSite) { metrics.track(Event.TopSiteRemoved) if (topSite.url == SupportUtils.POCKET_TRENDING_URL) { diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlInteractor.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlInteractor.kt index b600d098343f..6237920ae91e 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlInteractor.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlInteractor.kt @@ -156,6 +156,13 @@ interface TopSiteInteractor { */ fun onOpenInPrivateTabClicked(topSite: TopSite) + /** + * Opens a dialog to rename the given top site. Called when an user clicks on the "Rename" top site menu item. + * + * @param topSite The top site that will be renamed. + */ + fun onRenameTopSiteClicked(topSite: TopSite) + /** * Removes the given top site. Called when an user clicks on the "Remove" top site menu item. * @@ -210,6 +217,10 @@ class SessionControlInteractor( controller.handleOpenInPrivateTabClicked(topSite) } + override fun onRenameTopSiteClicked(topSite: TopSite) { + controller.handleRenameTopSiteClicked(topSite) + } + override fun onRemoveTopSiteClicked(topSite: TopSite) { controller.handleRemoveTopSiteClicked(topSite) } diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/topsites/TopSiteItemViewHolder.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/topsites/TopSiteItemViewHolder.kt index 0b6fe85f741d..ff47c00d2f3e 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/topsites/TopSiteItemViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/topsites/TopSiteItemViewHolder.kt @@ -22,6 +22,7 @@ import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.loadIntoView import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor import org.mozilla.fenix.settings.SupportUtils +import org.mozilla.fenix.theme.ThemeManager import org.mozilla.fenix.utils.view.ViewHolder class TopSiteItemViewHolder( @@ -43,6 +44,9 @@ class TopSiteItemViewHolder( is TopSiteItemMenu.Item.OpenInPrivateTab -> interactor.onOpenInPrivateTabClicked( topSite ) + is TopSiteItemMenu.Item.RenameTopSite -> interactor.onRenameTopSiteClicked( + topSite + ) is TopSiteItemMenu.Item.RemoveTopSite -> interactor.onRemoveTopSiteClicked( topSite ) @@ -100,18 +104,24 @@ class TopSiteItemMenu( ) { sealed class Item { object OpenInPrivateTab : Item() + object RenameTopSite : Item() object RemoveTopSite : Item() } val menuBuilder by lazy { BrowserMenuBuilder(menuItems) } private val menuItems by lazy { - listOf( + 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) diff --git a/app/src/main/res/layout/rename_top_site_dialog.xml b/app/src/main/res/layout/rename_top_site_dialog.xml new file mode 100644 index 000000000000..7a5ebcde23d9 --- /dev/null +++ b/app/src/main/res/layout/rename_top_site_dialog.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c36f3a39234b..a292dc3bd682 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -611,8 +611,12 @@ Open tabs Collection name - - Remove + + Rename + + Top site title + + Remove Delete from history