Use Room rxjava integration

This commit is contained in:
Shinokuni 2019-08-22 21:19:50 +02:00
parent c9ae3a5cb5
commit 50de656f35
7 changed files with 32 additions and 48 deletions

View File

@ -51,8 +51,11 @@ dependencies {
}
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.room:room-runtime:2.1.0'
annotationProcessor 'androidx.room:room-compiler:2.1.0'
implementation 'android.arch.persistence.room:rxjava2:1.1.1'
implementation 'androidx.paging:paging-runtime:2.1.0'
implementation 'androidx.paging:paging-common:2.1.0'

View File

@ -8,6 +8,8 @@ import com.readrops.app.database.entities.account.Account;
import java.util.List;
import io.reactivex.Single;
@Dao
public abstract class AccountDao implements BaseDao<Account> {
@ -27,7 +29,7 @@ public abstract class AccountDao implements BaseDao<Account> {
public abstract Integer getAccountCountByType(int accountType);
@Query("Select count(*) From Account")
public abstract Integer getAccountCount();
public abstract Single<Integer> getAccountCount();
@Query("Update Account set writeToken = :writeToken Where id = :accountId")
public abstract void updateWriteToken(int accountId, String writeToken);

View File

@ -6,23 +6,25 @@ import androidx.room.Update;
import java.util.List;
import io.reactivex.Completable;
public interface BaseDao<T> {
@Insert
long insert(T entity);
long insert(T entity); // can't turn return type to Single<Long> while some repositories can't use rxjava properly
@Insert
List<Long> insert(List<T> entities);
@Update
void update(T entity);
Completable update(T entity);
@Update
void update(List<T> entities);
Completable update(List<T> entities);
@Delete
void delete(T entity);
Completable delete(T entity);
@Delete
void delete(List<T> entities);
Completable delete(List<T> entities);
}

View File

@ -13,15 +13,14 @@ import com.readrops.app.database.pojo.FeedWithFolder;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Single;
@Dao
public abstract class FeedDao implements BaseDao<Feed> {
@Query("Select * from Feed Where account_id = :accountId order by name ASC")
public abstract List<Feed> getAllFeeds(int accountId);
@Query("Delete From Feed Where id = :feedId")
public abstract void delete(int feedId);
@Query("Select case When :feedUrl In (Select url from Feed Where account_id = :accountId) Then 1 else 0 end")
public abstract boolean feedExists(String feedUrl, int accountId);
@ -29,7 +28,7 @@ public abstract class FeedDao implements BaseDao<Feed> {
public abstract boolean remoteFeedExists(String remoteId, int accountId);
@Query("Select count(*) from Feed Where account_id = :accountId")
public abstract int getFeedCount(int accountId);
public abstract Single<Integer> getFeedCount(int accountId);
@Query("Select * from Feed Where url = :feedUrl")
public abstract Feed getFeedByUrl(String feedUrl);

View File

@ -15,6 +15,8 @@ import com.readrops.app.database.pojo.ItemWithFeed;
import java.util.List;
import io.reactivex.Completable;
@Dao
public abstract class ItemDao implements BaseDao<Item> {
@ -35,13 +37,13 @@ public abstract class ItemDao implements BaseDao<Item> {
* @param readChanged
*/
@Query("Update Item Set read_changed = :readChanged, read = :readState Where id = :itemId")
public abstract void setReadState(int itemId, int readState, int readChanged);
public abstract Completable setReadState(int itemId, int readState, int readChanged);
@Query("Update Item set read_changed = 1, read = :readState Where feed_id In (Select id From Feed Where account_id = :accountId)")
public abstract void setAllItemsReadState(int readState, int accountId);
public abstract Completable setAllItemsReadState(int readState, int accountId);
@Query("Update Item set read_changed = 1, read = :readState Where feed_id = :feedId")
public abstract void setAllFeedItemsReadState(int feedId, int readState);
public abstract Completable setAllFeedItemsReadState(int feedId, int readState);
@Query("Update Item set read_it_later = 1 Where id = :itemId")
public abstract void setReadItLater(int itemId);

View File

@ -51,10 +51,7 @@ public abstract class ARepository {
}
public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().delete(feed.getId());
emitter.onComplete();
});
return database.feedDao().delete(feed);
}
public Completable addFolder(Folder folder) {
@ -65,42 +62,28 @@ public abstract class ARepository {
}
public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().update(folder);
emitter.onComplete();
});
return database.folderDao().update(folder);
}
public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().delete(folder);
emitter.onComplete();
});
return database.folderDao().delete(folder);
}
public Completable setItemReadState(Item item, boolean read) {
return Completable.create(emitter -> {
database.itemDao().setReadState(item.getId(), read ? 1 : 0, !item.isReadChanged() ? 1 : 0);
emitter.onComplete();
});
return database.itemDao().setReadState(item.getId(), read ? 1 : 0, !item.isReadChanged() ? 1 : 0);
}
public Completable setAllItemsReadState(Boolean read) {
return Completable.create(emitter -> {
database.itemDao().setAllItemsReadState(read ? 1 : 0, account.getId());
emitter.onComplete();
});
return database.itemDao().setAllItemsReadState(read ? 1 : 0, account.getId());
}
public Completable setAllFeedItemsReadState(int feedId, boolean read) {
return Completable.create(emitter -> {
database.itemDao().setAllFeedItemsReadState(feedId, read ? 1 : 0);
emitter.onComplete();
});
return database.itemDao().setAllFeedItemsReadState(feedId, read ? 1 : 0);
}
public Single<Integer> getFeedCount(int accountId) {
return Single.create(emitter -> emitter.onSuccess(database.feedDao().getFeedCount(accountId)));
return database.feedDao().getFeedCount(accountId);
}
protected void setFaviconUtils(List<Feed> feeds) {

View File

@ -37,24 +37,17 @@ public class AccountViewModel extends AndroidViewModel {
long id = database.accountDao().insert(account);
emitter.onSuccess(id);
});
}
public Completable update(Account account) {
return Completable.create(emitter -> {
database.accountDao().update(account);
emitter.onComplete();
});
return database.accountDao().update(account);
}
public Completable delete(Account account) {
return Completable.create(emitter -> {
database.accountDao().delete(account);
emitter.onComplete();
});
return database.accountDao().delete(account);
}
public Single<Integer> getAccountCount() {
return Single.create(emitter -> emitter.onSuccess(database.accountDao().getAccountCount()));
return database.accountDao().getAccountCount();
}
}