Put some requests in the abstract repository for a better abstract model

This commit is contained in:
Shinokuni 2019-08-17 18:00:24 +02:00
parent 20ba537670
commit f462933c8d
4 changed files with 38 additions and 71 deletions

View File

@ -42,15 +42,40 @@ public abstract class ARepository {
public abstract Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results); public abstract Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results);
public abstract Completable updateFeed(Feed feed); public Completable updateFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().updateFeedFields(feed.getId(), feed.getName(), feed.getUrl(), feed.getFolderId());
emitter.onComplete();
});
}
public abstract Completable deleteFeed(Feed feed); public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().delete(feed.getId());
emitter.onComplete();
});
}
public abstract Completable addFolder(Folder folder); public Completable addFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().insert(folder);
emitter.onComplete();
});
}
public abstract Completable updateFolder(Folder folder); public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().update(folder);
emitter.onComplete();
});
}
public abstract Completable deleteFolder(Folder folder); public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().delete(folder);
emitter.onComplete();
});
}
public Completable setItemReadState(Item item, boolean read) { public Completable setItemReadState(Item item, boolean read) {
return Completable.create(emitter -> { return Completable.create(emitter -> {

View File

@ -110,10 +110,7 @@ public class FreshRSSRepository extends ARepository {
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials()); FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return api.deleteFeed(account.getWriteToken(), feed.getUrl()) return api.deleteFeed(account.getWriteToken(), feed.getUrl())
.andThen(Completable.create(emitter -> { .andThen(super.deleteFeed(feed));
database.feedDao().delete(feed.getId());
emitter.onComplete();
}));
} }
@Override @Override
@ -145,7 +142,7 @@ public class FreshRSSRepository extends ARepository {
}); });
} else { } else {
return api.markItemReadUnread(read, item.getRemoteId(), account.getWriteToken()) return api.markItemReadUnread(read, item.getRemoteId(), account.getWriteToken())
.concatWith(super.setItemReadState(item, read)); .andThen(super.setItemReadState(item, read));
} }
} }

View File

@ -8,7 +8,6 @@ import androidx.annotation.Nullable;
import com.readrops.app.database.entities.Account; import com.readrops.app.database.entities.Account;
import com.readrops.app.database.entities.Feed; 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.entities.Item;
import com.readrops.app.utils.FeedInsertionResult; import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.HtmlParser; import com.readrops.app.utils.HtmlParser;
@ -33,7 +32,6 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import io.reactivex.Completable;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
@ -145,46 +143,6 @@ public class LocalFeedRepository extends ARepository {
}); });
} }
@Override
public Completable updateFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().updateFeedFields(feed.getId(), feed.getName(), feed.getUrl(), feed.getFolderId());
emitter.onComplete();
});
}
@Override
public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> {
database.feedDao().delete(feed.getId());
emitter.onComplete();
});
}
@Override
public Completable addFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().insert(folder);
emitter.onComplete();
});
}
@Override
public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().update(folder);
emitter.onComplete();
});
}
@Override
public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> {
database.folderDao().delete(folder);
emitter.onComplete();
});
}
private void insertNewItems(AFeed feed, RSSQuery.RSSType type) throws ParseException { private void insertNewItems(AFeed feed, RSSQuery.RSSType type) throws ParseException {
Feed dbFeed = null; Feed dbFeed = null;
List<Item> items = null; List<Item> items = null;
@ -284,8 +242,4 @@ public class LocalFeedRepository extends ARepository {
else else
return FeedInsertionResult.FeedInsertionError.UNKNOWN_ERROR; return FeedInsertionResult.FeedInsertionError.UNKNOWN_ERROR;
} }
} }

View File

@ -162,7 +162,6 @@ public class NextNewsRepository extends ARepository {
NextNewsAPI api = new NextNewsAPI(account.toCredentials()); NextNewsAPI api = new NextNewsAPI(account.toCredentials());
Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId()); Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId());
NextNewsRenameFeed newsRenameFeed = new NextNewsRenameFeed(Integer.parseInt(feed.getRemoteId()), feed.getName()); NextNewsRenameFeed newsRenameFeed = new NextNewsRenameFeed(Integer.parseInt(feed.getRemoteId()), feed.getName());
NextNewsFeed newsFeed; NextNewsFeed newsFeed;
@ -173,18 +172,13 @@ public class NextNewsRepository extends ARepository {
try { try {
if (api.renameFeed(newsRenameFeed) && api.changeFeedFolder(newsFeed)) { if (api.renameFeed(newsRenameFeed) && api.changeFeedFolder(newsFeed)) {
if (folder != null) emitter.onComplete();
database.feedDao().updateFeedFields(feed.getId(), feed.getName(), feed.getUrl(), folder.getId());
else
database.feedDao().updateFeedFields(feed.getId(), feed.getName(), feed.getUrl(), null);
} else } else
emitter.onError(new Exception("Unknown error")); emitter.onError(new Exception("Unknown error when updating feed"));
} catch (Exception e) { } catch (Exception e) {
emitter.onError(e); emitter.onError(e);
} }
}).andThen(super.updateFeed(feed));
emitter.onComplete();
});
} }
@Override @Override
@ -194,7 +188,6 @@ public class NextNewsRepository extends ARepository {
try { try {
if (api.deleteFeed(Integer.parseInt(feed.getRemoteId()))) { if (api.deleteFeed(Integer.parseInt(feed.getRemoteId()))) {
database.feedDao().delete(feed.getId());
emitter.onComplete(); emitter.onComplete();
} else } else
emitter.onError(new Exception("Unknown error")); emitter.onError(new Exception("Unknown error"));
@ -203,7 +196,7 @@ public class NextNewsRepository extends ARepository {
} }
emitter.onComplete(); emitter.onComplete();
}); }).andThen(super.deleteFeed(feed));
} }
@Override @Override
@ -233,7 +226,6 @@ public class NextNewsRepository extends ARepository {
try { try {
if (api.renameFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) { if (api.renameFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) {
database.folderDao().update(folder);
emitter.onComplete(); emitter.onComplete();
} else } else
emitter.onError(new Exception("Unknown error")); emitter.onError(new Exception("Unknown error"));
@ -243,7 +235,7 @@ public class NextNewsRepository extends ARepository {
} }
emitter.onComplete(); emitter.onComplete();
}); }).andThen(super.updateFolder(folder));
} }
@Override @Override
@ -253,7 +245,6 @@ public class NextNewsRepository extends ARepository {
try { try {
if (api.deleteFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) { if (api.deleteFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) {
database.folderDao().delete(folder);
emitter.onComplete(); emitter.onComplete();
} else } else
emitter.onError(new Exception("Unknown error")); emitter.onError(new Exception("Unknown error"));
@ -263,7 +254,7 @@ public class NextNewsRepository extends ARepository {
} }
emitter.onComplete(); emitter.onComplete();
}); }).andThen(super.deleteFolder(folder));
} }
private List<Feed> insertFeeds(List<NextNewsFeed> nextNewsFeeds) { private List<Feed> insertFeeds(List<NextNewsFeed> nextNewsFeeds) {