Browse Source

improvements and implement parse task in delete post

pull/54/head
Thodoris1999 6 years ago
parent
commit
4938597f2e
  1. 17
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 51
      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

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

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

@ -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.parsing.NetworkTask;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.utils.parsing.Parcel;
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 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 gr.thmmy.mthmmy.base.BaseApplication;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
@ -30,15 +29,21 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
@Override @Override
protected final Parcel<T> doInBackground(String... input) { 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; String responseBodyString;
try { try {
responseBodyString = response.body().string(); responseBodyString = response.body().string();
} catch (NullPointerException npe) { } 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); return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null);
} catch (IOException e) { } catch (IOException e) {
Timber.e(e); Timber.e(e, "Error getting response body string");
return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null);
} }
try { try {
@ -62,9 +67,9 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
super.onPostExecute(tParcel); 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); 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> { 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() {} public NewParseTask() {}
@Override @Override
protected final T performTask(Document document) throws ParseException { protected final T performTask(Document document) {
try { try {
return parse(document); return parse(document);
} catch (Exception e) { } 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. * ParseException is to be used for errors while parsing.
*/ */
public class ParseException extends Exception { public class ParseException extends RuntimeException {
public ParseException() {} public ParseException() {}
public ParseException(String message) 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 java.util.ArrayList;
import gr.thmmy.mthmmy.activities.settings.SettingsActivity; 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.EditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply;
@ -50,7 +50,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
//callbacks for topic activity //callbacks for topic activity
private TopicTask.TopicTaskObserver topicTaskObserver; private TopicTask.TopicTaskObserver topicTaskObserver;
private DeleteTask.DeleteTaskCallbacks deleteTaskCallbacks; private DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener;
private DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener;
private ReplyTask.ReplyTaskCallbacks replyFinishListener; private ReplyTask.ReplyTaskCallbacks replyFinishListener;
private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks; private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks;
private EditTask.EditTaskCallbacks editTaskCallbacks; private EditTask.EditTaskCallbacks editTaskCallbacks;
@ -133,7 +134,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
public void deletePost(String postDeleteUrl) { public void deletePost(String postDeleteUrl) {
Timber.i("Deleting post"); Timber.i("Deleting post");
new DeleteTask(deleteTaskCallbacks).execute(postDeleteUrl); new DeleteTask(deleteTaskStartedListener, deleteTaskFinishedListener).execute(postDeleteUrl);
} }
public void prepareForEdit(int position, String postEditURL) { public void prepareForEdit(int position, String postEditURL) {
@ -313,8 +314,13 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
this.topicTaskObserver = topicTaskObserver; 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) { public void setReplyFinishListener(ReplyTask.ReplyTaskCallbacks replyFinishListener) {

Loading…
Cancel
Save