Browse Source

almost finish message edit request

pull/30/head
Thodoris1999 7 years ago
parent
commit
ad1768ef4d
  1. 44
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/EditTaskDTO.java
  2. 97
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  3. 71
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  4. 5
      app/src/main/res/drawable/ic_edit_black_24dp.xml
  5. 2
      app/src/main/res/layout/activity_topic_overflow_menu.xml

44
app/src/main/java/gr/thmmy/mthmmy/activities/topic/EditTaskDTO.java

@ -0,0 +1,44 @@
package gr.thmmy.mthmmy.activities.topic;
public class EditTaskDTO {
private int position;
private final String subject, message, numReplies, seqnum, sc, topic;
public EditTaskDTO(int position, String subject, String message, String numReplies, String seqnum, String sc, String topic) {
this.position = position;
this.subject = subject;
this.message = message;
this.numReplies = numReplies;
this.seqnum = seqnum;
this.sc = sc;
this.topic = topic;
}
public int getPosition() {
return position;
}
public String getSubject() {
return subject;
}
public String getMessage() {
return message;
}
public String getNumReplies() {
return numReplies;
}
public String getSeqnum() {
return seqnum;
}
public String getSc() {
return sc;
}
public String getTopic() {
return topic;
}
}

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

@ -1037,7 +1037,9 @@ public class TopicActivity extends BaseActivity {
} }
} }
class PrepareForEdit extends AsyncTask<String, Void, String> { class PrepareForEdit extends AsyncTask<Integer, Void, Boolean> {
int position;
String numReplies, seqnum, sc, topic, postText = "";
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
@ -1049,10 +1051,12 @@ public class TopicActivity extends BaseActivity {
} }
@Override @Override
protected String doInBackground(String... strings) { protected Boolean doInBackground(Integer... positions) {
Document document; Document document;
position = positions[0];
String url = postsList.get(position).getPostEditURL();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(strings[0] + ";wap2") .url(url + ";wap2")
.build(); .build();
try { try {
@ -1060,18 +1064,97 @@ public class TopicActivity extends BaseActivity {
document = Jsoup.parse(response.body().string()); document = Jsoup.parse(response.body().string());
Element message = document.select("textarea").first(); Element message = document.select("textarea").first();
Timber.e(message.html()); postText = message.html();
return message.html(); numReplies = replyPageUrl.substring(replyPageUrl.indexOf("num_replies=") + 12);
seqnum = document.select("input[name=seqnum]").first().attr("value");
sc = document.select("input[name=sc]").first().attr("value");
topic = document.select("input[name=topic]").first().attr("value");
return true;
} catch (IOException | Selector.SelectorParseException e) { } catch (IOException | Selector.SelectorParseException e) {
Timber.e(e, "Prepare failed."); Timber.e(e, "Prepare failed.");
return ""; return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
postsList.get(position).setPostType(Post.TYPE_EDIT);
topicAdapter.notifyItemChanged(position);
topicAdapter.prepareForEdit(new EditTask(), numReplies, seqnum, sc, topic, postText);
recyclerView.scrollToPosition(position);
progressBar.setVisibility(ProgressBar.GONE);
}
}
public class EditTask extends AsyncTask<EditTaskDTO, Void, Boolean>{
EditTaskDTO dto;
@Override
protected void onPreExecute() {
progressBar.setVisibility(ProgressBar.VISIBLE);
paginationEnabled(false);
replyFAB.setEnabled(false);
}
@Override
protected Boolean doInBackground(EditTaskDTO... editTaskDTOS) {
dto = editTaskDTOS[0];
RequestBody postBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("message", dto.getMessage())
.addFormDataPart("num_replies", dto.getNumReplies())
.addFormDataPart("seqnum", dto.getSeqnum())
.addFormDataPart("sc", dto.getSc())
.addFormDataPart("subject", dto.getSubject())
.addFormDataPart("topic", dto.getTopic())
.build();
Request post = new Request.Builder()
.url(postsList.get(dto.getPosition()).getPostEditURL())
.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")
.post(postBody)
.build();
try {
client.newCall(post).execute();
Response response = client.newCall(post).execute();
switch (replyStatus(response)) {
case SUCCESSFUL:
return true;
case NEW_REPLY_WHILE_POSTING:
//TODO this...
return true;
default:
Timber.e("Malformed post. Request string: %s", post.toString());
return true;
}
} catch (IOException e) {
Timber.e(e, "Edit failed.");
return false;
} }
} }
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(Boolean result) {
View view = getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
postsList.get(dto.getPosition()).setPostType(Post.TYPE_POST);
topicAdapter.notifyItemChanged(dto.getPosition());
progressBar.setVisibility(ProgressBar.GONE); progressBar.setVisibility(ProgressBar.GONE);
replyFAB.setVisibility(View.VISIBLE);
bottomNavBar.setVisibility(View.VISIBLE);
if (!result)
Toast.makeText(TopicActivity.this, "Post failed!", Toast.LENGTH_SHORT).show();
paginationEnabled(true);
replyFAB.setEnabled(true);
} }
} }
} }

71
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -93,10 +93,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private TopicActivity.ReplyTask replyTask; private TopicActivity.ReplyTask replyTask;
private TopicActivity.DeleteTask deleteTask; private TopicActivity.DeleteTask deleteTask;
private TopicActivity.PrepareForEdit prepareForEditTask; private TopicActivity.PrepareForEdit prepareForEditTask;
private TopicActivity.EditTask editTask;
private final String[] replyDataHolder = new String[2]; private final String[] replyDataHolder = new String[2];
private final int replySubject = 0, replyText = 1; private final int replySubject = 0, replyText = 1;
private String numReplies, seqnum, sc, topic, buildedQuotes; private String numReplies, seqnum, sc, topic, buildedQuotes, postText;
private boolean canReply = false; private boolean canReply = false;
/** /**
@ -137,6 +138,16 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
this.deleteTask = deleteTask; this.deleteTask = deleteTask;
} }
void prepareForEdit(TopicActivity.EditTask editTask, String numReplies, String seqnum, String sc,
String topic, String postText) {
this.editTask = editTask;
this.numReplies = numReplies;
this.seqnum = seqnum;
this.sc = sc;
this.topic = topic;
this.postText = postText;
}
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
return postsList.get(position).getPostType(); return postsList.get(position).getPostType();
@ -486,8 +497,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
editPostButton.setOnClickListener(new View.OnClickListener() { editPostButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
postsList.get(position).setPostType(Post.TYPE_EDIT); prepareForEditTask.execute(position);
notifyItemChanged(position);
} }
}); });
} }
@ -546,35 +556,44 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
replyDataHolder[replyText] != null && !Objects.equals(replyDataHolder[replyText], "")) { replyDataHolder[replyText] != null && !Objects.equals(replyDataHolder[replyText], "")) {
holder.quickReply.setText(replyDataHolder[replyText]); holder.quickReply.setText(replyDataHolder[replyText]);
holder.quickReplySubject.setText(replyDataHolder[replySubject]); holder.quickReplySubject.setText(replyDataHolder[replySubject]);
holder.submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.quickReplySubject.getText().toString().isEmpty()) return;
if (holder.quickReply.getText().toString().isEmpty()) return;
holder.submitButton.setEnabled(false);
replyTask.execute(holder.quickReplySubject.getText().toString(),
holder.quickReply.getText().toString(), numReplies, seqnum, sc, topic);
holder.quickReplySubject.getText().clear();
holder.quickReplySubject.setText("Re: " + topicTitle);
holder.quickReply.getText().clear();
holder.submitButton.setEnabled(true);
}
});
} else if (postsList.get(position).getPostType() == Post.TYPE_EDIT) { } else if (postsList.get(position).getPostType() == Post.TYPE_EDIT) {
//post in edit mode //post in edit mode
holder.quickReplySubject.setText(postsList.get(position).getSubject()); holder.quickReplySubject.setText(postsList.get(position).getSubject());
String postText = "";
try {
postText = prepareForEditTask.execute(postsList.get(position).getPostEditURL()).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
holder.quickReply.setText(postText); holder.quickReply.setText(postText);
holder.submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.quickReplySubject.getText().toString().isEmpty()) return;
if (holder.quickReply.getText().toString().isEmpty()) return;
holder.submitButton.setEnabled(false);
editTask.execute(new EditTaskDTO(position, holder.quickReplySubject.getText().toString(),
holder.quickReply.getText().toString(), numReplies, seqnum, sc, topic));
holder.quickReplySubject.getText().clear();
//holder.quickReplySubject.setText("Re: " + topicTitle);
holder.quickReply.getText().clear();
holder.submitButton.setEnabled(true);
}
});
} }
holder.submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.quickReplySubject.getText().toString().isEmpty()) return;
if (holder.quickReply.getText().toString().isEmpty()) return;
holder.submitButton.setEnabled(false);
replyTask.execute(holder.quickReplySubject.getText().toString(),
holder.quickReply.getText().toString(), numReplies, seqnum, sc, topic);
holder.quickReplySubject.getText().clear();
holder.quickReplySubject.setText("Re: " + topicTitle);
holder.quickReply.getText().clear();
holder.submitButton.setEnabled(true);
}
});
if (backPressHidden) { if (backPressHidden) {
holder.quickReply.requestFocus(); holder.quickReply.requestFocus();
backPressHidden = false; backPressHidden = false;

5
app/src/main/res/drawable/ic_edit_black_24dp.xml

@ -1,5 +0,0 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>

2
app/src/main/res/layout/activity_topic_overflow_menu.xml

@ -40,7 +40,7 @@
android:layout_height="35dp" android:layout_height="35dp"
android:background="?android:attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:drawablePadding="5dp" android:drawablePadding="5dp"
android:drawableStart="@drawable/ic_edit_black_24dp" android:drawableStart="@drawable/ic_edit_white_24dp"
android:gravity="center_vertical" android:gravity="center_vertical"
android:paddingBottom="6dp" android:paddingBottom="6dp"
android:paddingEnd="12dp" android:paddingEnd="12dp"

Loading…
Cancel
Save