Add greader feed insertion and deletion requests

This commit is contained in:
Shinokuni 2019-08-17 16:04:56 +02:00
parent ccf0a46a13
commit 62eec1e077
8 changed files with 72 additions and 23 deletions

View File

@ -12,6 +12,7 @@ import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.readrops.app.R;
import com.readrops.readropslibrary.services.Credentials;
import com.readrops.readropslibrary.services.freshrss.FreshRSSCredentials;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsCredentials;
@ -67,6 +68,8 @@ public class Account implements Parcelable {
currentAccount = in.readByte() != 0;
login = in.readString();
password = in.readString();
token = in.readString();
writeToken = in.readString();
}
public static final Creator<Account> CREATOR = new Creator<Account>() {
@ -193,6 +196,8 @@ public class Account implements Parcelable {
dest.writeByte((byte) (currentAccount ? 1 : 0));
dest.writeString(login);
dest.writeString(password);
dest.writeString(token);
dest.writeString(writeToken);
}
public enum AccountType implements Parcelable {
@ -244,12 +249,15 @@ public class Account implements Parcelable {
}
}
public NextNewsCredentials toNextNewsCredentials() {
return new NextNewsCredentials(login, password, url);
}
public FreshRSSCredentials toFreshRSSCredentials() {
return new FreshRSSCredentials(token, url);
public Credentials toCredentials() {
switch (accountType) {
case NEXTCLOUD_NEWS:
return new NextNewsCredentials(login, password, url);
case FRESHRSS:
return new FreshRSSCredentials(token, url);
default:
return null;
}
}
public boolean isLocal() {

View File

@ -61,7 +61,7 @@ public class FreshRSSRepository extends ARepository {
@Override
public Observable<Feed> sync(List<Feed> feeds) {
FreshRSSAPI api = new FreshRSSAPI(account.toFreshRSSCredentials());
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
FreshRSSSyncData syncData = new FreshRSSSyncData();
SyncType syncType;
@ -87,7 +87,17 @@ public class FreshRSSRepository extends ARepository {
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return null;
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
List<Completable> completableList = new ArrayList<>();
for (ParsingResult result : results) {
completableList.add(api.createFeed(account.getWriteToken(), result.getUrl()));
}
// TODO : see how to handle exceptions/errors like the others repositories
return Completable.concat(completableList)
.andThen(Single.just(new ArrayList<>()));
}
@Override
@ -97,7 +107,13 @@ public class FreshRSSRepository extends ARepository {
@Override
public Completable deleteFeed(Feed feed) {
return null;
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
return api.deleteFeed(account.getWriteToken(), feed.getUrl())
.andThen(Completable.create(emitter -> {
database.feedDao().delete(feed.getId());
emitter.onComplete();
}));
}
@Override
@ -117,17 +133,19 @@ public class FreshRSSRepository extends ARepository {
@Override
public Completable setItemReadState(Item item, boolean read) {
FreshRSSAPI api = new FreshRSSAPI(account.toFreshRSSCredentials());
FreshRSSAPI api = new FreshRSSAPI(account.toCredentials());
if (account.getWriteToken() == null) {
return api.getWriteToken()
.flatMapCompletable(writeToken -> {
database.accountDao().updateWriteToken(account.getId(), writeToken);
return api.markItemReadUnread(read, item.getRemoteId(), writeToken).concatWith(super.setItemReadState(item, read));
return api.markItemReadUnread(read, item.getRemoteId(), writeToken)
.concatWith(super.setItemReadState(item, read));
});
} else {
return api.markItemReadUnread(read, item.getRemoteId(), account.getWriteToken()).concatWith(super.setItemReadState(item, read));
return api.markItemReadUnread(read, item.getRemoteId(), account.getWriteToken())
.concatWith(super.setItemReadState(item, read));
}
}

View File

@ -51,7 +51,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Single<Boolean> login(Account account, boolean insert) {
return Single.create(emitter -> {
NextNewsAPI newsAPI = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials());
NextNewsUser user = newsAPI.login();
if (user != null) {
@ -70,7 +70,7 @@ public class NextNewsRepository extends ARepository {
public Observable<Feed> sync(List<Feed> feeds) {
return Observable.create(emitter -> {
try {
NextNewsAPI newsAPI = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials());
long lastModified = LocalDateTime.now().toDateTime().getMillis();
SyncType syncType;
@ -122,7 +122,7 @@ public class NextNewsRepository extends ARepository {
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return Single.create(emitter -> {
List<FeedInsertionResult> feedInsertionResults = new ArrayList<>();
NextNewsAPI newsAPI = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI newsAPI = new NextNewsAPI(account.toCredentials());
for (ParsingResult result : results) {
FeedInsertionResult insertionResult = new FeedInsertionResult();
@ -159,7 +159,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Completable updateFeed(Feed feed) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
Folder folder = feed.getFolderId() == null ? null : database.folderDao().select(feed.getFolderId());
@ -190,7 +190,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Completable deleteFeed(Feed feed) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try {
if (api.deleteFeed(Integer.parseInt(feed.getRemoteId()))) {
@ -209,7 +209,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Completable addFolder(Folder folder) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try {
NextNewsFolders folders = api.createFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()));
@ -229,7 +229,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Completable updateFolder(Folder folder) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try {
if (api.renameFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) {
@ -249,7 +249,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Completable deleteFolder(Folder folder) {
return Completable.create(emitter -> {
NextNewsAPI api = new NextNewsAPI(account.toNextNewsCredentials());
NextNewsAPI api = new NextNewsAPI(account.toCredentials());
try {
if (api.deleteFolder(new NextNewsFolder(Integer.parseInt(folder.getRemoteId()), folder.getName()))) {

View File

@ -1,13 +1,15 @@
package com.readrops.app.viewmodels;
import android.app.Application;
import androidx.lifecycle.AndroidViewModel;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Account;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.FreshRSSRepository;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.NextNewsRepository;
import com.readrops.app.utils.FeedInsertionResult;
@ -39,6 +41,9 @@ public class AddFeedsViewModel extends AndroidViewModel {
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication(), account);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication(), account);
break;
}
return repository.addFeeds(results);

View File

@ -11,6 +11,7 @@ import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.FreshRSSRepository;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.NextNewsRepository;
@ -41,6 +42,9 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel {
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(getApplication(), account);
break;
case FRESHRSS:
repository = new FreshRSSRepository(getApplication(), account);
break;
}
feedsWithFolder = db.feedDao().getAllFeedsWithFolder(account.getId());

View File

@ -3,6 +3,7 @@ package com.readrops.readropslibrary.services.freshrss;
import androidx.annotation.NonNull;
import com.readrops.readropslibrary.services.API;
import com.readrops.readropslibrary.services.Credentials;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeeds;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFolders;
@ -19,7 +20,7 @@ import okhttp3.RequestBody;
public class FreshRSSAPI extends API<FreshRSSService> {
public FreshRSSAPI(FreshRSSCredentials credentials) {
public FreshRSSAPI(Credentials credentials) {
super(credentials, FreshRSSService.class, FreshRSSService.END_POINT);
}
@ -103,6 +104,14 @@ public class FreshRSSAPI extends API<FreshRSSService> {
return api.setItemReadState(token, null, EXCLUDE_ITEMS.EXCLUDE_READ_ITEMS.value, itemId);
}
public Completable createFeed(String token, String feedUrl) {
return api.createOrDeleteFeed(token, "feed/" + feedUrl, "subscribe");
}
public Completable deleteFeed(String token, String feedUrl) {
return api.createOrDeleteFeed(token, "feed/" + feedUrl, "unsubscribe");
}
public enum EXCLUDE_ITEMS {
EXCLUDE_READ_ITEMS("user/-/state/com.google/read");

View File

@ -41,4 +41,8 @@ public interface FreshRSSService {
@FormUrlEncoded
@POST("reader/api/0/edit-tag")
Completable setItemReadState(@Field("T") String token, @Field("a") String readAction, @Field("r") String unreadAction, @Field("i") String itemId);
@FormUrlEncoded
@POST("reader/api/0/subscription/edit")
Completable createOrDeleteFeed(@Field("T") String token, @Field("s") String feedUrl, @Field("ac") String action);
}

View File

@ -6,6 +6,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.readropslibrary.services.API;
import com.readrops.readropslibrary.services.Credentials;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
@ -29,7 +30,7 @@ public class NextNewsAPI extends API<NextNewsService> {
private static final String TAG = NextNewsAPI.class.getSimpleName();
public NextNewsAPI(NextNewsCredentials credentials) {
public NextNewsAPI(Credentials credentials) {
super(credentials, NextNewsService.class, NextNewsService.END_POINT);
}