Skip to content

Commit

Permalink
Move actual work from OperationsHelper to OperationsWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
di72nn committed Mar 10, 2020
1 parent e661486 commit 2a6334e
Show file tree
Hide file tree
Showing 14 changed files with 561 additions and 542 deletions.
446 changes: 10 additions & 436 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/OperationsHelper.java

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/QueueHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void dequeueItems(Collection<QueueItem> items) {
}
}

void changeArticle(int articleID, ArticleChangeType articleChangeType) {
public void changeArticle(int articleID, ArticleChangeType articleChangeType) {
Log.d(TAG, String.format("changeArticle(%d, %s) started", articleID, articleChangeType));

ArticleChangeItem item = new QueueItem(Action.ARTICLE_CHANGE)
Expand All @@ -218,7 +218,7 @@ void changeArticle(int articleID, ArticleChangeType articleChangeType) {
Log.d(TAG, "changeArticle() finished");
}

void deleteTagsFromArticle(int articleID, Collection<String> tags) {
public void deleteTagsFromArticle(int articleID, Collection<String> tags) {
Log.d(TAG, String.format("deleteTagsFromArticle(%d, %s) started",
articleID, tags.toString()));

Expand All @@ -232,7 +232,7 @@ void deleteTagsFromArticle(int articleID, Collection<String> tags) {
Log.d(TAG, "deleteTagsFromArticle() finished");
}

void addAnnotationToArticle(int articleId, long annotationId) {
public void addAnnotationToArticle(int articleId, long annotationId) {
Log.d(TAG, String.format("addAnnotationToArticle(%d, %d) started", articleId, annotationId));

AddOrUpdateAnnotationItem item = new QueueItem(Action.ANNOTATION_ADD)
Expand All @@ -245,7 +245,7 @@ void addAnnotationToArticle(int articleId, long annotationId) {
Log.d(TAG, "addAnnotationToArticle() finished");
}

void updateAnnotationOnArticle(int articleId, long annotationId) {
public void updateAnnotationOnArticle(int articleId, long annotationId) {
Log.d(TAG, String.format("updateAnnotationOnArticle(%d, %d) started",
articleId, annotationId));

Expand All @@ -259,7 +259,7 @@ void updateAnnotationOnArticle(int articleId, long annotationId) {
Log.d(TAG, "updateAnnotationOnArticle() finished");
}

void deleteAnnotationFromArticle(int articleId, int remoteAnnotationId) {
public void deleteAnnotationFromArticle(int articleId, int remoteAnnotationId) {
Log.d(TAG, String.format("deleteAnnotationFromArticle(%d, %d) started",
articleId, remoteAnnotationId));

Expand All @@ -273,7 +273,7 @@ void deleteAnnotationFromArticle(int articleId, int remoteAnnotationId) {
Log.d(TAG, "deleteAnnotationFromArticle() finished");
}

void deleteArticle(int articleID) {
public void deleteArticle(int articleID) {
Log.d(TAG, String.format("deleteArticle(%d) started", articleID));

ArticleDeleteItem item = new QueueItem(Action.ARTICLE_DELETE)
Expand All @@ -285,7 +285,7 @@ void deleteArticle(int articleID) {
Log.d(TAG, "deleteArticle() finished");
}

void addLink(String link, String origin) {
public void addLink(String link, String origin) {
Log.d(TAG, String.format("addLink(%s, %s) started", link, origin));

AddLinkItem item = new QueueItem(Action.ADD_LINK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Context;

import fr.gaulupeau.apps.Poche.data.OperationsHelper;

public class AddArticleTask extends GenericFieldsTask {

public AddArticleTask(String url, String originUrl) {
Expand All @@ -13,7 +11,7 @@ public AddArticleTask(String url, String originUrl) {

@Override
public void run(Context context) {
OperationsHelper.addArticleBG(genericStringField1, genericStringField2);
new OperationsWorker(context).addArticle(genericStringField1, genericStringField2);
}

// Parcelable implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static fr.gaulupeau.apps.Poche.events.EventHelper.postStickyEvent;
import static fr.gaulupeau.apps.Poche.events.EventHelper.removeStickyEvent;

public class ArticleAsFileDownloader extends BaseWorker {
public class ArticleAsFileDownloader extends BaseNetworkWorker {

private static final String TAG = ArticleAsFileDownloader.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.os.Parcel;

import fr.gaulupeau.apps.Poche.data.OperationsHelper;
import fr.gaulupeau.apps.Poche.data.dao.entities.QueueItem;

import static fr.gaulupeau.apps.Poche.service.ParcelableUtils.readEnum;
Expand Down Expand Up @@ -38,17 +37,19 @@ protected ArticleChangeTask(int articleId, QueueItem.ArticleChangeType articleCh

@Override
public void run(Context context) {
OperationsWorker operationsWorker = new OperationsWorker(context);

switch (articleChangeType) {
case ARCHIVE:
OperationsHelper.archiveArticleBG(genericIntField1, genericBooleanField1);
operationsWorker.archiveArticle(genericIntField1, genericBooleanField1);
return;

case FAVORITE:
OperationsHelper.favoriteArticleBG(genericIntField1, genericBooleanField1);
operationsWorker.favoriteArticle(genericIntField1, genericBooleanField1);
return;

case TITLE:
OperationsHelper.changeArticleTitleBG(genericIntField1, genericStringField1);
operationsWorker.changeArticleTitle(genericIntField1, genericStringField1);
return;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static fr.gaulupeau.apps.Poche.events.EventHelper.postStickyEvent;
import static fr.gaulupeau.apps.Poche.events.EventHelper.removeStickyEvent;

public class ArticleImagesFetcher extends BaseWorker {
public class ArticleImagesFetcher extends BaseNetworkWorker {

private static final String TAG = ArticleImagesFetcher.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static fr.gaulupeau.apps.Poche.events.EventHelper.postStickyEvent;
import static fr.gaulupeau.apps.Poche.events.EventHelper.removeStickyEvent;

public class ArticleUpdater extends BaseWorker {
public class ArticleUpdater extends BaseNetworkWorker {

private static final String TAG = ArticleUpdater.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;
import android.util.Log;

import java.io.IOException;

import fr.gaulupeau.apps.Poche.network.Updater;
import fr.gaulupeau.apps.Poche.network.WallabagConnection;
import fr.gaulupeau.apps.Poche.network.exceptions.IncorrectConfigurationException;
import wallabag.apiwrapper.WallabagService;
import wallabag.apiwrapper.exceptions.AuthorizationException;
import wallabag.apiwrapper.exceptions.UnsuccessfulResponseException;

public class BaseNetworkWorker extends BaseWorker {

private static final String TAG = BaseNetworkWorker.class.getSimpleName();

public BaseNetworkWorker(Context context) {
super(context);
}

protected ActionResult processException(Exception e, String scope) {
ActionResult result = new ActionResult();

Log.w(TAG, String.format("%s %s", scope, e.getClass().getName()), e);

if (e instanceof UnsuccessfulResponseException) {
UnsuccessfulResponseException ure = (UnsuccessfulResponseException) e;
if (ure instanceof AuthorizationException) {
result.setErrorType(ActionResult.ErrorType.INCORRECT_CREDENTIALS);
result.setMessage(ure.getResponseBody()); // TODO: fix message
} else {
result.setErrorType(ure.getResponseCode() == 500
? ActionResult.ErrorType.SERVER_ERROR
: ActionResult.ErrorType.UNKNOWN);
result.setMessage(e.toString());
result.setException(e);
}
} else if (e instanceof IncorrectConfigurationException) {
result.setErrorType(ActionResult.ErrorType.INCORRECT_CONFIGURATION);
result.setMessage(e.getMessage());
} else if (e instanceof IOException) {
boolean handled = false;

if (getSettings().isConfigurationOk()) {
if (e instanceof java.net.UnknownHostException
|| e instanceof java.net.ConnectException // TODO: maybe filter by message
|| e instanceof java.net.SocketTimeoutException) {
result.setErrorType(ActionResult.ErrorType.TEMPORARY);
handled = true;
} else if (e instanceof javax.net.ssl.SSLException
&& e.getMessage() != null
&& e.getMessage().contains("Connection timed out")) {
result.setErrorType(ActionResult.ErrorType.TEMPORARY);
handled = true;
} else if (e instanceof java.net.SocketException
&& e.getMessage() != null
&& e.getMessage().contains("Software caused connection abort")) {
result.setErrorType(ActionResult.ErrorType.TEMPORARY);
handled = true;
}
}

if (!handled) {
result.setErrorType(ActionResult.ErrorType.UNKNOWN);
result.setMessage(e.toString());
result.setException(e);
}
// IOExceptions in most cases mean temporary error,
// in some cases may mean that the action was completed anyway.
} else if (e instanceof IllegalArgumentException && !getSettings().isConfigurationOk()) {
result.setErrorType(ActionResult.ErrorType.INCORRECT_CONFIGURATION);
result.setMessage(e.toString());
} else { // other exceptions meant to be handled outside
result.setErrorType(ActionResult.ErrorType.UNKNOWN);
result.setMessage(e.toString());
result.setException(e);
}

return result;
}

protected WallabagService getWallabagService()
throws IncorrectConfigurationException {
return WallabagConnection.getWallabagService();
}

protected Updater getUpdater() throws IncorrectConfigurationException {
return new Updater(getDaoSession(), getWallabagService());
}

}
81 changes: 0 additions & 81 deletions app/src/main/java/fr/gaulupeau/apps/Poche/service/BaseWorker.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;
import android.util.Log;

import java.io.IOException;

import fr.gaulupeau.apps.Poche.data.DbConnection;
import fr.gaulupeau.apps.Poche.data.Settings;
import fr.gaulupeau.apps.Poche.data.dao.DaoSession;
import fr.gaulupeau.apps.Poche.network.Updater;
import fr.gaulupeau.apps.Poche.network.WallabagConnection;
import fr.gaulupeau.apps.Poche.network.exceptions.IncorrectConfigurationException;
import wallabag.apiwrapper.WallabagService;
import wallabag.apiwrapper.exceptions.AuthorizationException;
import wallabag.apiwrapper.exceptions.UnsuccessfulResponseException;

public class BaseWorker {

private static final String TAG = BaseWorker.class.getSimpleName();

private final Context context;

private Settings settings;
Expand All @@ -29,67 +18,6 @@ public BaseWorker(Context context) {
this.context = context;
}

protected ActionResult processException(Exception e, String scope) {
ActionResult result = new ActionResult();

Log.w(TAG, String.format("%s %s", scope, e.getClass().getName()), e);

if (e instanceof UnsuccessfulResponseException) {
UnsuccessfulResponseException ure = (UnsuccessfulResponseException) e;
if (ure instanceof AuthorizationException) {
result.setErrorType(ActionResult.ErrorType.INCORRECT_CREDENTIALS);
result.setMessage(ure.getResponseBody()); // TODO: fix message
} else {
result.setErrorType(ure.getResponseCode() == 500
? ActionResult.ErrorType.SERVER_ERROR
: ActionResult.ErrorType.UNKNOWN);
result.setMessage(e.toString());
result.setException(e);
}
} else if (e instanceof IncorrectConfigurationException) {
result.setErrorType(ActionResult.ErrorType.INCORRECT_CONFIGURATION);
result.setMessage(e.getMessage());
} else if (e instanceof IOException) {
boolean handled = false;

if (getSettings().isConfigurationOk()) {
if (e instanceof java.net.UnknownHostException
|| e instanceof java.net.ConnectException // TODO: maybe filter by message
|| e instanceof java.net.SocketTimeoutException) {
result.setErrorType(ActionResult.ErrorType.TEMPORARY);
handled = true;
} else if (e instanceof javax.net.ssl.SSLException
&& e.getMessage() != null
&& e.getMessage().contains("Connection timed out")) {
result.setErrorType(ActionResult.ErrorType.TEMPORARY);
handled = true;
} else if (e instanceof java.net.SocketException
&& e.getMessage() != null
&& e.getMessage().contains("Software caused connection abort")) {
result.setErrorType(ActionResult.ErrorType.TEMPORARY);
handled = true;
}
}

if (!handled) {
result.setErrorType(ActionResult.ErrorType.UNKNOWN);
result.setMessage(e.toString());
result.setException(e);
}
// IOExceptions in most cases mean temporary error,
// in some cases may mean that the action was completed anyway.
} else if (e instanceof IllegalArgumentException && !getSettings().isConfigurationOk()) {
result.setErrorType(ActionResult.ErrorType.INCORRECT_CONFIGURATION);
result.setMessage(e.toString());
} else { // other exceptions meant to be handled outside
result.setErrorType(ActionResult.ErrorType.UNKNOWN);
result.setMessage(e.toString());
result.setException(e);
}

return result;
}

protected Context getContext() {
return context;
}
Expand All @@ -110,13 +38,4 @@ protected DaoSession getDaoSession() {
return daoSession;
}

protected WallabagService getWallabagService()
throws IncorrectConfigurationException {
return WallabagConnection.getWallabagService();
}

protected Updater getUpdater() throws IncorrectConfigurationException {
return new Updater(getDaoSession(), getWallabagService());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Context;

import fr.gaulupeau.apps.Poche.data.OperationsHelper;

public class DeleteArticleTask extends GenericFieldsTask {

public DeleteArticleTask(int articleId) {
Expand All @@ -12,7 +10,7 @@ public DeleteArticleTask(int articleId) {

@Override
public void run(Context context) {
OperationsHelper.deleteArticleBG(genericIntField1);
new OperationsWorker(context).deleteArticle(genericIntField1);
}

// Parcelable implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static fr.gaulupeau.apps.Poche.events.EventHelper.postStickyEvent;
import static fr.gaulupeau.apps.Poche.events.EventHelper.removeStickyEvent;

public class DeletedArticleSweeper extends BaseWorker {
public class DeletedArticleSweeper extends BaseNetworkWorker {

private static final String TAG = DeletedArticleSweeper.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static fr.gaulupeau.apps.Poche.events.EventHelper.postStickyEvent;
import static fr.gaulupeau.apps.Poche.events.EventHelper.removeStickyEvent;

public class OfflineChangesSynchronizer extends BaseWorker {
public class OfflineChangesSynchronizer extends BaseNetworkWorker {

private static final String TAG = OfflineChangesSynchronizer.class.getSimpleName();

Expand Down
Loading

0 comments on commit 2a6334e

Please sign in to comment.