mirror of https://github.com/ThmmyNoLife/mTHMMY
Thodoris1999
6 years ago
12 changed files with 424 additions and 159 deletions
@ -1,62 +1,39 @@ |
|||||
package gr.thmmy.mthmmy.activities.topic.tasks; |
package gr.thmmy.mthmmy.activities.topic.tasks; |
||||
|
|
||||
import android.os.AsyncTask; |
import org.jsoup.nodes.Document; |
||||
|
|
||||
import java.io.IOException; |
import java.io.IOException; |
||||
|
|
||||
import gr.thmmy.mthmmy.activities.topic.Posting; |
import gr.thmmy.mthmmy.utils.NetworkResultCodes; |
||||
import gr.thmmy.mthmmy.base.BaseApplication; |
import gr.thmmy.mthmmy.utils.NetworkTask; |
||||
import okhttp3.OkHttpClient; |
import okhttp3.OkHttpClient; |
||||
import okhttp3.Request; |
import okhttp3.Request; |
||||
import okhttp3.Response; |
import okhttp3.Response; |
||||
import timber.log.Timber; |
|
||||
|
|
||||
public class DeleteTask extends AsyncTask<String, Void, Boolean> { |
public class DeleteTask extends NetworkTask<Void> { |
||||
private DeleteTaskCallbacks listener; |
|
||||
|
|
||||
public DeleteTask(DeleteTaskCallbacks listener) { |
public DeleteTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<Void> onParseTaskFinishedListener) { |
||||
this.listener = listener; |
super(onParseTaskStartedListener, onParseTaskFinishedListener); |
||||
} |
} |
||||
|
|
||||
@Override |
@Override |
||||
protected void onPreExecute() { |
protected Response sendRequest(OkHttpClient client, String... input) throws IOException { |
||||
listener.onDeleteTaskStarted(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected Boolean doInBackground(String... args) { |
|
||||
Request delete = new Request.Builder() |
Request delete = new Request.Builder() |
||||
.url(args[0]) |
.url(input[0]) |
||||
.header("User-Agent", |
.header("User-Agent", |
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36") |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36") |
||||
.build(); |
.build(); |
||||
|
|
||||
try { |
|
||||
OkHttpClient client = BaseApplication.getInstance().getClient(); |
|
||||
client.newCall(delete).execute(); |
client.newCall(delete).execute(); |
||||
Response response = client.newCall(delete).execute(); |
return client.newCall(delete).execute(); |
||||
//Response response = client.newCall(delete).execute();
|
|
||||
switch (Posting.replyStatus(response)) { |
|
||||
case SUCCESSFUL: |
|
||||
BaseApplication.getInstance().logFirebaseAnalyticsEvent("post_deletion", null); |
|
||||
return true; |
|
||||
default: |
|
||||
Timber.e("Something went wrong. Request string: %s", delete.toString()); |
|
||||
return false; |
|
||||
} |
|
||||
} catch (IOException e) { |
|
||||
Timber.e(e, "Delete failed."); |
|
||||
return false; |
|
||||
} |
|
||||
} |
} |
||||
|
|
||||
@Override |
@Override |
||||
protected void onPostExecute(Boolean result) { |
protected Void performTask(Document document) { |
||||
listener.onDeleteTaskFinished(result); |
return null; |
||||
} |
} |
||||
|
|
||||
public interface DeleteTaskCallbacks { |
@Override |
||||
void onDeleteTaskStarted(); |
protected int getResultCode(Response response, Void data) { |
||||
void onDeleteTaskFinished(boolean result); |
return NetworkResultCodes.SUCCESSFUL; |
||||
} |
} |
||||
} |
} |
||||
|
@ -0,0 +1,80 @@ |
|||||
|
package gr.thmmy.mthmmy.utils; |
||||
|
|
||||
|
import android.os.AsyncTask; |
||||
|
|
||||
|
public abstract class ExternalAsyncTask<U, V> extends AsyncTask<U, Void, V> { |
||||
|
|
||||
|
protected OnParseTaskStartedListener onParseTaskStartedListener; |
||||
|
protected OnParseTaskCancelledListener onParseTaskCancelledListener; |
||||
|
protected OnParseTaskFinishedListener<V> onParseTaskFinishedListener; |
||||
|
|
||||
|
@Override |
||||
|
protected void onPreExecute() { |
||||
|
if (onParseTaskStartedListener != null) |
||||
|
onParseTaskStartedListener.onParseStart(); |
||||
|
else |
||||
|
super.onPreExecute(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onCancelled() { |
||||
|
if (onParseTaskCancelledListener != null) |
||||
|
onParseTaskCancelledListener.onParseCancel(); |
||||
|
else |
||||
|
super.onCancelled(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onCancelled(V v) { |
||||
|
if (onParseTaskCancelledListener != null) |
||||
|
onParseTaskCancelledListener.onParseCancel(); |
||||
|
else |
||||
|
super.onCancelled(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onPostExecute(V v) { |
||||
|
if (onParseTaskFinishedListener != null) |
||||
|
onParseTaskFinishedListener.onParseFinish(v); |
||||
|
else |
||||
|
super.onPostExecute(v); |
||||
|
} |
||||
|
|
||||
|
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, |
||||
|
OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskStartedListener = onParseTaskStartedListener; |
||||
|
this.onParseTaskCancelledListener = onParseTaskCancelledListener; |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskStartedListener = onParseTaskStartedListener; |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public ExternalAsyncTask() { } |
||||
|
|
||||
|
public void setOnParseTaskStartedListener(OnParseTaskStartedListener onParseTaskStartedListener) { |
||||
|
this.onParseTaskStartedListener = onParseTaskStartedListener; |
||||
|
} |
||||
|
|
||||
|
public void setOnParseTaskCancelledListener(OnParseTaskCancelledListener onParseTaskCancelledListener) { |
||||
|
this.onParseTaskCancelledListener = onParseTaskCancelledListener; |
||||
|
} |
||||
|
|
||||
|
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskStartedListener { |
||||
|
void onParseStart(); |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskCancelledListener { |
||||
|
void onParseCancel(); |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskFinishedListener<V> { |
||||
|
void onParseFinish(V result); |
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package gr.thmmy.mthmmy.utils; |
||||
|
|
||||
|
public class NetworkResultCodes { |
||||
|
/** |
||||
|
* The request was successful |
||||
|
*/ |
||||
|
public static final int SUCCESSFUL = 0; |
||||
|
/** |
||||
|
* Error 404, page was not found |
||||
|
*/ |
||||
|
public static final int NOT_FOUND = 1; |
||||
|
/** |
||||
|
* User session ended while posting the reply |
||||
|
*/ |
||||
|
public static final int SESSION_ENDED = 2; |
||||
|
/** |
||||
|
* Exception occured while parsing |
||||
|
*/ |
||||
|
public static final int PARSE_ERROR = 3; |
||||
|
/** |
||||
|
* Other undefined of unidentified error |
||||
|
*/ |
||||
|
public static final int OTHER_ERROR = 4; |
||||
|
/** |
||||
|
* Failed to connect to thmmy.gr |
||||
|
*/ |
||||
|
public static final int NETWORK_ERROR = 5; |
||||
|
/** |
||||
|
* Error while excecuting NetworkTask's performTask() |
||||
|
*/ |
||||
|
public static final int PERFORM_TASK_ERROR = 6; |
||||
|
} |
@ -0,0 +1,91 @@ |
|||||
|
package gr.thmmy.mthmmy.utils; |
||||
|
|
||||
|
import org.jsoup.Jsoup; |
||||
|
import org.jsoup.nodes.Document; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
import gr.thmmy.mthmmy.base.BaseApplication; |
||||
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
||||
|
import okhttp3.OkHttpClient; |
||||
|
import okhttp3.Request; |
||||
|
import okhttp3.Response; |
||||
|
import timber.log.Timber; |
||||
|
|
||||
|
public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>> { |
||||
|
|
||||
|
protected OnParseTaskFinishedListener<T> onParseTaskFinishedListener; |
||||
|
|
||||
|
public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, |
||||
|
OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { |
||||
|
super(onParseTaskStartedListener, onParseTaskCancelledListener, null); |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { |
||||
|
super(onParseTaskStartedListener, null); |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public NetworkTask() {} |
||||
|
|
||||
|
@Override |
||||
|
protected final Parcel<T> doInBackground(String... input) { |
||||
|
Response response; |
||||
|
try { |
||||
|
response = sendRequest(BaseApplication.getInstance().getClient(), input); |
||||
|
} catch (IOException e) { |
||||
|
Timber.e(e, "Error connecting to thmmy.gr"); |
||||
|
return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); |
||||
|
} |
||||
|
String responseBodyString; |
||||
|
try { |
||||
|
responseBodyString = response.body().string(); |
||||
|
} catch (NullPointerException npe) { |
||||
|
Timber.wtf(npe, "Invalid response. Detatails: https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html#body--"); |
||||
|
return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); |
||||
|
} catch (IOException e) { |
||||
|
Timber.e(e, "Error getting response body string"); |
||||
|
return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); |
||||
|
} |
||||
|
try { |
||||
|
T data = performTask(Jsoup.parse(responseBodyString)); |
||||
|
int resultCode = getResultCode(response, data); |
||||
|
return new Parcel<>(resultCode, data); |
||||
|
} catch (ParseException pe) { |
||||
|
Timber.e(pe); |
||||
|
return new Parcel<>(NetworkResultCodes.PARSE_ERROR, null); |
||||
|
} catch (Exception e) { |
||||
|
Timber.e(e); |
||||
|
return new Parcel<>(NetworkResultCodes.PERFORM_TASK_ERROR, null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onPostExecute(Parcel<T> tParcel) { |
||||
|
if (onParseTaskFinishedListener != null) |
||||
|
onParseTaskFinishedListener.onParseFinish(tParcel.getResultCode(), tParcel.getData()); |
||||
|
else |
||||
|
super.onPostExecute(tParcel); |
||||
|
} |
||||
|
|
||||
|
protected Response sendRequest(OkHttpClient client, String... input) throws IOException { |
||||
|
String url = input[0]; |
||||
|
Request request = new Request.Builder() |
||||
|
.url(url) |
||||
|
.build(); |
||||
|
return client.newCall(request).execute(); |
||||
|
} |
||||
|
|
||||
|
protected abstract T performTask(Document document); |
||||
|
|
||||
|
protected abstract int getResultCode(Response response, T data); |
||||
|
|
||||
|
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskFinishedListener<T> { |
||||
|
void onParseFinish(int resultCode, T data); |
||||
|
} |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package gr.thmmy.mthmmy.utils; |
||||
|
|
||||
|
public class Parcel<T> { |
||||
|
|
||||
|
private int resultCode; |
||||
|
private T data; |
||||
|
|
||||
|
public Parcel(int resultCode, T data) { |
||||
|
this.resultCode = resultCode; |
||||
|
this.data = data; |
||||
|
} |
||||
|
|
||||
|
public int getResultCode() { |
||||
|
return resultCode; |
||||
|
} |
||||
|
|
||||
|
public T getData() { |
||||
|
return data; |
||||
|
} |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
|
import org.jsoup.nodes.Document; |
||||
|
|
||||
|
import gr.thmmy.mthmmy.utils.NetworkTask; |
||||
|
|
||||
|
public abstract class NewParseTask<T> extends NetworkTask<T> { |
||||
|
|
||||
|
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, |
||||
|
OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { |
||||
|
super(onParseTaskStartedListener, onParseTaskCancelledListener, onParseTaskFinishedListener); |
||||
|
} |
||||
|
|
||||
|
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { |
||||
|
super(onParseTaskStartedListener, onParseTaskFinishedListener); |
||||
|
} |
||||
|
|
||||
|
public NewParseTask() {} |
||||
|
|
||||
|
@Override |
||||
|
protected final T performTask(Document document) { |
||||
|
try { |
||||
|
return parse(document); |
||||
|
} catch (Exception e) { |
||||
|
throw new ParseException("Parse failed.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected abstract T parse (Document document) throws ParseException; |
||||
|
} |
Loading…
Reference in new issue