Loading components/feature/top-sites/src/androidTest/java/mozilla/components/feature/top/sites/PinnedSitesStorageTest.kt +10 −10 Original line number Diff line number Diff line Loading @@ -63,8 +63,8 @@ class PinnedSitesStorageTest { @Test fun testAddingTopSite() { storage.addTopSite("Mozilla", "https://www.mozilla.org") storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true) storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true) val topSites = getAllTopSites() Loading @@ -80,13 +80,13 @@ class PinnedSitesStorageTest { @Test fun testRemovingTopSites() { storage.addTopSite("Mozilla", "https://www.mozilla.org") storage.addTopSite("Firefox", "https://www.firefox.com") storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Firefox", "https://www.firefox.com") getAllTopSites().let { topSites -> assertEquals(2, topSites.size) storage.removeTopSite(topSites[0]) storage.removePinnedSite(topSites[0]) } getAllTopSites().let { topSites -> Loading @@ -99,10 +99,10 @@ class PinnedSitesStorageTest { @Test fun testGettingTopSites() = runBlocking { storage.addTopSite("Mozilla", "https://www.mozilla.org") storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true) storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true) val topSites = storage.getTopSites().first() val topSites = storage.getPinnedSites().first() assertNotNull(topSites) assertEquals(2, topSites.size) Loading Loading @@ -247,8 +247,8 @@ class PinnedSitesStorageTest { } } private fun getAllTopSites(): List<TopSite> { val dataSource = storage.getTopSitesPaged().create() private fun getAllTopSites(): List<PinnedSite> { val dataSource = storage.getPinnedSitesPaged().create() val pagedList = PagedList.Builder(dataSource, 10) .setNotifyExecutor(executor) Loading components/feature/top-sites/src/androidTest/java/mozilla/components/feature/top/sites/db/TopSiteDaoTest.kt→components/feature/top-sites/src/androidTest/java/mozilla/components/feature/top/sites/db/PinnedSiteDaoTest.kt +9 −9 Original line number Diff line number Diff line Loading @@ -17,12 +17,12 @@ import org.junit.Test import java.util.concurrent.ExecutorService import java.util.concurrent.Executors class TopSiteDaoTest { class PinnedSiteDaoTest { private val context: Context get() = ApplicationProvider.getApplicationContext() private lateinit var database: TopSiteDatabase private lateinit var topSiteDao: TopSiteDao private lateinit var pinnedSiteDao: PinnedSiteDao private lateinit var executor: ExecutorService @get:Rule Loading @@ -31,7 +31,7 @@ class TopSiteDaoTest { @Before fun setUp() { database = Room.inMemoryDatabaseBuilder(context, TopSiteDatabase::class.java).build() topSiteDao = database.topSiteDao() pinnedSiteDao = database.pinnedSiteDao() executor = Executors.newSingleThreadExecutor() } Loading @@ -49,10 +49,10 @@ class TopSiteDaoTest { isDefault = false, createdAt = 200 ).also { it.id = topSiteDao.insertTopSite(it) it.id = pinnedSiteDao.insertPinnedSite(it) } val dataSource = topSiteDao.getTopSitesPaged().create() val dataSource = pinnedSiteDao.getPinnedSitesPaged().create() val pagedList = PagedList.Builder(dataSource, 10) .setNotifyExecutor(executor) Loading @@ -71,7 +71,7 @@ class TopSiteDaoTest { isDefault = false, createdAt = 200 ).also { it.id = topSiteDao.insertTopSite(it) it.id = pinnedSiteDao.insertPinnedSite(it) } val topSite2 = PinnedSiteEntity( Loading @@ -80,12 +80,12 @@ class TopSiteDaoTest { isDefault = false, createdAt = 100 ).also { it.id = topSiteDao.insertTopSite(it) it.id = pinnedSiteDao.insertPinnedSite(it) } topSiteDao.deleteTopSite(topSite1) pinnedSiteDao.deletePinnedSite(topSite1) val dataSource = topSiteDao.getTopSitesPaged().create() val dataSource = pinnedSiteDao.getPinnedSitesPaged().create() val pagedList = PagedList.Builder(dataSource, 10) .setNotifyExecutor(executor) Loading components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/TopSite.kt→components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/PinnedSite.kt +6 −6 Original line number Diff line number Diff line Loading @@ -5,26 +5,26 @@ package mozilla.components.feature.top.sites /** * A top site. * A pinned site. */ interface TopSite { interface PinnedSite { /** * Unique ID of this top site. * Unique ID of this pinned site. */ val id: Long /** * The title of the top site. * The title of the pinned site. */ val title: String /** * The URL of the top site. * The URL of the pinned site. */ val url: String /** * Whether or not the top site is a default top site (added as a default by the application). * Whether or not the pinned site is a default pinned site (added as a default by the application). */ val isDefault: Boolean } components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/PinnedSiteStorage.kt +20 −20 Original line number Diff line number Diff line Loading @@ -8,12 +8,12 @@ import android.content.Context import androidx.paging.DataSource import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import mozilla.components.feature.top.sites.adapter.TopSiteAdapter import mozilla.components.feature.top.sites.adapter.PinnedSiteAdapter import mozilla.components.feature.top.sites.db.TopSiteDatabase import mozilla.components.feature.top.sites.db.PinnedSiteEntity /** * A storage implementation for organizing top sites. * A storage implementation for organizing pinned sites. */ class PinnedSiteStorage( context: Context Loading @@ -21,46 +21,46 @@ class PinnedSiteStorage( internal var database: Lazy<TopSiteDatabase> = lazy { TopSiteDatabase.get(context) } /** * Adds a new [TopSite]. * Adds a new [PinnedSite]. * * @param title The title string. * @param url The URL string. * @param isDefault Whether or not the top site added should be a default top site. This is * used to identify top sites that are added by the application. * @param isDefault Whether or not the pinned site added should be a default pinned site. This * is used to identify pinned sites that are added by the application. */ fun addTopSite(title: String, url: String, isDefault: Boolean = false) { fun addPinnedSite(title: String, url: String, isDefault: Boolean = false) { PinnedSiteEntity( title = title, url = url, isDefault = isDefault, createdAt = System.currentTimeMillis() ).also { entity -> entity.id = database.value.topSiteDao().insertTopSite(entity) entity.id = database.value.pinnedSiteDao().insertPinnedSite(entity) } } /** * Returns a [Flow] list of all the [TopSite] instances. * Returns a [Flow] list of all the [PinnedSite] instances. */ fun getTopSites(): Flow<List<TopSite>> { return database.value.topSiteDao().getTopSites().map { list -> list.map { entity -> TopSiteAdapter(entity) } fun getPinnedSites(): Flow<List<PinnedSite>> { return database.value.pinnedSiteDao().getPinnedSites().map { list -> list.map { entity -> PinnedSiteAdapter(entity) } } } /** * Returns all [TopSite]s as a [DataSource.Factory]. * Returns all [PinnedSite]s as a [DataSource.Factory]. */ fun getTopSitesPaged(): DataSource.Factory<Int, TopSite> = database.value .topSiteDao() .getTopSitesPaged() .map { entity -> TopSiteAdapter(entity) } fun getPinnedSitesPaged(): DataSource.Factory<Int, PinnedSite> = database.value .pinnedSiteDao() .getPinnedSitesPaged() .map { entity -> PinnedSiteAdapter(entity) } /** * Removes the given [TopSite]. * Removes the given [PinnedSite]. */ fun removeTopSite(site: TopSite) { val topSiteEntity = (site as TopSiteAdapter).entity database.value.topSiteDao().deleteTopSite(topSiteEntity) fun removePinnedSite(site: PinnedSite) { val pinnedSiteEntity = (site as PinnedSiteAdapter).entity database.value.pinnedSiteDao().deletePinnedSite(pinnedSiteEntity) } } components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/adapter/TopSiteAdapter.kt→components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/adapter/PinnedSiteAdapter.kt +4 −4 Original line number Diff line number Diff line Loading @@ -4,12 +4,12 @@ package mozilla.components.feature.top.sites.adapter import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.PinnedSite import mozilla.components.feature.top.sites.db.PinnedSiteEntity internal class TopSiteAdapter( internal class PinnedSiteAdapter( internal val entity: PinnedSiteEntity ) : TopSite { ) : PinnedSite { override val id: Long get() = entity.id!! Loading @@ -23,7 +23,7 @@ internal class TopSiteAdapter( get() = entity.isDefault override fun equals(other: Any?): Boolean { if (other !is TopSiteAdapter) { if (other !is PinnedSiteAdapter) { return false } Loading Loading
components/feature/top-sites/src/androidTest/java/mozilla/components/feature/top/sites/PinnedSitesStorageTest.kt +10 −10 Original line number Diff line number Diff line Loading @@ -63,8 +63,8 @@ class PinnedSitesStorageTest { @Test fun testAddingTopSite() { storage.addTopSite("Mozilla", "https://www.mozilla.org") storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true) storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true) val topSites = getAllTopSites() Loading @@ -80,13 +80,13 @@ class PinnedSitesStorageTest { @Test fun testRemovingTopSites() { storage.addTopSite("Mozilla", "https://www.mozilla.org") storage.addTopSite("Firefox", "https://www.firefox.com") storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Firefox", "https://www.firefox.com") getAllTopSites().let { topSites -> assertEquals(2, topSites.size) storage.removeTopSite(topSites[0]) storage.removePinnedSite(topSites[0]) } getAllTopSites().let { topSites -> Loading @@ -99,10 +99,10 @@ class PinnedSitesStorageTest { @Test fun testGettingTopSites() = runBlocking { storage.addTopSite("Mozilla", "https://www.mozilla.org") storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true) storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true) val topSites = storage.getTopSites().first() val topSites = storage.getPinnedSites().first() assertNotNull(topSites) assertEquals(2, topSites.size) Loading Loading @@ -247,8 +247,8 @@ class PinnedSitesStorageTest { } } private fun getAllTopSites(): List<TopSite> { val dataSource = storage.getTopSitesPaged().create() private fun getAllTopSites(): List<PinnedSite> { val dataSource = storage.getPinnedSitesPaged().create() val pagedList = PagedList.Builder(dataSource, 10) .setNotifyExecutor(executor) Loading
components/feature/top-sites/src/androidTest/java/mozilla/components/feature/top/sites/db/TopSiteDaoTest.kt→components/feature/top-sites/src/androidTest/java/mozilla/components/feature/top/sites/db/PinnedSiteDaoTest.kt +9 −9 Original line number Diff line number Diff line Loading @@ -17,12 +17,12 @@ import org.junit.Test import java.util.concurrent.ExecutorService import java.util.concurrent.Executors class TopSiteDaoTest { class PinnedSiteDaoTest { private val context: Context get() = ApplicationProvider.getApplicationContext() private lateinit var database: TopSiteDatabase private lateinit var topSiteDao: TopSiteDao private lateinit var pinnedSiteDao: PinnedSiteDao private lateinit var executor: ExecutorService @get:Rule Loading @@ -31,7 +31,7 @@ class TopSiteDaoTest { @Before fun setUp() { database = Room.inMemoryDatabaseBuilder(context, TopSiteDatabase::class.java).build() topSiteDao = database.topSiteDao() pinnedSiteDao = database.pinnedSiteDao() executor = Executors.newSingleThreadExecutor() } Loading @@ -49,10 +49,10 @@ class TopSiteDaoTest { isDefault = false, createdAt = 200 ).also { it.id = topSiteDao.insertTopSite(it) it.id = pinnedSiteDao.insertPinnedSite(it) } val dataSource = topSiteDao.getTopSitesPaged().create() val dataSource = pinnedSiteDao.getPinnedSitesPaged().create() val pagedList = PagedList.Builder(dataSource, 10) .setNotifyExecutor(executor) Loading @@ -71,7 +71,7 @@ class TopSiteDaoTest { isDefault = false, createdAt = 200 ).also { it.id = topSiteDao.insertTopSite(it) it.id = pinnedSiteDao.insertPinnedSite(it) } val topSite2 = PinnedSiteEntity( Loading @@ -80,12 +80,12 @@ class TopSiteDaoTest { isDefault = false, createdAt = 100 ).also { it.id = topSiteDao.insertTopSite(it) it.id = pinnedSiteDao.insertPinnedSite(it) } topSiteDao.deleteTopSite(topSite1) pinnedSiteDao.deletePinnedSite(topSite1) val dataSource = topSiteDao.getTopSitesPaged().create() val dataSource = pinnedSiteDao.getPinnedSitesPaged().create() val pagedList = PagedList.Builder(dataSource, 10) .setNotifyExecutor(executor) Loading
components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/TopSite.kt→components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/PinnedSite.kt +6 −6 Original line number Diff line number Diff line Loading @@ -5,26 +5,26 @@ package mozilla.components.feature.top.sites /** * A top site. * A pinned site. */ interface TopSite { interface PinnedSite { /** * Unique ID of this top site. * Unique ID of this pinned site. */ val id: Long /** * The title of the top site. * The title of the pinned site. */ val title: String /** * The URL of the top site. * The URL of the pinned site. */ val url: String /** * Whether or not the top site is a default top site (added as a default by the application). * Whether or not the pinned site is a default pinned site (added as a default by the application). */ val isDefault: Boolean }
components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/PinnedSiteStorage.kt +20 −20 Original line number Diff line number Diff line Loading @@ -8,12 +8,12 @@ import android.content.Context import androidx.paging.DataSource import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import mozilla.components.feature.top.sites.adapter.TopSiteAdapter import mozilla.components.feature.top.sites.adapter.PinnedSiteAdapter import mozilla.components.feature.top.sites.db.TopSiteDatabase import mozilla.components.feature.top.sites.db.PinnedSiteEntity /** * A storage implementation for organizing top sites. * A storage implementation for organizing pinned sites. */ class PinnedSiteStorage( context: Context Loading @@ -21,46 +21,46 @@ class PinnedSiteStorage( internal var database: Lazy<TopSiteDatabase> = lazy { TopSiteDatabase.get(context) } /** * Adds a new [TopSite]. * Adds a new [PinnedSite]. * * @param title The title string. * @param url The URL string. * @param isDefault Whether or not the top site added should be a default top site. This is * used to identify top sites that are added by the application. * @param isDefault Whether or not the pinned site added should be a default pinned site. This * is used to identify pinned sites that are added by the application. */ fun addTopSite(title: String, url: String, isDefault: Boolean = false) { fun addPinnedSite(title: String, url: String, isDefault: Boolean = false) { PinnedSiteEntity( title = title, url = url, isDefault = isDefault, createdAt = System.currentTimeMillis() ).also { entity -> entity.id = database.value.topSiteDao().insertTopSite(entity) entity.id = database.value.pinnedSiteDao().insertPinnedSite(entity) } } /** * Returns a [Flow] list of all the [TopSite] instances. * Returns a [Flow] list of all the [PinnedSite] instances. */ fun getTopSites(): Flow<List<TopSite>> { return database.value.topSiteDao().getTopSites().map { list -> list.map { entity -> TopSiteAdapter(entity) } fun getPinnedSites(): Flow<List<PinnedSite>> { return database.value.pinnedSiteDao().getPinnedSites().map { list -> list.map { entity -> PinnedSiteAdapter(entity) } } } /** * Returns all [TopSite]s as a [DataSource.Factory]. * Returns all [PinnedSite]s as a [DataSource.Factory]. */ fun getTopSitesPaged(): DataSource.Factory<Int, TopSite> = database.value .topSiteDao() .getTopSitesPaged() .map { entity -> TopSiteAdapter(entity) } fun getPinnedSitesPaged(): DataSource.Factory<Int, PinnedSite> = database.value .pinnedSiteDao() .getPinnedSitesPaged() .map { entity -> PinnedSiteAdapter(entity) } /** * Removes the given [TopSite]. * Removes the given [PinnedSite]. */ fun removeTopSite(site: TopSite) { val topSiteEntity = (site as TopSiteAdapter).entity database.value.topSiteDao().deleteTopSite(topSiteEntity) fun removePinnedSite(site: PinnedSite) { val pinnedSiteEntity = (site as PinnedSiteAdapter).entity database.value.pinnedSiteDao().deletePinnedSite(pinnedSiteEntity) } }
components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/adapter/TopSiteAdapter.kt→components/feature/top-sites/src/main/java/mozilla/components/feature/top/sites/adapter/PinnedSiteAdapter.kt +4 −4 Original line number Diff line number Diff line Loading @@ -4,12 +4,12 @@ package mozilla.components.feature.top.sites.adapter import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.PinnedSite import mozilla.components.feature.top.sites.db.PinnedSiteEntity internal class TopSiteAdapter( internal class PinnedSiteAdapter( internal val entity: PinnedSiteEntity ) : TopSite { ) : PinnedSite { override val id: Long get() = entity.id!! Loading @@ -23,7 +23,7 @@ internal class TopSiteAdapter( get() = entity.isDefault override fun equals(other: Any?): Boolean { if (other !is TopSiteAdapter) { if (other !is PinnedSiteAdapter) { return false } Loading