Skip to content

Commit

Permalink
Ability to rename top sites, closes mozilla-mobile#9548
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzos authored and gabrielluong committed Oct 30, 2020
1 parent ff8e92b commit 71e5513
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -85,6 +89,11 @@ interface SessionControlController {
*/
fun handlePrivateBrowsingLearnMoreClicked()

/**
* @see [TopSiteInteractor.onRenameTopSiteClicked]
*/
fun handleRenameTopSiteClicked(topSite: TopSite)

/**
* @see [TopSiteInteractor.onRemoveTopSiteClicked]
*/
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/layout/rename_top_site_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<EditText
android:id="@+id/top_site_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:autofillHints="false"
android:backgroundTint="?neutral"
android:hint="@string/rename_top_site_hint"
android:inputType="textCapSentences"
android:singleLine="true"
android:textAlignment="viewStart" />
</LinearLayout>
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,12 @@
<string name="collection_open_tabs">Open tabs</string>
<!-- Hint for adding name of a collection -->
<string name="collection_name_hint">Collection name</string>
<!-- Text for the menu button to remove a top site -->
<string name="remove_top_site">Remove</string>
<!-- Text for the menu button to rename a top site -->
<string name="rename_top_site">Rename</string>
<!-- Hint for renaming a top site -->
<string name="rename_top_site_hint">Top site title</string>
<!-- Text for the menu button to remove a top site -->
<string name="remove_top_site">Remove</string>
<!-- Text for the menu button to delete a top site from history -->
<string name="delete_from_history">Delete from history</string>
<!-- Postfix for private WebApp titles, placeholder is replaced with app name -->
Expand Down

0 comments on commit 71e5513

Please sign in to comment.