Add adapter for Nextcloud News feeds api call

This commit is contained in:
Shinokuni 2020-01-11 19:09:43 +01:00
parent 6705b47df6
commit fca4b7f513
5 changed files with 89 additions and 25 deletions

View File

@ -10,7 +10,6 @@ import androidx.annotation.Nullable;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.Utils;
import com.readrops.app.utils.matchers.FeedMatcher;
import com.readrops.app.utils.matchers.ItemMatcher;
import com.readrops.readropsdb.entities.Feed;
import com.readrops.readropsdb.entities.Folder;
@ -113,14 +112,14 @@ public class NextNewsRepository extends ARepository<NextNewsAPI> {
if (!syncResult.isError()) {
insertFolders(syncResult.getFolders());
timings.addSplit("insert folders");
/*insertFolders(syncResult.getFolders());
timings.addSplit("insert folders");*/
insertFeeds(syncResult.getFeeds());
timings.addSplit("insert feeds");
insertItems(syncResult.getItems(), syncType == SyncType.INITIAL_SYNC);
timings.addSplit("insert items");
/*insertItems(syncResult.getItems(), syncType == SyncType.INITIAL_SYNC);
timings.addSplit("insert items");*/
timings.dumpToLog();
account.setLastModified(lastModified);
@ -150,9 +149,9 @@ public class NextNewsRepository extends ARepository<NextNewsAPI> {
NextNewsFeeds nextNewsFeeds = api.createFeed(result.getUrl(), 0);
if (nextNewsFeeds != null) {
List<Feed> newFeeds = insertFeeds(nextNewsFeeds.getFeeds());
/*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));
insertionResult.setFeed(newFeeds.get(0));*/
} else
insertionResult.setInsertionError(FeedInsertionResult.FeedInsertionError.UNKNOWN_ERROR);
@ -269,14 +268,12 @@ public class NextNewsRepository extends ARepository<NextNewsAPI> {
}).andThen(super.deleteFolder(folder));
}
private List<Feed> insertFeeds(List<NextNewsFeed> nextNewsFeeds) {
List<Feed> feeds = new ArrayList<>();
for (NextNewsFeed nextNewsFeed : nextNewsFeeds) {
feeds.add(FeedMatcher.nextNewsFeedToFeed(nextNewsFeed, account));
private List<Feed> insertFeeds(List<Feed> nextNewsFeeds) {
for (Feed nextNewsFeed : nextNewsFeeds) {
nextNewsFeed.setAccountId(account.getId());
}
List<Long> insertedFeedsIds = database.feedDao().feedsUpsert(feeds, account);
List<Long> insertedFeedsIds = database.feedDao().feedsUpsert(nextNewsFeeds, account);
List<Feed> insertedFeeds = new ArrayList<>();
if (!insertedFeedsIds.isEmpty()) {

View File

@ -5,9 +5,11 @@ import android.content.res.Resources;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.readropsdb.entities.Feed;
import com.readrops.readropslibrary.services.API;
import com.readrops.readropslibrary.services.Credentials;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.nextcloudnews.adapters.NextNewsFeedsAdapter;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder;
@ -23,6 +25,7 @@ import com.squareup.moshi.Moshi;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import retrofit2.Response;
@ -38,6 +41,7 @@ public class NextNewsAPI extends API<NextNewsService> {
@Override
protected Moshi buildMoshi() {
return new Moshi.Builder()
.add(new NextNewsFeedsAdapter())
.build();
}
@ -86,14 +90,14 @@ public class NextNewsAPI extends API<NextNewsService> {
private void initialSync(NextNewsSyncResult syncResult) throws IOException {
getFeedsAndFolders(syncResult);
Response<NextNewsItems> itemsResponse = api.getItems(3, false, MAX_ITEMS).execute();
/*Response<NextNewsItems> itemsResponse = api.getItems(3, false, MAX_ITEMS).execute();
NextNewsItems itemList = itemsResponse.body();
if (!itemsResponse.isSuccessful())
syncResult.setError(true);
if (itemList != null)
syncResult.setItems(itemList.getItems());
syncResult.setItems(itemList.getItems());*/
}
private void classicSync(NextNewsSyncResult syncResult, NextNewsSyncData data) throws IOException {
@ -111,23 +115,23 @@ public class NextNewsAPI extends API<NextNewsService> {
}
private void getFeedsAndFolders(NextNewsSyncResult syncResult) throws IOException {
Response<NextNewsFeeds> feedResponse = api.getFeeds().execute();
NextNewsFeeds feedList = feedResponse.body();
Response<List<Feed>> feedResponse = api.getFeeds().execute();
List<Feed> feedList = feedResponse.body();
if (!feedResponse.isSuccessful())
syncResult.setError(true);
Response<NextNewsFolders> folderResponse = api.getFolders().execute();
/*Response<NextNewsFolders> folderResponse = api.getFolders().execute();
NextNewsFolders folderList = folderResponse.body();
if (!folderResponse.isSuccessful())
syncResult.setError(true);
if (folderList != null)
syncResult.setFolders(folderList.getFolders());
syncResult.setFolders(folderList.getFolders());*/
if (feedList != null)
syncResult.setFeeds(feedList.getFeeds());
syncResult.setFeeds(feedList);
}

View File

@ -1,5 +1,6 @@
package com.readrops.readropslibrary.services.nextcloudnews;
import com.readrops.readropsdb.entities.Feed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolders;
@ -8,6 +9,7 @@ import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItems;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsRenameFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsUser;
import java.util.List;
import java.util.Map;
import okhttp3.ResponseBody;
@ -31,7 +33,7 @@ public interface NextNewsService {
Call<NextNewsFolders> getFolders();
@GET("feeds")
Call<NextNewsFeeds> getFeeds();
Call<List<Feed>> getFeeds();
@GET("items")
Call<NextNewsItems> getItems(@Query("type") int type, @Query("getRead") boolean read, @Query("batchSize") int batchSize);

View File

@ -1,6 +1,6 @@
package com.readrops.readropslibrary.services.nextcloudnews;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropsdb.entities.Feed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItem;
@ -10,7 +10,7 @@ public class NextNewsSyncResult {
private List<NextNewsFolder> folders;
private List<NextNewsFeed> feeds;
private List<Feed> feeds;
private List<NextNewsItem> items;
@ -28,11 +28,11 @@ public class NextNewsSyncResult {
this.folders = folders;
}
public List<NextNewsFeed> getFeeds() {
public List<Feed> getFeeds() {
return feeds;
}
public void setFeeds(List<NextNewsFeed> feeds) {
public void setFeeds(List<Feed> feeds) {
this.feeds = feeds;
}

View File

@ -0,0 +1,61 @@
package com.readrops.readropslibrary.services.nextcloudnews.adapters
import android.annotation.SuppressLint
import com.readrops.readropsdb.entities.Feed
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonReader
import com.squareup.moshi.ToJson
class NextNewsFeedsAdapter {
@ToJson
fun toJson(feed: Feed): String = ""
@SuppressLint("CheckResult")
@FromJson
fun fromJson(reader: JsonReader): List<Feed> {
val feeds = mutableListOf<Feed>()
reader.beginObject()
while (reader.hasNext()) {
if (reader.nextName() == "feeds") parseFeeds(reader, feeds) else reader.skipValue()
}
reader.endObject()
return feeds
}
private fun parseFeeds(reader: JsonReader, feeds: MutableList<Feed>) {
reader.beginArray()
while (reader.hasNext()) {
val feed = Feed()
reader.beginObject()
while (reader.hasNext()) {
with(feed) {
when (reader.selectName(NAMES)) {
0 -> remoteId = reader.nextString()
1 -> url = reader.nextString()
2 -> name = reader.nextString()
3 -> iconUrl = reader.nextString()
4 -> remoteFolderId = reader.nextInt().toString()
5 -> siteUrl = if (reader.peek() != JsonReader.Token.NULL) reader.nextString() else reader.nextNull()
else -> reader.skipValue()
}
}
}
feeds += feed
reader.endObject()
}
reader.endArray()
}
companion object {
val NAMES: JsonReader.Options = JsonReader.Options.of("id", "url", "title", "faviconLink", "folderId", "link")
}
}