Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Closes #8751: Added methods to rename top site entries #8799

Merged
merged 10 commits into from
Oct 27, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ class PinnedSiteDaoTest {
assertEquals(topSite, pinnedSites[0])
}

@Test
fun testUpdatingTopSite() {
val topSite = PinnedSiteEntity(
title = "Mozilla",
url = "https://www.mozilla.org",
isDefault = false,
createdAt = 200
).also {
it.id = pinnedSiteDao.insertPinnedSite(it)
}

topSite.title = "Mozilla (IT)"
topSite.url = "https://www.mozilla.org/it"
pinnedSiteDao.updatePinnedSite(topSite)

val pinnedSites = pinnedSiteDao.getPinnedSites()

assertEquals(1, pinnedSites.size)
assertEquals(topSite, pinnedSites[0])
assertEquals(topSite.title, pinnedSites[0].title)
assertEquals(topSite.url, pinnedSites[0].url)
}

@Test
fun testRemovingTopSite() {
val topSite1 = PinnedSiteEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class DefaultTopSitesStorage(
}
}

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

notifyObservers { onStorageUpdated() }
}
}

override fun removeTopSite(topSite: TopSite) {
scope.launch {
if (topSite.type != FRECENT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ class PinnedSiteStorage(context: Context) {
pinnedSiteDao.getPinnedSites().map { entity -> entity.toTopSite() }
}

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

/**
* Removes the given pinned site.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ interface TopSitesStorage : Observable<TopSitesStorage.Observer> {
*/
fun addTopSite(title: String, url: String, isDefault: Boolean = false)

/**
* Renames the given [TopSite].
*
* @param topSite The top site.
* @param title The new title for the top site.
*/
fun renameTopSite(topSite: TopSite, title: String)
lorenzos marked this conversation as resolved.
Show resolved Hide resolved

/**
* Removes the given [TopSite].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ class TopSitesUseCases(topSitesStorage: TopSitesStorage) {
}
}

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

lorenzos marked this conversation as resolved.
Show resolved Hide resolved
/**
* Remove a top site use case.
*/
Expand All @@ -41,6 +56,10 @@ class TopSitesUseCases(topSitesStorage: TopSitesStorage) {
AddPinnedSiteUseCase(topSitesStorage)
}

val renameTopSites: RenameTopSiteUseCase by lazy {
RenameTopSiteUseCase(topSitesStorage)
}

lorenzos marked this conversation as resolved.
Show resolved Hide resolved
val removeTopSites: RemoveTopSiteUseCase by lazy {
RemoveTopSiteUseCase(topSitesStorage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update

/**
* Internal DAO for accessing [PinnedSiteEntity] instances.
Expand All @@ -20,6 +21,10 @@ internal interface PinnedSiteDao {
@Insert
fun insertPinnedSite(site: PinnedSiteEntity): Long

@WorkerThread
@Update
fun updatePinnedSite(site: PinnedSiteEntity)

@WorkerThread
@Delete
fun deletePinnedSite(site: PinnedSiteEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class TopSitesUseCasesTest {
)
}

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

val title = "New title"
useCases.renameTopSites(topSite, title)

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

lorenzos marked this conversation as resolved.
Show resolved Hide resolved
@Test
fun `RemoveTopSiteUseCase`() = runBlocking {
val topSitesStorage: TopSitesStorage = mock()
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ permalink: /changelog/
* **feature-downloads**
* 🚒 Bug fixed [issue #8585](https://github.com/mozilla-mobile/android-components/issues/8784) create download directory when it doesn't exists for more information see [mozilla-mobile/fenix#15527](https://github.com/mozilla-mobile/fenix/issues/5829).

* **feature-top-sites**
* Added methods to rename top site entries.
lorenzos marked this conversation as resolved.
Show resolved Hide resolved

# 63.0.0

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