Rename item state tables

This commit is contained in:
Shinokuni 2021-04-26 19:53:54 +02:00
parent 750bf10902
commit 4540870257
9 changed files with 110 additions and 99 deletions

View File

@ -17,8 +17,8 @@ import com.readrops.db.Database;
import com.readrops.db.entities.Feed; import com.readrops.db.entities.Feed;
import com.readrops.db.entities.Folder; import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item; import com.readrops.db.entities.Item;
import com.readrops.db.entities.ItemStateId; import com.readrops.db.entities.ItemState;
import com.readrops.db.entities.ReadStarStateChange; import com.readrops.db.entities.ItemStateChange;
import com.readrops.db.entities.account.Account; import com.readrops.db.entities.account.Account;
import org.koin.java.KoinJavaComponent; import org.koin.java.KoinJavaComponent;
@ -116,8 +116,8 @@ public abstract class ARepository {
} }
public Completable setItemReadState(Item item) { public Completable setItemReadState(Item item) {
return database.itemsIdsDao().upsertItemReadState(new ItemStateId(0, item.isRead(), item.isStarred(), item.getRemoteId(), account.getId())) return database.itemStateDao().upsertItemReadState(new ItemState(0, item.isRead(), item.isStarred(), item.getRemoteId(), account.getId()))
.andThen(database.itemsIdsDao().upsertReadStarStateChange(new ReadStarStateChange(item.getId(), .andThen(database.itemStateChangesDao().upsertItemStateChange(new ItemStateChange(item.getId(),
true, false, account.getId()))); true, false, account.getId())));
} }
@ -131,7 +131,7 @@ public abstract class ARepository {
public Completable setItemStarState(Item item) { public Completable setItemStarState(Item item) {
return database.itemDao().setStarState(item.getId(), item.isStarred()) 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()))); false, true, account.getId())));
} }

View File

@ -17,7 +17,7 @@ import com.readrops.db.Database;
import com.readrops.db.entities.Feed; import com.readrops.db.entities.Feed;
import com.readrops.db.entities.Folder; import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item; 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.entities.account.Account;
import com.readrops.db.pojo.ItemReadStarState; import com.readrops.db.pojo.ItemReadStarState;
@ -93,7 +93,9 @@ public class FreshRSSRepository extends ARepository {
TimingLogger logger = new TimingLogger(TAG, "FreshRSS sync timer"); TimingLogger logger = new TimingLogger(TAG, "FreshRSS sync timer");
return Single.<FreshRSSSyncData>create(emitter -> { return Single.<FreshRSSSyncData>create(emitter -> {
List<ItemReadStarState> itemStateChanges = database.itemsIdsDao().getItemStateChanges(account.getId()); List<ItemReadStarState> itemStateChanges = database
.itemStateChangesDao()
.getItemStateChanges(account.getId());
syncData.setReadItemsIds(itemStateChanges.stream() syncData.setReadItemsIds(itemStateChanges.stream()
.filter(it -> it.getReadChange() && it.getRead()) .filter(it -> it.getReadChange() && it.getRead())
@ -134,7 +136,7 @@ public class FreshRSSRepository extends ARepository {
account.setLastModified(newLastModified); account.setLastModified(newLastModified);
database.accountDao().updateLastModified(account.getId(), newLastModified); database.accountDao().updateLastModified(account.getId(), newLastModified);
database.itemsIdsDao().resetStateChanges(account.getId()); database.itemStateChangesDao().resetStateChanges(account.getId());
logger.dumpToLog(); logger.dumpToLog();
@ -253,9 +255,9 @@ public class FreshRSSRepository extends ARepository {
} }
private void insertItemsIds(List<String> unreadIds, List<String> starredIds) { private void insertItemsIds(List<String> unreadIds, List<String> starredIds) {
database.itemsIdsDao().deleteItemsIds(account.getId()); database.itemStateDao().deleteItemsStates(account.getId());
database.itemsIdsDao().insertItemStateId(unreadIds.stream().map(id -> database.itemStateDao().insertItemStates(unreadIds.stream().map(id ->
new ItemStateId(0, false, starredIds.stream() new ItemState(0, false, starredIds.stream()
.anyMatch(starredId -> starredId.equals(id)), id, account.getId())) .anyMatch(starredId -> starredId.equals(id)), id, account.getId()))
.collect(Collectors.toList())); .collect(Collectors.toList()));

View File

@ -2,7 +2,7 @@
"formatVersion": 1, "formatVersion": 1,
"database": { "database": {
"version": 3, "version": 3,
"identityHash": "276d6f5e56615e86b3634302b7565e95", "identityHash": "a1f1be38bb62d3d52c1563c7a2472a4c",
"entities": [ "entities": [
{ {
"tableName": "Feed", "tableName": "Feed",
@ -422,7 +422,7 @@
"foreignKeys": [] "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`))", "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": [ "fields": [
{ {
@ -460,7 +460,7 @@
"foreignKeys": [] "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)", "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": [ "fields": [
{ {
@ -507,7 +507,7 @@
"views": [], "views": [],
"setupQueries": [ "setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", "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')"
] ]
} }
} }

View File

@ -9,7 +9,7 @@ import com.readrops.db.entities.account.Account
import dev.matrix.roomigrant.GenerateRoomMigrations import dev.matrix.roomigrant.GenerateRoomMigrations
@Database(entities = [Feed::class, Item::class, Folder::class, Account::class, @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) @TypeConverters(Converters::class)
@GenerateRoomMigrations @GenerateRoomMigrations
abstract class Database : RoomDatabase() { abstract class Database : RoomDatabase() {
@ -21,5 +21,7 @@ abstract class Database : RoomDatabase() {
abstract fun accountDao(): AccountDao abstract fun accountDao(): AccountDao
abstract fun itemsIdsDao(): ItemsIdsDao abstract fun itemStateDao(): ItemStateDao
abstract fun itemStateChangesDao(): ItemStateChangeDao
} }

View File

@ -12,7 +12,7 @@ import androidx.sqlite.db.SupportSQLiteQuery;
import com.readrops.db.entities.Feed; import com.readrops.db.entities.Feed;
import com.readrops.db.entities.Folder; import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item; 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.ItemWithFeed;
import com.readrops.db.pojo.StarItem; import com.readrops.db.pojo.StarItem;
@ -23,7 +23,7 @@ import io.reactivex.Completable;
@Dao @Dao
public interface ItemDao extends BaseDao<Item> { public interface ItemDao extends BaseDao<Item> {
@RawQuery(observedEntities = {Item.class, Folder.class, Feed.class, ItemStateId.class}) @RawQuery(observedEntities = {Item.class, Folder.class, Feed.class, ItemState.class})
DataSource.Factory<Integer, ItemWithFeed> selectAll(SupportSQLiteQuery query); DataSource.Factory<Integer, ItemWithFeed> selectAll(SupportSQLiteQuery query);
@Query("Select * From Item Where id = :itemId") @Query("Select * From Item Where id = :itemId")

View File

@ -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<ItemStateChange> {
@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<ItemReadStarState>
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
}

View File

@ -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<ItemState> {
@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<ItemState>)
@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()
}
}

View File

@ -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<ItemReadStarState>
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<ItemStateId>)
@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()
}
}

View File

@ -5,7 +5,7 @@ import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@Entity @Entity
data class ReadStarStateChange( data class ItemStateChange(
@PrimaryKey val id: Int = 0, @PrimaryKey val id: Int = 0,
@ColumnInfo(name = "read_change") val readChange: Boolean = false, @ColumnInfo(name = "read_change") val readChange: Boolean = false,
@ColumnInfo(name = "star_change") val starChange: Boolean = false, @ColumnInfo(name = "star_change") val starChange: Boolean = false,
@ -13,7 +13,7 @@ data class ReadStarStateChange(
) )
@Entity @Entity
data class ItemStateId( data class ItemState(
@PrimaryKey(autoGenerate = true) val id: Int = 0, @PrimaryKey(autoGenerate = true) val id: Int = 0,
val read: Boolean = false, val read: Boolean = false,
val starred: Boolean = false, val starred: Boolean = false,