From 74dc3742dd022899cabf9623276ddd0cc2dd724c Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Fri, 25 Jun 2021 11:47:18 +0200 Subject: [PATCH 1/3] Add method to check whether there has been blogging reminders set --- .../fluxc/store/BloggingRemindersStoreTest.kt | 19 +++++++++++++++++-- .../fluxc/persistence/BloggingRemindersDao.kt | 5 ++++- .../fluxc/store/BloggingRemindersStore.kt | 6 +++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/store/BloggingRemindersStoreTest.kt b/example/src/test/java/org/wordpress/android/fluxc/store/BloggingRemindersStoreTest.kt index 0220afac4d..b02d6c070d 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/store/BloggingRemindersStoreTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/store/BloggingRemindersStoreTest.kt @@ -34,7 +34,7 @@ class BloggingRemindersStoreTest { fun `maps items emitted from dao`() = test { val dbEntity = BloggingReminders(siteId, monday = true) val domainModel = BloggingRemindersModel(siteId, setOf(MONDAY)) - whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(flowOf(dbEntity)) + whenever(bloggingRemindersDao.liveGetBySiteId(siteId)).thenReturn(flowOf(dbEntity)) whenever(mapper.toDomainModel(dbEntity)).thenReturn(domainModel) assertThat(store.bloggingRemindersModel(siteId).single()).isEqualTo(domainModel) @@ -42,7 +42,7 @@ class BloggingRemindersStoreTest { @Test fun `maps null value to empty model emitted from dao`() = test { - whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(flowOf(null)) + whenever(bloggingRemindersDao.liveGetBySiteId(siteId)).thenReturn(flowOf(null)) assertThat(store.bloggingRemindersModel(siteId).single()).isEqualTo(BloggingRemindersModel(siteId)) } @@ -57,4 +57,19 @@ class BloggingRemindersStoreTest { verify(bloggingRemindersDao).insert(dbEntity) } + + @Test + fun `has modified blogging reminders when DAO returns data`() = test { + val dbEntity = BloggingReminders(siteId, monday = true) + whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(listOf(dbEntity)) + + assertThat(store.hasModifiedBloggingReminders(siteId)).isTrue() + } + + @Test + fun `does not have modified blogging reminders when DAO returns no data`() = test { + whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(listOf()) + + assertThat(store.hasModifiedBloggingReminders(siteId)).isFalse() + } } diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt index 3d9a69280d..f222d817df 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt @@ -14,7 +14,10 @@ abstract class BloggingRemindersDao { abstract fun getAll(): Flow> @Query("SELECT * FROM BloggingReminders WHERE localSiteId = :siteId") - abstract fun getBySiteId(siteId: Int): Flow + abstract fun liveGetBySiteId(siteId: Int): Flow + + @Query("SELECT * FROM BloggingReminders WHERE localSiteId = :siteId") + abstract fun getBySiteId(siteId: Int): List @Insert(onConflict = OnConflictStrategy.REPLACE) abstract fun insert(type: BloggingReminders): Long diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt index bb758bb8fd..b78a3140d8 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt @@ -18,11 +18,15 @@ class BloggingRemindersStore private val coroutineEngine: CoroutineEngine ) { fun bloggingRemindersModel(siteId: Int): Flow { - return bloggingRemindersDao.getBySiteId(siteId).map { + return bloggingRemindersDao.liveGetBySiteId(siteId).map { it?.let { dbModel -> mapper.toDomainModel(dbModel) } ?: BloggingRemindersModel(siteId) } } + fun hasModifiedBloggingReminders(siteId: Int): Boolean { + return bloggingRemindersDao.getBySiteId(siteId).isNotEmpty() + } + suspend fun updateBloggingReminders(model: BloggingRemindersModel) = coroutineEngine.withDefaultContext(T.SETTINGS, this, "Updating blogging reminders") { bloggingRemindersDao.insert(mapper.toDatabaseModel(model)) From bcd8e19cb5aea5a649e34c51b7918b641a240dab Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Fri, 25 Jun 2021 11:47:32 +0200 Subject: [PATCH 2/3] Add migration file --- .../1.json | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 fluxc/schemas/org.wordpress.android.fluxc.persistence.WPAndroidDatabase/1.json diff --git a/fluxc/schemas/org.wordpress.android.fluxc.persistence.WPAndroidDatabase/1.json b/fluxc/schemas/org.wordpress.android.fluxc.persistence.WPAndroidDatabase/1.json new file mode 100644 index 0000000000..176d9998c2 --- /dev/null +++ b/fluxc/schemas/org.wordpress.android.fluxc.persistence.WPAndroidDatabase/1.json @@ -0,0 +1,76 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "4f29ca40a8cdc7081cbff3a88c7eaa87", + "entities": [ + { + "tableName": "BloggingReminders", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`localSiteId` INTEGER NOT NULL, `monday` INTEGER NOT NULL, `tuesday` INTEGER NOT NULL, `wednesday` INTEGER NOT NULL, `thursday` INTEGER NOT NULL, `friday` INTEGER NOT NULL, `saturday` INTEGER NOT NULL, `sunday` INTEGER NOT NULL, PRIMARY KEY(`localSiteId`))", + "fields": [ + { + "fieldPath": "localSiteId", + "columnName": "localSiteId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "monday", + "columnName": "monday", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tuesday", + "columnName": "tuesday", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "wednesday", + "columnName": "wednesday", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "thursday", + "columnName": "thursday", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "friday", + "columnName": "friday", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "saturday", + "columnName": "saturday", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "sunday", + "columnName": "sunday", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "localSiteId" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4f29ca40a8cdc7081cbff3a88c7eaa87')" + ] + } +} \ No newline at end of file From c9f6c62b355bae081dc36a507e5acf643ef2e843 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Fri, 25 Jun 2021 12:37:41 +0200 Subject: [PATCH 3/3] Make DB call suspend function --- .../android/fluxc/persistence/BloggingRemindersDao.kt | 4 ++-- .../android/fluxc/store/BloggingRemindersStore.kt | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt index f222d817df..ea0b61eee2 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/BloggingRemindersDao.kt @@ -17,10 +17,10 @@ abstract class BloggingRemindersDao { abstract fun liveGetBySiteId(siteId: Int): Flow @Query("SELECT * FROM BloggingReminders WHERE localSiteId = :siteId") - abstract fun getBySiteId(siteId: Int): List + abstract suspend fun getBySiteId(siteId: Int): List @Insert(onConflict = OnConflictStrategy.REPLACE) - abstract fun insert(type: BloggingReminders): Long + abstract suspend fun insert(type: BloggingReminders): Long @Entity(tableName = "BloggingReminders") data class BloggingReminders( diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt index b78a3140d8..be5edebb56 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/BloggingRemindersStore.kt @@ -23,9 +23,10 @@ class BloggingRemindersStore } } - fun hasModifiedBloggingReminders(siteId: Int): Boolean { - return bloggingRemindersDao.getBySiteId(siteId).isNotEmpty() - } + suspend fun hasModifiedBloggingReminders(siteId: Int) = + coroutineEngine.withDefaultContext(T.SETTINGS, this, "Has blogging reminders") { + bloggingRemindersDao.getBySiteId(siteId).isNotEmpty() + } suspend fun updateBloggingReminders(model: BloggingRemindersModel) = coroutineEngine.withDefaultContext(T.SETTINGS, this, "Updating blogging reminders") {