From a179a184323bc471fa337dc44f94e78f4c439efb Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sat, 11 Jan 2020 19:30:29 +0100 Subject: [PATCH] Add adapter for Nextcloud News folders api call --- .../app/repositories/NextNewsRepository.java | 16 ++---- .../services/nextcloudnews/NextNewsAPI.java | 9 ++-- .../nextcloudnews/NextNewsService.java | 3 +- .../nextcloudnews/NextNewsSyncResult.java | 8 +-- .../adapters/NextNewsFoldersAdapter.kt | 50 +++++++++++++++++++ 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/adapters/NextNewsFoldersAdapter.kt diff --git a/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java b/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java index ad19d783..4a84e681 100644 --- a/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java @@ -112,8 +112,8 @@ public class NextNewsRepository extends ARepository { if (!syncResult.isError()) { - /*insertFolders(syncResult.getFolders()); - timings.addSplit("insert folders");*/ + insertFolders(syncResult.getFolders()); + timings.addSplit("insert folders"); insertFeeds(syncResult.getFeeds()); timings.addSplit("insert feeds"); @@ -284,18 +284,12 @@ public class NextNewsRepository extends ARepository { return insertedFeeds; } - private void insertFolders(List nextNewsFolders) { - List folders = new ArrayList<>(); - - for (NextNewsFolder nextNewsFolder : nextNewsFolders) { - Folder folder = new Folder(nextNewsFolder.getName()); + private void insertFolders(List nextNewsFolders) { + for (Folder folder : nextNewsFolders) { folder.setAccountId(account.getId()); - folder.setRemoteId(String.valueOf(nextNewsFolder.getId())); - - folders.add(folder); } - database.folderDao().foldersUpsert(folders, account); + database.folderDao().foldersUpsert(nextNewsFolders, account); } private void insertItems(List items, boolean initialSync) { diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsAPI.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsAPI.java index 16486e54..d0850375 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsAPI.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsAPI.java @@ -6,10 +6,12 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.readrops.readropsdb.entities.Feed; +import com.readrops.readropsdb.entities.Folder; 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.adapters.NextNewsFoldersAdapter; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder; @@ -42,6 +44,7 @@ public class NextNewsAPI extends API { protected Moshi buildMoshi() { return new Moshi.Builder() .add(new NextNewsFeedsAdapter()) + .add(new NextNewsFoldersAdapter()) .build(); } @@ -121,14 +124,14 @@ public class NextNewsAPI extends API { if (!feedResponse.isSuccessful()) syncResult.setError(true); - /*Response folderResponse = api.getFolders().execute(); - NextNewsFolders folderList = folderResponse.body(); + Response> folderResponse = api.getFolders().execute(); + List folderList = folderResponse.body(); if (!folderResponse.isSuccessful()) syncResult.setError(true); if (folderList != null) - syncResult.setFolders(folderList.getFolders());*/ + syncResult.setFolders(folderList); if (feedList != null) syncResult.setFeeds(feedList); diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsService.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsService.java index 19d7c24e..0bd0da61 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsService.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsService.java @@ -1,6 +1,7 @@ package com.readrops.readropslibrary.services.nextcloudnews; import com.readrops.readropsdb.entities.Feed; +import com.readrops.readropsdb.entities.Folder; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolders; @@ -30,7 +31,7 @@ public interface NextNewsService { Call getUser(); @GET("folders") - Call getFolders(); + Call> getFolders(); @GET("feeds") Call> getFeeds(); diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsSyncResult.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsSyncResult.java index 518b388c..cda15e8f 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsSyncResult.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/NextNewsSyncResult.java @@ -1,14 +1,14 @@ package com.readrops.readropslibrary.services.nextcloudnews; import com.readrops.readropsdb.entities.Feed; -import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder; +import com.readrops.readropsdb.entities.Folder; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItem; import java.util.List; public class NextNewsSyncResult { - private List folders; + private List folders; private List feeds; @@ -20,11 +20,11 @@ public class NextNewsSyncResult { // empty constructor } - public List getFolders() { + public List getFolders() { return folders; } - public void setFolders(List folders) { + public void setFolders(List folders) { this.folders = folders; } diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/adapters/NextNewsFoldersAdapter.kt b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/adapters/NextNewsFoldersAdapter.kt new file mode 100644 index 00000000..0c6cc2d5 --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/nextcloudnews/adapters/NextNewsFoldersAdapter.kt @@ -0,0 +1,50 @@ +package com.readrops.readropslibrary.services.nextcloudnews.adapters + +import android.annotation.SuppressLint +import com.readrops.readropsdb.entities.Folder +import com.squareup.moshi.FromJson +import com.squareup.moshi.JsonReader +import com.squareup.moshi.ToJson + +class NextNewsFoldersAdapter { + + @ToJson + fun toJson(folder: Folder): String = "" + + @SuppressLint("CheckResult") + @FromJson + fun fromJson(reader: JsonReader): List { + val folders = mutableListOf() + + reader.beginObject() + reader.nextName() // "folders", beginning of folders array + reader.beginArray() + + while (reader.hasNext()) { + val folder = Folder() + reader.beginObject() + + while (reader.hasNext()) { + with(folder) { + when (reader.selectName(NAMES)) { + 0 -> remoteId = reader.nextInt().toString() + 1 -> name = reader.nextString() + else -> reader.skipValue() + } + } + } + + folders += folder + reader.endObject() + } + + reader.endArray() + reader.endObject() + + return folders + } + + companion object { + val NAMES: JsonReader.Options = JsonReader.Options.of("id", "name") + } +} \ No newline at end of file