Skip to content

Commit

Permalink
For mozilla-mobile#9599: Ability to edit top site URLs
Browse files Browse the repository at this point in the history
* Add functionality to edit both the title and the URL of a top site simultaneously
* Deprecate functionality to edit only the title of a top site
  • Loading branch information
pepve committed Feb 4, 2021
1 parent a4bcd42 commit 29788db
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ class DefaultTopSitesStorage(
}
}

override fun editTopSite(topSite: TopSite, title: String, url: String) {
scope.launch {
if (topSite.type != FRECENT) {
pinnedSitesStorage.editPinnedSite(topSite, title, url)
}

notifyObservers { onStorageUpdated() }
}
}

override suspend fun getTopSites(
totalSites: Int,
frecencyConfig: FrecencyThresholdOption?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ class PinnedSiteStorage(context: Context) {
pinnedSiteDao.updatePinnedSite(pinnedSite)
}

/**
* Edits the given pinned site.
*
* @param site The pinned site.
* @param title The new title for the top site.
* @param url The new url for the top site.
*/
suspend fun editPinnedSite(site: TopSite, title: String, url: String) = withContext(IO) {
val pinnedSite = site.toPinnedSite()
pinnedSite.title = title
pinnedSite.url = url
pinnedSiteDao.updatePinnedSite(pinnedSite)
}

/**
* Returns a count of pinned sites.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ interface TopSitesStorage : Observable<TopSitesStorage.Observer> {
*/
fun renameTopSite(topSite: TopSite, title: String)

/**
* Edits the given [TopSite].
*
* @param topSite The top site.
* @param title The new title for the top site.
* @param url The new url for the top site.
*/
fun editTopSite(topSite: TopSite, title: String, url: String)

/**
* Return a unified list of top sites based on the given number of sites desired.
* If `frecencyConfig` is specified, fill in any missing top sites with frecent top site results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ class TopSitesUseCases(topSitesStorage: TopSitesStorage) {
}
}

/**
* Edit a top site use case.
*/
class EditTopSiteUseCase internal constructor(private val storage: TopSitesStorage) {
/**
* Edits the given [TopSite].
*
* @param topSite The top site.
* @param title The new title for the top site.
* @param url The new url for the top site.
*/
operator fun invoke(topSite: TopSite, title: String, url: String) {
storage.editTopSite(topSite, title, url)
}
}

val addPinnedSites: AddPinnedSiteUseCase by lazy {
AddPinnedSiteUseCase(topSitesStorage)
}
Expand All @@ -73,7 +89,12 @@ class TopSitesUseCases(topSitesStorage: TopSitesStorage) {
RemoveTopSiteUseCase(topSitesStorage)
}

@Deprecated("Use editTopSites(topSite, title, url)")
val renameTopSites: RenameTopSiteUseCase by lazy {
RenameTopSiteUseCase(topSitesStorage)
}

val editTopSites: EditTopSiteUseCase by lazy {
EditTopSiteUseCase(topSitesStorage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ class TopSitesUseCasesTest {

verify(topSitesStorage).renameTopSite(topSite, title)
}

@Test
fun `EditTopSiteUseCase`() = runBlocking {
val topSitesStorage: TopSitesStorage = mock()
val topSite: TopSite = mock()
val useCases = TopSitesUseCases(topSitesStorage)

val title = "New title"
val url = "https://www.example.com/new-url"
useCases.editTopSites(topSite, title, url)

verify(topSitesStorage).editTopSite(topSite, title, url)
}
}
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ permalink: /changelog/
* **feature-webauthn**
* 🆕 New component to enable support for WebAuthn specification with `WebAuthnFeature`.

* **feature-top-sites**
* 🌟 Added `TopSitesUseCases.editTopSites` to edit both the name and the url of a pinned site. [#9599](https://github.com/mozilla-mobile/android-components/issues/9599)
* Deprecated `TopSitesUseCases.renameTopSites` which edits only the name of a pinned site. [#9599](https://github.com/mozilla-mobile/android-components/issues/9599)

# 72.0.0

* [Commits](https://github.com/mozilla-mobile/android-components/compare/v71.0.0...v72.0.0)
Expand Down

0 comments on commit 29788db

Please sign in to comment.