Browse Source

improvements and implement parse task in delete post

pull/54/head
Thodoris1999 6 years ago
parent
commit
4938597f2e
  1. 29
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 53
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java
  3. 19
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java
  5. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java
  6. 16
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

29
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -12,7 +12,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDelegate;
@ -39,7 +38,6 @@ import android.widget.Toast;
import java.util.ArrayList;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.EditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply;
@ -52,6 +50,7 @@ import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager;
import gr.thmmy.mthmmy.utils.HTMLUtils;
import gr.thmmy.mthmmy.utils.parsing.Parcel;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import gr.thmmy.mthmmy.viewmodel.TopicViewModel;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -498,23 +497,15 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
progressBar.setVisibility(ProgressBar.GONE);
}
});
viewModel.setDeleteTaskCallbacks(new DeleteTask.DeleteTaskCallbacks() {
@Override
public void onDeleteTaskStarted() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
public void onDeleteTaskFinished(boolean result) {
progressBar.setVisibility(ProgressBar.GONE);
if (result) {
Timber.i("Post deleted successfully");
viewModel.reloadPage();
} else {
Timber.w("Failed to delete post");
Toast.makeText(getBaseContext(), "Delete failed!", Toast.LENGTH_SHORT).show();
}
viewModel.setDeleteTaskStartedListener(() -> progressBar.setVisibility(ProgressBar.VISIBLE));
viewModel.setDeleteTaskFinishedListener((resultCode, data) -> {
progressBar.setVisibility(ProgressBar.GONE);
if (resultCode == Parcel.ResultCode.SUCCESSFUL) {
Timber.i("Post deleted successfully");
viewModel.reloadPage();
} else {
Timber.w("Failed to delete post");
Toast.makeText(getBaseContext(), "Delete failed!", Toast.LENGTH_SHORT).show();
}
});
viewModel.setReplyFinishListener(new ReplyTask.ReplyTaskCallbacks() {

53
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java

@ -1,62 +1,39 @@
package gr.thmmy.mthmmy.activities.topic.tasks;
import android.os.AsyncTask;
import org.jsoup.nodes.Document;
import java.io.IOException;
import gr.thmmy.mthmmy.activities.topic.Posting;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.utils.parsing.NetworkTask;
import gr.thmmy.mthmmy.utils.parsing.Parcel;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;
public class DeleteTask extends AsyncTask<String, Void, Boolean> {
private DeleteTaskCallbacks listener;
public class DeleteTask extends NetworkTask<Void> {
public DeleteTask(DeleteTaskCallbacks listener) {
this.listener = listener;
public DeleteTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected void onPreExecute() {
listener.onDeleteTaskStarted();
}
@Override
protected Boolean doInBackground(String... args) {
protected Response sendRequest(OkHttpClient client, String... input) throws IOException {
Request delete = new Request.Builder()
.url(args[0])
.url(input[0])
.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")
.build();
try {
OkHttpClient client = BaseApplication.getInstance().getClient();
client.newCall(delete).execute();
Response response = 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;
}
client.newCall(delete).execute();
return client.newCall(delete).execute();
}
@Override
protected void onPostExecute(Boolean result) {
listener.onDeleteTaskFinished(result);
protected Void performTask(Document document) {
return null;
}
public interface DeleteTaskCallbacks {
void onDeleteTaskStarted();
void onDeleteTaskFinished(boolean result);
@Override
protected int getResultCode(Response response, Void data) {
return Parcel.ResultCode.SUCCESSFUL;
}
}

19
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java

@ -7,7 +7,6 @@ import java.io.IOException;
import gr.thmmy.mthmmy.base.BaseApplication;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;
@ -30,15 +29,21 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
@Override
protected final Parcel<T> doInBackground(String... input) {
Response response = sendRequest(BaseApplication.getInstance().getClient(), input);
Response response;
try {
response = sendRequest(BaseApplication.getInstance().getClient(), input);
} catch (IOException e) {
Timber.e(e, "Error connecting to thmmy.gr");
return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null);
}
String responseBodyString;
try {
responseBodyString = response.body().string();
} catch (NullPointerException npe) {
Timber.e(npe, "Invalid response. Detatails: https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html#body--");
Timber.wtf(npe, "Invalid response. Detatails: https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html#body--");
return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null);
} catch (IOException e) {
Timber.e(e);
Timber.e(e, "Error getting response body string");
return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null);
}
try {
@ -62,9 +67,9 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
super.onPostExecute(tParcel);
}
protected abstract Response sendRequest(OkHttpClient client, String... input);
protected abstract Response sendRequest(OkHttpClient client, String... input) throws IOException;
protected abstract T performTask(Document document) throws ParseException;
protected abstract T performTask(Document document);
protected abstract int getResultCode(Response response, T data);
@ -73,6 +78,6 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
}
public interface OnParseTaskFinishedListener<T> {
void onParseFinish(int resultCOde, T data);
void onParseFinish(int resultCode, T data);
}
}

4
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java

@ -16,7 +16,7 @@ public abstract class NewParseTask<T> extends NetworkTask<T> {
public NewParseTask() {}
@Override
protected final T performTask(Document document) throws ParseException {
protected final T performTask(Document document) {
try {
return parse(document);
} catch (Exception e) {
@ -24,5 +24,5 @@ public abstract class NewParseTask<T> extends NetworkTask<T> {
}
}
protected abstract T parse (Document document);
protected abstract T parse (Document document) throws ParseException;
}

2
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java

@ -3,7 +3,7 @@ package gr.thmmy.mthmmy.utils.parsing;
/**
* ParseException is to be used for errors while parsing.
*/
public class ParseException extends Exception {
public class ParseException extends RuntimeException {
public ParseException() {}
public ParseException(String message)

16
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -9,8 +9,8 @@ import android.preference.PreferenceManager;
import java.util.ArrayList;
import gr.thmmy.mthmmy.activities.settings.SettingsActivity;
import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.EditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply;
@ -50,7 +50,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
//callbacks for topic activity
private TopicTask.TopicTaskObserver topicTaskObserver;
private DeleteTask.DeleteTaskCallbacks deleteTaskCallbacks;
private DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener;
private DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener;
private ReplyTask.ReplyTaskCallbacks replyFinishListener;
private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks;
private EditTask.EditTaskCallbacks editTaskCallbacks;
@ -133,7 +134,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
public void deletePost(String postDeleteUrl) {
Timber.i("Deleting post");
new DeleteTask(deleteTaskCallbacks).execute(postDeleteUrl);
new DeleteTask(deleteTaskStartedListener, deleteTaskFinishedListener).execute(postDeleteUrl);
}
public void prepareForEdit(int position, String postEditURL) {
@ -313,8 +314,13 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
this.topicTaskObserver = topicTaskObserver;
}
public void setDeleteTaskCallbacks(DeleteTask.DeleteTaskCallbacks deleteTaskCallbacks) {
this.deleteTaskCallbacks = deleteTaskCallbacks;
public void setDeleteTaskStartedListener(DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener) {
this.deleteTaskStartedListener = deleteTaskStartedListener;
}
public void setDeleteTaskFinishedListener(DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener) {
this.deleteTaskFinishedListener = deleteTaskFinishedListener;
}
public void setReplyFinishListener(ReplyTask.ReplyTaskCallbacks replyFinishListener) {

Loading…
Cancel
Save