From 4540870257712dff3b1e2b3ec0bc3dea5e6fab3e Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Mon, 26 Apr 2021 19:53:54 +0200 Subject: [PATCH] Rename item state tables --- .../app/repositories/ARepository.java | 10 +-- .../app/repositories/FreshRSSRepository.java | 14 ++-- db/schemas/com.readrops.db.Database/3.json | 8 +- db/src/main/java/com/readrops/db/Database.kt | 6 +- .../java/com/readrops/db/dao/ItemDao.java | 4 +- .../com/readrops/db/dao/ItemStateChangeDao.kt | 46 +++++++++++ .../java/com/readrops/db/dao/ItemStateDao.kt | 39 ++++++++++ .../java/com/readrops/db/dao/ItemsIdsDao.kt | 78 ------------------- .../db/entities/{ItemsIds.kt => ItemState.kt} | 4 +- 9 files changed, 110 insertions(+), 99 deletions(-) create mode 100644 db/src/main/java/com/readrops/db/dao/ItemStateChangeDao.kt create mode 100644 db/src/main/java/com/readrops/db/dao/ItemStateDao.kt delete mode 100644 db/src/main/java/com/readrops/db/dao/ItemsIdsDao.kt rename db/src/main/java/com/readrops/db/entities/{ItemsIds.kt => ItemState.kt} (91%) diff --git a/app/src/main/java/com/readrops/app/repositories/ARepository.java b/app/src/main/java/com/readrops/app/repositories/ARepository.java index c5618d74..3ff351cb 100644 --- a/app/src/main/java/com/readrops/app/repositories/ARepository.java +++ b/app/src/main/java/com/readrops/app/repositories/ARepository.java @@ -17,8 +17,8 @@ import com.readrops.db.Database; import com.readrops.db.entities.Feed; import com.readrops.db.entities.Folder; import com.readrops.db.entities.Item; -import com.readrops.db.entities.ItemStateId; -import com.readrops.db.entities.ReadStarStateChange; +import com.readrops.db.entities.ItemState; +import com.readrops.db.entities.ItemStateChange; import com.readrops.db.entities.account.Account; import org.koin.java.KoinJavaComponent; @@ -116,8 +116,8 @@ public abstract class ARepository { } public Completable setItemReadState(Item item) { - return database.itemsIdsDao().upsertItemReadState(new ItemStateId(0, item.isRead(), item.isStarred(), item.getRemoteId(), account.getId())) - .andThen(database.itemsIdsDao().upsertReadStarStateChange(new ReadStarStateChange(item.getId(), + return database.itemStateDao().upsertItemReadState(new ItemState(0, item.isRead(), item.isStarred(), item.getRemoteId(), account.getId())) + .andThen(database.itemStateChangesDao().upsertItemStateChange(new ItemStateChange(item.getId(), true, false, account.getId()))); } @@ -131,7 +131,7 @@ public abstract class ARepository { public Completable setItemStarState(Item item) { return database.itemDao().setStarState(item.getId(), item.isStarred()) - .andThen(database.itemsIdsDao().upsertReadStarStateChange(new ReadStarStateChange(item.getId(), + .andThen(database.itemStateChangesDao().upsertItemStateChange(new ItemStateChange(item.getId(), false, true, account.getId()))); } diff --git a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java index ad3168c7..a0398ebf 100644 --- a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java @@ -17,7 +17,7 @@ import com.readrops.db.Database; import com.readrops.db.entities.Feed; import com.readrops.db.entities.Folder; import com.readrops.db.entities.Item; -import com.readrops.db.entities.ItemStateId; +import com.readrops.db.entities.ItemState; import com.readrops.db.entities.account.Account; import com.readrops.db.pojo.ItemReadStarState; @@ -93,7 +93,9 @@ public class FreshRSSRepository extends ARepository { TimingLogger logger = new TimingLogger(TAG, "FreshRSS sync timer"); return Single.create(emitter -> { - List itemStateChanges = database.itemsIdsDao().getItemStateChanges(account.getId()); + List itemStateChanges = database + .itemStateChangesDao() + .getItemStateChanges(account.getId()); syncData.setReadItemsIds(itemStateChanges.stream() .filter(it -> it.getReadChange() && it.getRead()) @@ -134,7 +136,7 @@ public class FreshRSSRepository extends ARepository { account.setLastModified(newLastModified); database.accountDao().updateLastModified(account.getId(), newLastModified); - database.itemsIdsDao().resetStateChanges(account.getId()); + database.itemStateChangesDao().resetStateChanges(account.getId()); logger.dumpToLog(); @@ -253,9 +255,9 @@ public class FreshRSSRepository extends ARepository { } private void insertItemsIds(List unreadIds, List starredIds) { - database.itemsIdsDao().deleteItemsIds(account.getId()); - database.itemsIdsDao().insertItemStateId(unreadIds.stream().map(id -> - new ItemStateId(0, false, starredIds.stream() + database.itemStateDao().deleteItemsStates(account.getId()); + database.itemStateDao().insertItemStates(unreadIds.stream().map(id -> + new ItemState(0, false, starredIds.stream() .anyMatch(starredId -> starredId.equals(id)), id, account.getId())) .collect(Collectors.toList())); diff --git a/db/schemas/com.readrops.db.Database/3.json b/db/schemas/com.readrops.db.Database/3.json index 7acfa765..cfd61c7c 100644 --- a/db/schemas/com.readrops.db.Database/3.json +++ b/db/schemas/com.readrops.db.Database/3.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 3, - "identityHash": "276d6f5e56615e86b3634302b7565e95", + "identityHash": "a1f1be38bb62d3d52c1563c7a2472a4c", "entities": [ { "tableName": "Feed", @@ -422,7 +422,7 @@ "foreignKeys": [] }, { - "tableName": "ReadStarStateChange", + "tableName": "ItemStateChange", "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `read_change` INTEGER NOT NULL, `star_change` INTEGER NOT NULL, `account_id` INTEGER NOT NULL, PRIMARY KEY(`id`))", "fields": [ { @@ -460,7 +460,7 @@ "foreignKeys": [] }, { - "tableName": "ItemStateId", + "tableName": "ItemState", "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `read` INTEGER NOT NULL, `starred` INTEGER NOT NULL, `remote_id` TEXT NOT NULL, `account_id` INTEGER NOT NULL)", "fields": [ { @@ -507,7 +507,7 @@ "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, '276d6f5e56615e86b3634302b7565e95')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'a1f1be38bb62d3d52c1563c7a2472a4c')" ] } } \ No newline at end of file diff --git a/db/src/main/java/com/readrops/db/Database.kt b/db/src/main/java/com/readrops/db/Database.kt index fa645b35..16ab3856 100644 --- a/db/src/main/java/com/readrops/db/Database.kt +++ b/db/src/main/java/com/readrops/db/Database.kt @@ -9,7 +9,7 @@ import com.readrops.db.entities.account.Account import dev.matrix.roomigrant.GenerateRoomMigrations @Database(entities = [Feed::class, Item::class, Folder::class, Account::class, - ReadStarStateChange::class, ItemStateId::class], version = 3) + ItemStateChange::class, ItemState::class], version = 3) @TypeConverters(Converters::class) @GenerateRoomMigrations abstract class Database : RoomDatabase() { @@ -21,5 +21,7 @@ abstract class Database : RoomDatabase() { abstract fun accountDao(): AccountDao - abstract fun itemsIdsDao(): ItemsIdsDao + abstract fun itemStateDao(): ItemStateDao + + abstract fun itemStateChangesDao(): ItemStateChangeDao } \ No newline at end of file diff --git a/db/src/main/java/com/readrops/db/dao/ItemDao.java b/db/src/main/java/com/readrops/db/dao/ItemDao.java index acb83f7e..3d1cf353 100644 --- a/db/src/main/java/com/readrops/db/dao/ItemDao.java +++ b/db/src/main/java/com/readrops/db/dao/ItemDao.java @@ -12,7 +12,7 @@ import androidx.sqlite.db.SupportSQLiteQuery; import com.readrops.db.entities.Feed; import com.readrops.db.entities.Folder; import com.readrops.db.entities.Item; -import com.readrops.db.entities.ItemStateId; +import com.readrops.db.entities.ItemState; import com.readrops.db.pojo.ItemWithFeed; import com.readrops.db.pojo.StarItem; @@ -23,7 +23,7 @@ import io.reactivex.Completable; @Dao public interface ItemDao extends BaseDao { - @RawQuery(observedEntities = {Item.class, Folder.class, Feed.class, ItemStateId.class}) + @RawQuery(observedEntities = {Item.class, Folder.class, Feed.class, ItemState.class}) DataSource.Factory selectAll(SupportSQLiteQuery query); @Query("Select * From Item Where id = :itemId") diff --git a/db/src/main/java/com/readrops/db/dao/ItemStateChangeDao.kt b/db/src/main/java/com/readrops/db/dao/ItemStateChangeDao.kt new file mode 100644 index 00000000..4a25de7a --- /dev/null +++ b/db/src/main/java/com/readrops/db/dao/ItemStateChangeDao.kt @@ -0,0 +1,46 @@ +package com.readrops.db.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import com.readrops.db.entities.ItemStateChange +import com.readrops.db.pojo.ItemReadStarState +import io.reactivex.Completable + +@Dao +interface ItemStateChangeDao : BaseDao { + + @Insert + fun insertItemStateChange(itemStateChange: ItemStateChange) + + @Delete + fun deleteItemStateChange(itemStateChange: ItemStateChange) + + @Query("Delete From ItemStateChange Where account_id = :accountId") + fun resetStateChanges(accountId: Int) + + @Query("Select case When ItemState.remote_id is NULL Or ItemState.read = 1 Then 1 else 0 End read, " + + "case When ItemState.remote_id is NULL Or ItemState.starred = 1 Then 1 else 0 End starred," + + "ItemStateChange.read_change, ItemStateChange.star_change, Item.remoteId " + + "From ItemStateChange Inner Join Item On ItemStateChange.id = Item.id " + + "Left Join ItemState On ItemState.remote_id = Item.remoteId Where ItemStateChange.account_id = :accountId") + fun getItemStateChanges(accountId: Int): List + + fun upsertItemStateChange(itemStateChange: ItemStateChange) = Completable.create { + if (itemStateChange.readChange && readStateChangeExists(itemStateChange.id) || + itemStateChange.starChange && starStateChangeExists(itemStateChange.id)) { + deleteItemStateChange(itemStateChange) + } else { + insertItemStateChange(itemStateChange) + } + + it.onComplete() + } + + @Query("Select Case When :itemId In (Select id From ItemStateChange Where read_change = 1) Then 1 Else 0 End") + fun readStateChangeExists(itemId: Int): Boolean + + @Query("Select Case When :itemId In (Select id From ItemStateChange Where star_change = 1) Then 1 Else 0 End") + fun starStateChangeExists(itemId: Int): Boolean +} \ No newline at end of file diff --git a/db/src/main/java/com/readrops/db/dao/ItemStateDao.kt b/db/src/main/java/com/readrops/db/dao/ItemStateDao.kt new file mode 100644 index 00000000..7efa52ef --- /dev/null +++ b/db/src/main/java/com/readrops/db/dao/ItemStateDao.kt @@ -0,0 +1,39 @@ +package com.readrops.db.dao + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.Query +import com.readrops.db.entities.ItemState +import io.reactivex.Completable + +@Dao +interface ItemStateDao : BaseDao { + + @Query("Delete From ItemState Where account_id = :accountId") + fun deleteItemsStates(accountId: Int) + + @Query("Delete From ItemState Where remote_id = :remoteId And account_id = :accountId") + fun deleteItemState(remoteId: String, accountId: Int) + + @Insert + fun insertItemStates(items: List) + + @Insert + fun insertItemState(itemState: ItemState) + + @Query("Update ItemState set read = :read Where remote_id = :remoteId And account_id = :accountId") + fun updateItemReadState(read: Boolean, remoteId: String, accountId: Int) + + @Query("Select case When Exists (Select remote_id, account_id From ItemState Where remote_id = :remoteId And account_id = :accountId) Then 1 else 0 End") + fun itemStateExists(remoteId: String, accountId: Int): Boolean + + fun upsertItemReadState(itemState: ItemState) = Completable.create { + if (itemStateExists(itemState.remoteId, itemState.accountId)) { + updateItemReadState(itemState.read, itemState.remoteId, itemState.accountId) + } else { + insertItemState(itemState) + } + + it.onComplete() + } +} \ No newline at end of file diff --git a/db/src/main/java/com/readrops/db/dao/ItemsIdsDao.kt b/db/src/main/java/com/readrops/db/dao/ItemsIdsDao.kt deleted file mode 100644 index eaa82f3b..00000000 --- a/db/src/main/java/com/readrops/db/dao/ItemsIdsDao.kt +++ /dev/null @@ -1,78 +0,0 @@ -package com.readrops.db.dao - -import androidx.room.Dao -import androidx.room.Delete -import androidx.room.Insert -import androidx.room.Query -import com.readrops.db.entities.ItemStateId -import com.readrops.db.entities.ReadStarStateChange -import com.readrops.db.pojo.ItemReadStarState -import io.reactivex.Completable - -@Dao -interface ItemsIdsDao { - - @Insert - fun insertReadStarStateChange(readStarStateChange: ReadStarStateChange) - - @Delete - fun deleteReadStarStateChange(readStarStateChange: ReadStarStateChange) - - @Query("Delete From ReadStarStateChange Where account_id = :accountId") - fun deleteReadStarStateChanges(accountId: Int) - - @Query("Delete From ReadStarStateChange Where account_id = :accountId") - fun resetStateChanges(accountId: Int) - - @Query("Select case When ItemStateId.remote_id is NULL Or ItemStateId.read = 1 Then 1 else 0 End read, " + - "case When ItemStateId.remote_id is NULL Or ItemStateId.starred = 1 Then 1 else 0 End starred," + - "ReadStarStateChange.read_change, ReadStarStateChange.star_change, Item.remoteId " + - "From ReadStarStateChange Inner Join Item On ReadStarStateChange.id = Item.id " + - "Left Join ItemStateId On ItemStateId.remote_id = Item.remoteId Where ReadStarStateChange.account_id = :accountId") - fun getItemStateChanges(accountId: Int): List - - fun upsertReadStarStateChange(readStarStateChange: ReadStarStateChange) = Completable.create { - if (readStarStateChange.readChange && readStateChangeExists(readStarStateChange.id) || - readStarStateChange.starChange && starStateChangeExists(readStarStateChange.id)) { - deleteReadStarStateChange(readStarStateChange) - } else { - insertReadStarStateChange(readStarStateChange) - } - - it.onComplete() - } - - @Query("Select Case When :itemId In (Select id From ReadStarStateChange Where read_change = 1) Then 1 Else 0 End") - fun readStateChangeExists(itemId: Int): Boolean - - @Query("Select Case When :itemId In (Select id From ReadStarStateChange Where star_change = 1) Then 1 Else 0 End") - fun starStateChangeExists(itemId: Int): Boolean - - @Query("Delete From ItemStateId Where account_id = :accountId") - fun deleteItemsIds(accountId: Int) - - @Query("Delete From ItemStateId Where remote_id = :remoteId And account_id = :accountId") - fun deleteItemStateId(remoteId: String, accountId: Int) - - @Insert - fun insertItemStateId(itemsIds: List) - - @Insert - fun insertItemStateId(itemStateId: ItemStateId) - - @Query("Update ItemStateId set read = :read Where remote_id = :remoteId And account_id = :accountId") - fun updateItemReadState(read: Boolean, remoteId: String, accountId: Int) - - @Query("Select case When Exists (Select remote_id, account_id From ItemStateId Where remote_id = :remoteId And account_id = :accountId) Then 1 else 0 End") - fun itemStateExists(remoteId: String, accountId: Int): Boolean - - fun upsertItemReadState(itemStateId: ItemStateId) = Completable.create { - if (itemStateExists(itemStateId.remoteId, itemStateId.accountId)) { - updateItemReadState(itemStateId.read, itemStateId.remoteId, itemStateId.accountId) - } else { - insertItemStateId(itemStateId) - } - - it.onComplete() - } -} \ No newline at end of file diff --git a/db/src/main/java/com/readrops/db/entities/ItemsIds.kt b/db/src/main/java/com/readrops/db/entities/ItemState.kt similarity index 91% rename from db/src/main/java/com/readrops/db/entities/ItemsIds.kt rename to db/src/main/java/com/readrops/db/entities/ItemState.kt index 4ba00a4c..96f719a9 100644 --- a/db/src/main/java/com/readrops/db/entities/ItemsIds.kt +++ b/db/src/main/java/com/readrops/db/entities/ItemState.kt @@ -5,7 +5,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey @Entity -data class ReadStarStateChange( +data class ItemStateChange( @PrimaryKey val id: Int = 0, @ColumnInfo(name = "read_change") val readChange: Boolean = false, @ColumnInfo(name = "star_change") val starChange: Boolean = false, @@ -13,7 +13,7 @@ data class ReadStarStateChange( ) @Entity -data class ItemStateId( +data class ItemState( @PrimaryKey(autoGenerate = true) val id: Int = 0, val read: Boolean = false, val starred: Boolean = false,