Refactor repositories to put item read state request in it

Put account variable needed for most request directly in the constructor instead in each method as a variable. Put item read state request code in the repository instead of the viewModel
This commit is contained in:
Shinokuni 2019-08-17 12:23:06 +02:00
parent 75e9323177
commit 5eb4353f8a
9 changed files with 88 additions and 85 deletions

View File

@ -532,7 +532,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
if (viewModel.getCurrentAccount().getPassword() == null)
account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey()));
viewModel.sync(feeds, account)
viewModel.sync(feeds)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Feed>() {

View File

@ -4,12 +4,15 @@ import android.app.Application;
import android.graphics.Bitmap;
import android.util.Patterns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.palette.graphics.Palette;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.entities.Item;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ParsingResult;
@ -26,26 +29,35 @@ import io.reactivex.schedulers.Schedulers;
public abstract class ARepository {
protected Database database;
protected Account account;
protected ARepository(Application application) {
protected ARepository(@NonNull Application application, @Nullable Account account) {
this.database = Database.getInstance(application);
this.account = account;
}
public abstract Single<Boolean> login(Account account, boolean insert);
public abstract Observable<Feed> sync(List<Feed> feeds, Account account);
public abstract Observable<Feed> sync(List<Feed> feeds);
public abstract Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account);
public abstract Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results);
public abstract Completable updateFeed(Feed feed, Account account);
public abstract Completable updateFeed(Feed feed);
public abstract Completable deleteFeed(Feed feed, Account account);
public abstract Completable deleteFeed(Feed feed);
public abstract Completable addFolder(Folder folder, Account account);
public abstract Completable addFolder(Folder folder);
public abstract Completable updateFolder(Folder folder, Account account);
public abstract Completable updateFolder(Folder folder);
public abstract Completable deleteFolder(Folder folder, Account account);
public abstract Completable deleteFolder(Folder 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();
});
}
public Single<Integer> getFeedCount(int accountId) {
return Single.create(emitter -> emitter.onSuccess(database.feedDao().getFeedCount(accountId)));

View File

@ -2,11 +2,13 @@ package com.readrops.app.repositories;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.app.database.entities.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.entities.Item;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.FeedMatcher;
import com.readrops.app.utils.ItemMatcher;
@ -32,8 +34,8 @@ import io.reactivex.Single;
public class FreshRSSRepository extends ARepository {
public FreshRSSRepository(Application application) {
super(application);
public FreshRSSRepository(@NonNull Application application, @Nullable Account account) {
super(application, account);
}
@Override
@ -58,7 +60,7 @@ public class FreshRSSRepository extends ARepository {
}
@Override
public Observable<Feed> sync(List<Feed> feeds, Account account) {
public Observable<Feed> sync(List<Feed> feeds) {
FreshRSSAPI api = new FreshRSSAPI(account.toFreshRSSCredentials());
FreshRSSSyncData syncData = new FreshRSSSyncData();
@ -84,47 +86,46 @@ public class FreshRSSRepository extends ARepository {
}
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account) {
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return null;
}
@Override
public Completable updateFeed(Feed feed, Account account) {
public Completable updateFeed(Feed feed) {
return null;
}
@Override
public Completable deleteFeed(Feed feed, Account account) {
public Completable deleteFeed(Feed feed) {
return null;
}
@Override
public Completable addFolder(Folder folder, Account account) {
public Completable addFolder(Folder folder) {
return null;
}
@Override
public Completable updateFolder(Folder folder, Account account) {
public Completable updateFolder(Folder folder) {
return null;
}
@Override
public Completable deleteFolder(Folder folder, Account account) {
public Completable deleteFolder(Folder folder) {
return null;
}
public Completable markItemReadUnread(ItemWithFeed itemWithFeed, Boolean read, Account account) {
@Override
public Completable setItemReadState(Item item, boolean read) {
FreshRSSAPI api = new FreshRSSAPI(account.toFreshRSSCredentials());
if (account.getWriteToken() == null) {
return api.getWriteToken()
.flatMapCompletable(writeToken -> api.
markItemReadUnread(read, itemWithFeed.getItem().getRemoteId(), writeToken));
markItemReadUnread(read, item.getRemoteId(), writeToken));
} else {
return api.markItemReadUnread(read, itemWithFeed.getItem().getRemoteId(), account.getWriteToken());
return api.markItemReadUnread(read, item.getRemoteId(), account.getWriteToken());
}
}
private List<Feed> insertFeeds(List<FreshRSSFeed> freshRSSFeeds, Account account) {

View File

@ -3,6 +3,7 @@ package com.readrops.app.repositories;
import android.accounts.NetworkErrorException;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.app.database.entities.Account;
@ -40,8 +41,8 @@ public class LocalFeedRepository extends ARepository {
private static final String TAG = LocalFeedRepository.class.getSimpleName();
public LocalFeedRepository(Application application) {
super(application);
public LocalFeedRepository(@NonNull Application application, @Nullable Account account) {
super(application, account);
}
@ -51,7 +52,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Observable<Feed> sync(@Nullable List<Feed> feeds, Account account) {
public Observable<Feed> sync(@Nullable List<Feed> feeds) {
return Observable.create(emitter -> {
List<Feed> feedList;
@ -104,7 +105,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account) {
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return Single.create(emitter -> {
List<FeedInsertionResult> insertionResults = new ArrayList<>();
@ -116,7 +117,7 @@ public class LocalFeedRepository extends ARepository {
RSSQueryResult queryResult = rssNet.queryUrl(parsingResult.getUrl(), new HashMap<>());
if (queryResult != null && queryResult.getException() == null) {
Feed feed = insertFeed(queryResult.getFeed(), queryResult.getRssType(), account);
Feed feed = insertFeed(queryResult.getFeed(), queryResult.getRssType());
if (feed != null) {
insertionResult.setFeed(feed);
insertionResults.add(insertionResult);
@ -145,7 +146,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Completable updateFeed(Feed feed, Account account) {
public Completable updateFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().updateFeedFields(feed.getId(), feed.getName(), feed.getUrl(), feed.getFolderId());
emitter.onComplete();
@ -153,7 +154,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Completable deleteFeed(Feed feed, Account account) {
public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().delete(feed.getId());
emitter.onComplete();
@ -161,7 +162,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Completable addFolder(Folder folder, Account account) {
public Completable addFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().insert(folder);
emitter.onComplete();
@ -169,7 +170,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Completable updateFolder(Folder folder, Account account) {
public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().update(folder);
emitter.onComplete();
@ -177,7 +178,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Completable deleteFolder(Folder folder, Account account) {
public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().delete(folder);
emitter.onComplete();
@ -209,7 +210,7 @@ public class LocalFeedRepository extends ARepository {
insertItems(items, dbFeed);
}
private Feed insertFeed(AFeed feed, RSSQuery.RSSType type, Account account) throws IOException {
private Feed insertFeed(AFeed feed, RSSQuery.RSSType type) throws IOException {
Feed dbFeed = null;
switch (type) {
case RSS_2:

View File

@ -4,6 +4,9 @@ import android.app.Application;
import android.database.sqlite.SQLiteConstraintException;
import android.util.TimingLogger;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.app.database.entities.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
@ -41,8 +44,8 @@ public class NextNewsRepository extends ARepository {
private static final String TAG = NextNewsRepository.class.getSimpleName();
public NextNewsRepository(Application application) {
super(application);
public NextNewsRepository(@NonNull Application application, @Nullable Account account) {
super(application, account);
}
@Override
@ -64,7 +67,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Observable<Feed> sync(List<Feed> feeds, Account account) {
public Observable<Feed> sync(List<Feed> feeds) {
return Observable.create(emitter -> {
try {
NextNewsAPI newsAPI = new NextNewsAPI(account.toNextNewsCredentials());
@ -90,13 +93,13 @@ public class NextNewsRepository extends ARepository {
if (!syncResult.isError()) {
insertFolders(syncResult.getFolders(), account);
insertFolders(syncResult.getFolders());
timings.addSplit("insert folders");
insertFeeds(syncResult.getFeeds(), account);
insertFeeds(syncResult.getFeeds());
timings.addSplit("insert feeds");
insertItems(syncResult.getItems(), account, syncType == SyncType.INITIAL_SYNC);
insertItems(syncResult.getItems(), syncType == SyncType.INITIAL_SYNC);
timings.addSplit("insert items");
timings.dumpToLog();
@ -116,7 +119,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account) {
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return Single.create(emitter -> {
List<FeedInsertionResult> feedInsertionResults = new ArrayList<>();
NextNewsAPI newsAPI = new NextNewsAPI(account.toNextNewsCredentials());
@ -128,7 +131,7 @@ public class NextNewsRepository extends ARepository {
NextNewsFeeds nextNewsFeeds = newsAPI.createFeed(result.getUrl(), 0);
if (nextNewsFeeds != null) {
List<Feed> newFeeds = insertFeeds(nextNewsFeeds.getFeeds(), account);
List<Feed> newFeeds = insertFeeds(nextNewsFeeds.getFeeds());
// there is always only one object in the list, see nextcloud news api doc
insertionResult.setFeed(newFeeds.get(0));
@ -154,7 +157,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Completable updateFeed(Feed feed, Account account) {
public Completable updateFeed(Feed feed) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
@ -185,7 +188,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Completable deleteFeed(Feed feed, Account account) {
public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
@ -204,7 +207,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Completable addFolder(Folder folder, Account account) {
public Completable addFolder(Folder folder) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
@ -212,7 +215,7 @@ public class NextNewsRepository extends ARepository {
NextNewsFolders folders = api.createFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()));
if (folders != null)
insertFolders(folders.getFolders(), account);
insertFolders(folders.getFolders());
else
emitter.onError(new Exception("Unknown error"));
} catch (Exception e) {
@ -224,7 +227,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Completable updateFolder(Folder folder, Account account) {
public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
@ -244,7 +247,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Completable deleteFolder(Folder folder, Account account) {
public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
@ -263,7 +266,7 @@ public class NextNewsRepository extends ARepository {
});
}
private List<Feed> insertFeeds(List<NextNewsFeed> nextNewsFeeds, Account account) {
private List<Feed> insertFeeds(List<NextNewsFeed> nextNewsFeeds) {
List<Feed> feeds = new ArrayList<>();
for (NextNewsFeed nextNewsFeed : nextNewsFeeds) {
@ -281,7 +284,7 @@ public class NextNewsRepository extends ARepository {
return insertedFeeds;
}
private void insertFolders(List<NextNewsFolder> nextNewsFolders, Account account) {
private void insertFolders(List<NextNewsFolder> nextNewsFolders) {
List<Folder> folders = new ArrayList<>();
for (NextNewsFolder nextNewsFolder : nextNewsFolders) {
@ -295,7 +298,7 @@ public class NextNewsRepository extends ARepository {
database.folderDao().foldersUpsert(folders, account);
}
private void insertItems(List<NextNewsItem> items, Account account, boolean initialSync) {
private void insertItems(List<NextNewsItem> items, boolean initialSync) {
List<Item> newItems = new ArrayList<>();
for (NextNewsItem nextNewsItem : items) {

View File

@ -28,10 +28,10 @@ public class AccountViewModel extends AndroidViewModel {
public void setAccountType(Account.AccountType accountType) throws Exception {
switch (accountType) {
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication());
repository = new NextNewsRepository(getApplication(), null);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication());
repository = new FreshRSSRepository(getApplication(), null);
break;
default:
throw new Exception("unknown account type");
@ -64,10 +64,6 @@ public class AccountViewModel extends AndroidViewModel {
});
}
public Single<Integer> getAccountCountByAccountType(int accountTypeCode) {
return Single.create(emitter -> emitter.onSuccess(database.accountDao().getAccountCountByType(accountTypeCode)));
}
public Single<Integer> getAccountCount() {
return Single.create(emitter -> emitter.onSuccess(database.accountDao().getAccountCount()));
}

View File

@ -34,14 +34,14 @@ public class AddFeedsViewModel extends AndroidViewModel {
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results, Account account) {
switch (account.getAccountType()) {
case LOCAL:
repository = new LocalFeedRepository(getApplication());
repository = new LocalFeedRepository(getApplication(), account);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication());
repository = new NextNewsRepository(getApplication(), account);
break;
}
return repository.addFeeds(results, account);
return repository.addFeeds(results);
}
public Single<List<ParsingResult>> parseUrl(String url) {

View File

@ -63,13 +63,13 @@ public class MainViewModel extends AndroidViewModel {
private void setRepository(Account.AccountType accountType) {
switch (accountType) {
case LOCAL:
repository = new LocalFeedRepository(getApplication());
repository = new LocalFeedRepository(getApplication(), currentAccount);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication());
repository = new NextNewsRepository(getApplication(), currentAccount);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication());
repository = new FreshRSSRepository(getApplication(), currentAccount);
break;
}
}
@ -125,8 +125,8 @@ public class MainViewModel extends AndroidViewModel {
return itemsWithFeed;
}
public Observable<Feed> sync(List<Feed> feeds, Account account) {
return repository.sync(feeds, account);
public Observable<Feed> sync(List<Feed> feeds) {
return repository.sync(feeds);
}
public Single<Integer> getFeedCount() {
@ -250,18 +250,8 @@ public class MainViewModel extends AndroidViewModel {
//region Item read state
public Completable setItemReadState(ItemWithFeed item, boolean read) {
Completable completable = Completable.create(emitter -> {
db.itemDao().setReadState(item.getItem().getId(), read ? 1 : 0, !item.getItem().isReadChanged() ? 1 : 0);
emitter.onComplete();
});
// TODO : temporary until a better idea comes out
if (currentAccount.getAccountType() == Account.AccountType.FRESHRSS) {
return completable.andThen(((FreshRSSRepository) repository).
markItemReadUnread(item, read, currentAccount));
} else
return completable;
public Completable setItemReadState(ItemWithFeed itemWithFeed, boolean read) {
return repository.setItemReadState(itemWithFeed.getItem(), read);
}
public Completable setItemsReadState(List<ItemWithFeed> items, boolean read) {

View File

@ -36,10 +36,10 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel {
private void setup() {
switch (account.getAccountType()) {
case LOCAL:
repository = new LocalFeedRepository(getApplication());
repository = new LocalFeedRepository(getApplication(), account);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication());
repository = new NextNewsRepository(getApplication(), account);
break;
}
@ -52,7 +52,7 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel {
}
public Completable updateFeedWithFolder(Feed feed) {
return repository.updateFeed(feed, account);
return repository.updateFeed(feed);
}
public Account getAccount() {
@ -69,18 +69,18 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel {
}
public Completable addFolder(Folder folder) {
return repository.addFolder(folder, account);
return repository.addFolder(folder);
}
public Completable updateFolder(Folder folder) {
return repository.updateFolder(folder, account);
return repository.updateFolder(folder);
}
public Completable deleteFolder(Folder folder) {
return repository.deleteFolder(folder, account);
return repository.deleteFolder(folder);
}
public Completable deleteFeed(Feed feed) {
return repository.deleteFeed(feed, account);
return repository.deleteFeed(feed);
}
}