Skip to content

Commit

Permalink
For mozilla-mobile#9548 - Add ability to rename top sites
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielluong committed Oct 28, 2020
1 parent 7d59a58 commit 0e3174b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
29 changes: 29 additions & 0 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -65,6 +66,7 @@ import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.feature.top.sites.TopSitesConfig
import mozilla.components.feature.top.sites.TopSitesFeature
import mozilla.components.lib.state.ext.consumeFrom
Expand Down Expand Up @@ -241,6 +243,7 @@ class HomeFragment : Fragment() {
hideOnboarding = ::hideOnboardingAndOpenSearch,
registerCollectionStorageObserver = ::registerCollectionStorageObserver,
showDeleteCollectionPrompt = ::showDeleteCollectionPrompt,
showRenameTopSitePrompt = ::showRenameTopSitePrompt,
showTabTray = ::openTabTray,
handleSwipedItemDeletionCancel = ::handleSwipedItemDeletionCancel
)
Expand Down Expand Up @@ -637,6 +640,32 @@ class HomeFragment : Fragment() {
}.show()
}

private fun showRenameTopSitePrompt(topSite: TopSite) {
val context = context ?: return
val customLayout =
LayoutInflater.from(context).inflate(R.layout.top_sites_rename_dialog, null)
val topSiteRenameEditText: EditText = customLayout.findViewById(R.id.top_site_name)
topSiteRenameEditText.setText(topSite.title)

AlertDialog.Builder(context).apply {
setView(customLayout)
setTitle(R.string.top_sites_rename_dialog_title)
setNegativeButton(R.string.top_sites_rename_dialog_cancel) { dialog: DialogInterface, _ ->
dialog.cancel()
}
setPositiveButton(R.string.top_sites_rename_dialog_ok) { dialog: DialogInterface, _ ->
lifecycleScope.launch(IO) {
with(requireComponents.useCases.topSitesUseCase) {
renameTopSites(topSite, topSiteRenameEditText.text.toString())
}
launch(Main) {
dialog.dismiss()
}
}
}
}.show()
}

override fun onStop() {
super.onStop()
val homeViewModel: HomeScreenViewModel by activityViewModels {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ interface SessionControlController {
*/
fun handleRemoveTopSiteClicked(topSite: TopSite)

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

/**
* @see [CollectionInteractor.onRenameCollectionTapped]
*/
Expand Down Expand Up @@ -172,6 +177,7 @@ class DefaultSessionControlController(
wasSwiped: Boolean,
handleSwipedItemDeletionCancel: () -> Unit
) -> Unit,
private val showRenameTopSitePrompt: (topSite: TopSite) -> Unit,
private val showTabTray: () -> Unit,
private val handleSwipedItemDeletionCancel: () -> Unit
) : SessionControlController {
Expand Down Expand Up @@ -294,6 +300,10 @@ class DefaultSessionControlController(
}
}

override fun handleRenameTopSiteClicked(topSite: TopSite) {
showRenameTopSitePrompt(topSite)
}

override fun handleRenameCollectionTapped(collection: TabCollection) {
showCollectionCreationFragment(
step = SaveCollectionStep.RenameCollection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ interface TopSiteInteractor {
*/
fun onRemoveTopSiteClicked(topSite: TopSite)

/**
* Renames 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)

/**
* Selects the given top site. Called when a user clicks on a top site.
*
Expand Down Expand Up @@ -214,6 +221,10 @@ class SessionControlInteractor(
controller.handleRemoveTopSiteClicked(topSite)
}

override fun onRenameTopSiteClicked(topSite: TopSite) {
controller.handleRenameTopSiteClicked(topSite)
}

override fun onRenameCollectionTapped(collection: TabCollection) {
controller.handleRenameCollectionTapped(collection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class TopSiteItemViewHolder(
is TopSiteItemMenu.Item.RemoveTopSite -> interactor.onRemoveTopSiteClicked(
topSite
)
is TopSiteItemMenu.Item.RenameTopSite -> interactor.onRenameTopSiteClicked(
topSite
)
}
}
val menu = topSiteMenu.menuBuilder.build(view.context).show(anchor = it)
Expand Down Expand Up @@ -101,12 +104,13 @@ class TopSiteItemMenu(
sealed class Item {
object OpenInPrivateTab : Item()
object RemoveTopSite : Item()
object RenameTopSite : Item()
}

val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }

private val menuItems by lazy {
listOf(
val menuItems = mutableListOf(
SimpleBrowserMenuItem(
context.getString(R.string.bookmark_menu_open_in_private_tab_button)
) {
Expand All @@ -122,5 +126,15 @@ class TopSiteItemMenu(
onItemTapped.invoke(Item.RemoveTopSite)
}
)

if (isPinnedSite) {
menuItems.add(1, SimpleBrowserMenuItem(
context.getString(R.string.rename_top_site)
) {
onItemTapped.invoke(Item.RenameTopSite)
})
}

menuItems
}
}
34 changes: 34 additions & 0 deletions app/src/main/res/layout/top_sites_rename_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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">

<TextView
android:id="@+id/rename_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:text="@string/top_sites_rename_dialog_title"
android:textAllCaps="true"
android:textAppearance="@style/Body16TextStyle"
android:textColor="?secondaryText" />

<EditText
android:id="@+id/top_site_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:autofillHints="false"
android:backgroundTint="?neutral"
android:hint="@string/top_site_name_hint"
android:inputType="textCapSentences"
android:singleLine="true"
android:textAlignment="viewStart" />
</LinearLayout>
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@
<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>
<!-- 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 Expand Up @@ -1583,6 +1585,14 @@
<string name="top_sites_max_limit_confirmation_button">OK, Got It</string>
<!-- Label for the show most visited sites preference -->
<string name="top_sites_toggle_top_frecent_sites">Show most visited sites</string>
<!-- Title text displayed in the rename top site dialog. -->
<string name="top_sites_rename_dialog_title">Rename</string>
<!-- Hint for renaming title of a top site -->
<string name="top_site_name_hint">Top site name</string>
<!-- Button caption to confirm the add-on collection configuration -->
<string name="top_sites_rename_dialog_ok">OK</string>
<!-- Dialog button text for canceling the rename top site prompt. -->
<string name="top_sites_rename_dialog_cancel">Cancel</string>

<!-- Content description for close button in collection placeholder. -->
<string name="remove_home_collection_placeholder_content_description">Remove</string>
Expand Down

0 comments on commit 0e3174b

Please sign in to comment.