Add normal sync calls in NextNewsAPI

This commit is contained in:
Shinokuni 2019-05-14 14:10:14 +02:00
parent ac030c146f
commit 970c14581b
5 changed files with 101 additions and 39 deletions

View File

@ -14,6 +14,7 @@ import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.Utils;
import com.readrops.readropslibrary.services.nextcloudnews.Credentials;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsAPI;
import com.readrops.readropslibrary.services.nextcloudnews.SyncData;
import com.readrops.readropslibrary.services.nextcloudnews.SyncResult;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder;
@ -44,8 +45,8 @@ public class NextNewsRepository extends ARepository {
try {
NextNewsAPI newsAPI = new NextNewsAPI();
Credentials credentials = new Credentials("Lucas", LibUtils.NEXTCLOUD_PASSWORD, "https://cloud.openserver.fr");
SyncResult syncResult = newsAPI.sync(credentials, NextNewsAPI.SyncType.INITIAL_SYNC, null);
Credentials credentials = new Credentials("", LibUtils.NEXTCLOUD_PASSWORD, "");
SyncResult syncResult = newsAPI.sync(credentials, NextNewsAPI.SyncType.INITIAL_SYNC, new SyncData());
if (!syncResult.isError()) {
TimingLogger timings = new TimingLogger(TAG, "sync");
@ -62,7 +63,7 @@ public class NextNewsRepository extends ARepository {
emitter.onComplete();
} else
emitter.onError(new Throwable());
} catch (IOException e) {
e.printStackTrace();
emitter.onError(e);

View File

@ -3,6 +3,7 @@ package com.readrops.readropslibrary.services;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolders;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItems;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsUser;
import java.util.List;
@ -18,6 +19,9 @@ public interface NextNewsService {
String ENDPOINT = "/index.php/apps/news/api/v1-2/";
@GET("user")
Call<NextNewsUser> getUser();
@GET("folders")
Call<NextNewsFolders> getFolders();
@ -27,20 +31,8 @@ public interface NextNewsService {
@GET("items")
Call<NextNewsItems> getItems(@Query("type") int type, @Query("getRead") boolean read, @Query("batchSize") int batchSize);
@GET("items/updated?type=3")
Call<NextNewsItems> getNewItems(@Query("lastModified") long lastModified);
@PUT("items/read/multiple")
Call<ResponseBody> setReadArticles(@Body List<Integer> itemsIds);
@PUT("items/unread/multiple")
Call<ResponseBody> setUnreadArticles(@Body List<Integer> itemsIds);
@PUT("items/starred/multiple")
Call<ResponseBody> setStarredArticles(@Body List<Integer> itemsIds);
@PUT("items/unstarred/multiple")
Call<ResponseBody> setUnstarredArticles(@Body List<Integer> itemsIds);
@GET("items/updated")
Call<NextNewsItems> getNewItems(@Query("lastModified") long lastModified, @Query("type") int type);
@PUT("items/{stateType}/multiple")
Call<ResponseBody> setArticlesState(@Path("stateType") String stateType, @Body List<Integer> items);

View File

@ -1,7 +1,5 @@
package com.readrops.readropslibrary.services.nextcloudnews;
import android.util.TimingLogger;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -21,6 +19,8 @@ public class NextNewsAPI {
private static final String TAG = NextNewsAPI.class.getSimpleName();
private NextNewsService api;
public NextNewsAPI() {
}
@ -37,44 +37,84 @@ public class NextNewsAPI {
HttpManager httpManager = new HttpManager(credentials);
Retrofit retrofit = getConfiguredRetrofitInstance(httpManager);
NextNewsService api = retrofit.create(NextNewsService.class);
api = retrofit.create(NextNewsService.class);
SyncResult syncResult = new SyncResult();
TimingLogger timings = new TimingLogger(TAG, "sync");
switch (syncType) {
case INITIAL_SYNC:
initialSync(syncResult);
break;
case CLASSIC_SYNC:
if (data == null)
throw new NullPointerException("SyncData can't be null");
classicSync(syncResult, data);
break;
}
return syncResult;
}
private void initialSync(SyncResult syncResult) throws IOException {
getFeedsAndFolders(syncResult);
Response<NextNewsItems> itemsResponse = api.getItems(3, false, -1).execute();
NextNewsItems itemList = itemsResponse.body();
if (!itemsResponse.isSuccessful())
syncResult.setError(true);
if (itemList != null)
syncResult.setItems(itemList.getItems());
}
private void classicSync(SyncResult syncResult, SyncData data) throws IOException {
putModifiedItems(data, syncResult);
getFeedsAndFolders(syncResult);
Response<NextNewsItems> itemsResponse = api.getNewItems(data.getLastModified(), 3).execute();
NextNewsItems itemList = itemsResponse.body();
if (!itemsResponse.isSuccessful())
syncResult.setError(true);
if (itemList != null)
syncResult.setItems(itemList.getItems());
}
private void getFeedsAndFolders(SyncResult syncResult) throws IOException {
Response<NextNewsFeeds> feedResponse = api.getFeeds().execute();
NextNewsFeeds feedList = feedResponse.body();
timings.addSplit("get feeds");
if (!feedResponse.isSuccessful())
syncResult.setError(true);
Response<NextNewsFolders> folderResponse = api.getFolders().execute();
NextNewsFolders folderList = folderResponse.body();
timings.addSplit("get folders");
if (!folderResponse.isSuccessful())
syncResult.setError(true);
Response<NextNewsItems> itemsResponse = api.getItems(3, false, -1).execute();
NextNewsItems itemList = itemsResponse.body();
timings.addSplit("get items");
if (!itemsResponse.isSuccessful())
syncResult.setError(true);
timings.dumpToLog();
if (feedList.getFeeds() != null)
syncResult.setFeeds(feedList.getFeeds());
if (folderList.getFolders() != null)
if (folderList != null)
syncResult.setFolders(folderList.getFolders());
if (itemList.getItems() != null)
syncResult.setItems(itemList.getItems());
if (feedList != null)
syncResult.setFeeds(feedList.getFeeds());
return syncResult;
}
private void putModifiedItems(SyncData data, SyncResult syncResult) throws IOException {
Response readItemsResponse = api.setArticlesState(StateType.READ.name(),
data.getReadItems()).execute();
Response unreadItemsResponse = api.setArticlesState(StateType.UNREAD.toString(),
data.getUnreadItems()).execute();
if (!readItemsResponse.isSuccessful())
syncResult.setError(true);
if (!unreadItemsResponse.isSuccessful())
syncResult.setError(true);
}
public enum SyncType {

View File

@ -13,6 +13,8 @@ public class SyncData {
private List<Integer> unstarredItems;
private long lastModified;
public SyncData() {
unreadItems = new ArrayList<>();
readItems = new ArrayList<>();
@ -51,4 +53,12 @@ public class SyncData {
public List<Integer> getUnstarredItems() {
return unstarredItems;
}
public long getLastModified() {
return lastModified;
}
public void setLastModified(long lastModified) {
this.lastModified = lastModified;
}
}

View File

@ -0,0 +1,19 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
public class NextNewsUser {
private String userId;
private String displayName;
private long lastLoginTimestamp;
private Avatar avatar;
public class Avatar {
private String data;
private String mime;
}
}