sync now returns potential errors

This commit is contained in:
Shinokuni 2019-03-13 09:32:14 +01:00
parent 604c56ebc0
commit fc369888ac
7 changed files with 105 additions and 24 deletions

View File

@ -28,6 +28,7 @@ import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
import com.github.clans.fab.FloatingActionMenu;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.utils.SyncError;
import com.readrops.app.views.AddFeedDialog;
import com.readrops.app.views.MainItemListAdapter;
import com.readrops.app.viewmodels.MainViewModel;
@ -45,7 +46,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableCompletableObserver;
import io.reactivex.schedulers.Schedulers;
@ -259,9 +262,14 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S
viewModel.sync(feeds)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableCompletableObserver() {
.subscribe(new SingleObserver<List<SyncError>>() {
@Override
public void onComplete() {
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(List<SyncError> syncErrors) {
refreshLayout.setRefreshing(false);
adapter.submitList(newItems);
}

View File

@ -5,6 +5,7 @@ import android.os.Handler;
import android.os.Looper;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.utils.SyncError;
import com.readrops.app.views.SimpleCallback;
import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Feed;
@ -34,7 +35,7 @@ public abstract class ARepository {
this.callback = callback;
}
public abstract Completable sync(List<Feed> feeds);
public abstract Single<List<SyncError>> sync(List<Feed> feeds);
public abstract void addFeed(ParsingResult result);

View File

@ -1,5 +1,6 @@
package com.readrops.app.repositories;
import android.accounts.NetworkErrorException;
import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.graphics.Bitmap;
@ -11,11 +12,13 @@ import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Item;
import com.readrops.app.utils.SyncError;
import com.readrops.app.utils.Utils;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ParsingResult;
import com.readrops.readropslibrary.QueryCallback;
import com.readrops.readropslibrary.Utils.LibUtils;
import com.readrops.readropslibrary.Utils.UnknownFormatException;
import com.readrops.readropslibrary.localfeed.AFeed;
import com.readrops.readropslibrary.localfeed.RSSQuery;
import com.readrops.readropslibrary.localfeed.RSSQueryResult;
@ -55,8 +58,8 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
}
@Override
public Completable sync(List<Feed> feeds) {
return Completable.create(emitter -> {
public Single<List<SyncError>> sync(List<Feed> feeds) {
return Single.create(emitter -> {
List<Feed> feedList;
if (feeds == null || feeds.size() == 0)
@ -65,9 +68,11 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
feedList = new ArrayList<>(feeds);
RSSQuery rssQuery = new RSSQuery();
rssQuery.setCallback(this);
List<SyncError> syncErrors = new ArrayList<>();
for (Feed feed : feedList) {
SyncError syncError = new SyncError();
try {
HashMap<String, String> headers = new HashMap<>();
if (feed.getEtag() != null)
@ -78,13 +83,29 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
RSSQueryResult queryResult = rssQuery.queryUrl(feed.getUrl(), headers);
if (queryResult != null && queryResult.getException() == null)
insertNewItems(queryResult.getFeed(), queryResult.getRssType());
else if (queryResult != null && queryResult.getException() != null) {
Exception e = queryResult.getException();
} catch (ParseException e) {
emitter.onError(e);
if (e instanceof UnknownFormatException)
syncError.setInsertionError(SyncError.FeedInsertionError.FORMAT_ERROR);
else if (e instanceof NetworkErrorException)
syncError.setInsertionError(SyncError.FeedInsertionError.NETWORK_ERROR);
syncError.setFeed(feed);
syncErrors.add(syncError);
}
} catch (Exception e) {
if (e instanceof IOException)
syncError.setInsertionError(SyncError.FeedInsertionError.NETWORK_ERROR);
else
syncError.setInsertionError(SyncError.FeedInsertionError.PARSE_ERROR);
syncError.setFeed(feed);
syncErrors.add(syncError);
}
}
emitter.onComplete();
emitter.onSuccess(syncErrors);
});
}
@ -92,10 +113,9 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
public void addFeed(ParsingResult result) {
executor.execute(() -> {
try {
RSSQuery rssNet = new RSSQuery();
rssNet.setCallback(this);
RSSQuery rssQuery = new RSSQuery();
RSSQueryResult queryResult = rssNet.queryUrl(result.getUrl(), new HashMap<>());
RSSQueryResult queryResult = rssQuery.queryUrl(result.getUrl(), new HashMap<>());
if (queryResult != null && queryResult.getException() == null) {
insertFeed(queryResult.getFeed(), queryResult.getRssType());
}
@ -114,9 +134,8 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
for (ParsingResult result : results) {
RSSQuery rssNet = new RSSQuery();
rssNet.setCallback(this);
RSSQueryResult queryResult = rssNet.queryUrl(result.getUrl(), new HashMap<>());
if (queryResult != null && queryResult.getException() == null) {
Feed feed = insertFeed(queryResult.getFeed(), queryResult.getRssType());
if (feed != null)

View File

@ -0,0 +1,41 @@
package com.readrops.app.utils;
import com.readrops.app.database.entities.Feed;
public class SyncError {
private Feed feed;
private FeedInsertionError insertionError;
public SyncError() {
}
public SyncError(Feed feed, FeedInsertionError insertionError) {
this.feed = feed;
this.insertionError = insertionError;
}
public Feed getFeed() {
return feed;
}
public void setFeed(Feed feed) {
this.feed = feed;
}
public FeedInsertionError getInsertionError() {
return insertionError;
}
public void setInsertionError(FeedInsertionError insertionError) {
this.insertionError = insertionError;
}
public enum FeedInsertionError {
NETWORK_ERROR,
DB_ERROR,
PARSE_ERROR,
FORMAT_ERROR
}
}

View File

@ -8,12 +8,14 @@ import android.support.annotation.NonNull;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.utils.SyncError;
import com.readrops.app.views.SimpleCallback;
import com.readrops.app.utils.ParsingResult;
import java.util.List;
import io.reactivex.Completable;
import io.reactivex.Single;
public class MainViewModel extends AndroidViewModel {
@ -36,7 +38,7 @@ public class MainViewModel extends AndroidViewModel {
repository.setCallback(simpleCallback);
}
public Completable sync(List<Feed> feeds) {
public Single<List<SyncError>> sync(List<Feed> feeds) {
return repository.sync(feeds);
}

View File

@ -0,0 +1,12 @@
package com.readrops.readropslibrary.Utils;
public class UnknownFormatException extends Exception {
public UnknownFormatException() {
}
public UnknownFormatException(String message) {
super(message);
}
}

View File

@ -1,10 +1,12 @@
package com.readrops.readropslibrary.localfeed;
import android.accounts.NetworkErrorException;
import android.util.Log;
import com.google.gson.Gson;
import com.readrops.readropslibrary.QueryCallback;
import com.readrops.readropslibrary.Utils.LibUtils;
import com.readrops.readropslibrary.Utils.UnknownFormatException;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
@ -20,6 +22,8 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@ -32,8 +36,6 @@ public class RSSQuery {
private static final String RSS_2_REGEX = "rss.*version=\"2.0\"";
private QueryCallback callback;
/**
* Request the url given in parameter.
* This method is synchronous, <b>it has to be called from another thread than the main one</b>.
@ -49,7 +51,7 @@ public class RSSQuery {
RSSType type = getRSSType(header);
if (type == null) {
queryResult = new RSSQueryResult(new IllegalArgumentException("bad content type : " + header + "for " + url));
queryResult = new RSSQueryResult(new UnknownFormatException("bad content type : " + header + "for " + url));
return queryResult;
}
@ -57,7 +59,7 @@ public class RSSQuery {
} else if (response.code() == 304)
return null;
else
return new RSSQueryResult(new Exception("Error " + response.code() + " when requesting url " + url));
return new RSSQueryResult(new NetworkErrorException("Error " + response.code() + " when requesting url " + url));
}
public boolean isUrlFeedLink(String url) throws IOException {
@ -133,7 +135,7 @@ public class RSSQuery {
if (type == RSSType.RSS_UNKNOWN) {
RSSType contentType = getContentRSSType(xml);
if (contentType == RSSType.RSS_UNKNOWN) {
return new RSSQueryResult(new Exception("Unknown content format"));
return new RSSQueryResult(new UnknownFormatException("Unknown content format"));
} else
type = contentType;
}
@ -192,10 +194,6 @@ public class RSSQuery {
return type;
}
public void setCallback(QueryCallback callback) {
this.callback = callback;
}
public enum RSSType {
RSS_2("rss"),
RSS_ATOM("atom"),