Fix authentification failure, sort items by date and add better handling for null values

This commit is contained in:
Shinokuni 2019-05-12 22:42:45 +02:00
parent 92ce491771
commit ac030c146f
5 changed files with 83 additions and 37 deletions

View File

@ -1,21 +1,9 @@
package com.readrops.app.activities; package com.readrops.app.activities;
import androidx.lifecycle.ViewModelProviders;
import androidx.paging.PagedList;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import android.util.Log; import android.util.Log;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Menu; import android.view.Menu;
@ -26,6 +14,19 @@ import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.lifecycle.ViewModelProviders;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader; import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
@ -453,7 +454,9 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
e.printStackTrace();
refreshLayout.setRefreshing(false); refreshLayout.setRefreshing(false);
syncProgressLayout.setVisibility(View.GONE);
Toast.makeText(getApplication(), e.getMessage(), Toast.LENGTH_LONG).show(); Toast.makeText(getApplication(), e.getMessage(), Toast.LENGTH_LONG).show();
} }

View File

@ -14,7 +14,7 @@ import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.Utils; import com.readrops.app.utils.Utils;
import com.readrops.readropslibrary.services.nextcloudnews.Credentials; import com.readrops.readropslibrary.services.nextcloudnews.Credentials;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsAPI; import com.readrops.readropslibrary.services.nextcloudnews.NextNewsAPI;
import com.readrops.readropslibrary.services.nextcloudnews.SyncResults; import com.readrops.readropslibrary.services.nextcloudnews.SyncResult;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItem; import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItem;
@ -22,6 +22,7 @@ import com.readrops.readropslibrary.utils.LibUtils;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import io.reactivex.Completable; import io.reactivex.Completable;
@ -43,21 +44,25 @@ public class NextNewsRepository extends ARepository {
try { try {
NextNewsAPI newsAPI = new NextNewsAPI(); NextNewsAPI newsAPI = new NextNewsAPI();
Credentials credentials = new Credentials("", LibUtils.NEXTCLOUD_PASSWORD, ""); Credentials credentials = new Credentials("Lucas", LibUtils.NEXTCLOUD_PASSWORD, "https://cloud.openserver.fr");
SyncResults results = newsAPI.sync(credentials, NextNewsAPI.SyncType.INITIAL_SYNC, null); SyncResult syncResult = newsAPI.sync(credentials, NextNewsAPI.SyncType.INITIAL_SYNC, null);
TimingLogger timings = new TimingLogger(TAG, "sync"); if (!syncResult.isError()) {
insertFolders(results.getFolders()); TimingLogger timings = new TimingLogger(TAG, "sync");
timings.addSplit("insert folders"); insertFolders(syncResult.getFolders());
timings.addSplit("insert folders");
insertFeeds(results.getFeeds()); insertFeeds(syncResult.getFeeds());
timings.addSplit("insert feeds"); timings.addSplit("insert feeds");
insertItems(results.getItems()); insertItems(syncResult.getItems());
timings.addSplit("insert items"); timings.addSplit("insert items");
timings.dumpToLog(); timings.dumpToLog();
emitter.onComplete(); emitter.onComplete();
} else
emitter.onError(new Throwable());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
emitter.onError(e); emitter.onError(e);
@ -155,6 +160,7 @@ public class NextNewsRepository extends ARepository {
} }
} }
Collections.sort(newItems, Item::compareTo);
database.itemDao().insert(newItems); database.itemDao().insert(newItems);
} }
} }

View File

@ -13,6 +13,7 @@ import com.readrops.readropslibrary.utils.HttpManager;
import java.io.IOException; import java.io.IOException;
import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
@ -32,30 +33,48 @@ public class NextNewsAPI {
.build(); .build();
} }
public SyncResults sync(@NonNull Credentials credentials, @NonNull SyncType syncType, @Nullable SyncData data) throws IOException { public SyncResult sync(@NonNull Credentials credentials, @NonNull SyncType syncType, @Nullable SyncData data) throws IOException {
HttpManager httpManager = new HttpManager(credentials); HttpManager httpManager = new HttpManager(credentials);
Retrofit retrofit = getConfiguredRetrofitInstance(httpManager); Retrofit retrofit = getConfiguredRetrofitInstance(httpManager);
NextNewsService api = retrofit.create(NextNewsService.class); NextNewsService api = retrofit.create(NextNewsService.class);
SyncResult syncResult = new SyncResult();
TimingLogger timings = new TimingLogger(TAG, "sync"); TimingLogger timings = new TimingLogger(TAG, "sync");
NextNewsFeeds feedList = api.getFeeds().execute().body(); Response<NextNewsFeeds> feedResponse = api.getFeeds().execute();
NextNewsFeeds feedList = feedResponse.body();
timings.addSplit("get feeds"); timings.addSplit("get feeds");
NextNewsFolders folderList = api.getFolders().execute().body(); if (!feedResponse.isSuccessful())
syncResult.setError(true);
Response<NextNewsFolders> folderResponse = api.getFolders().execute();
NextNewsFolders folderList = folderResponse.body();
timings.addSplit("get folders"); timings.addSplit("get folders");
NextNewsItems itemList = api.getItems(3, false, 300).execute().body(); if (!folderResponse.isSuccessful())
syncResult.setError(true);
Response<NextNewsItems> itemsResponse = api.getItems(3, false, -1).execute();
NextNewsItems itemList = itemsResponse.body();
timings.addSplit("get items"); timings.addSplit("get items");
if (!itemsResponse.isSuccessful())
syncResult.setError(true);
timings.dumpToLog(); timings.dumpToLog();
SyncResults results = new SyncResults(); if (feedList.getFeeds() != null)
results.setFeeds(feedList.getFeeds()); syncResult.setFeeds(feedList.getFeeds());
results.setFolders(folderList.getFolders());
results.setItems(itemList.getItems());
return results; if (folderList.getFolders() != null)
syncResult.setFolders(folderList.getFolders());
if (itemList.getItems() != null)
syncResult.setItems(itemList.getItems());
return syncResult;
} }
public enum SyncType { public enum SyncType {

View File

@ -6,7 +6,7 @@ import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItem;
import java.util.List; import java.util.List;
public class SyncResults { public class SyncResult {
private List<NextNewsFolder> folders; private List<NextNewsFolder> folders;
@ -14,7 +14,9 @@ public class SyncResults {
private List<NextNewsItem> items; private List<NextNewsItem> items;
public SyncResults() { private boolean error;
public SyncResult() {
} }
public List<NextNewsFolder> getFolders() { public List<NextNewsFolder> getFolders() {
@ -40,4 +42,12 @@ public class SyncResults {
public void setItems(List<NextNewsItem> items) { public void setItems(List<NextNewsItem> items) {
this.items = items; this.items = items;
} }
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
} }

View File

@ -19,7 +19,7 @@ public class HttpManager {
this.credentials = credentials; this.credentials = credentials;
okHttpClient = HttpBuilder.getBuilder() okHttpClient = HttpBuilder.getBuilder()
.callTimeout(20, TimeUnit.SECONDS) .callTimeout(30, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.HOURS) .readTimeout(1, TimeUnit.HOURS)
.addInterceptor(new AuthInterceptor()) .addInterceptor(new AuthInterceptor())
.build(); .build();
@ -35,11 +35,19 @@ public class HttpManager {
public class AuthInterceptor implements Interceptor { public class AuthInterceptor implements Interceptor {
public AuthInterceptor() {
}
@Override @Override
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
Request request = chain.request(); Request request = chain.request();
request = request.newBuilder() // AuthInterceptor is called twice when using Okhttp for the second time and I don't know why,
// which adds the authorization header twice and make the auth fail
// So preventively, I delete the first added header
// TODO : find why AuthInterceptor is called twice
request = request.newBuilder().removeHeader("Authorization")
.addHeader("Authorization", credentials.getBase64()) .addHeader("Authorization", credentials.getBase64())
.build(); .build();