From ad1768ef4dbd91cd690e5194d974c3d0773b78ec Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 17 Jul 2018 15:35:41 +0300 Subject: [PATCH] almost finish message edit request --- .../mthmmy/activities/topic/EditTaskDTO.java | 44 +++++++++ .../activities/topic/TopicActivity.java | 97 +++++++++++++++++-- .../mthmmy/activities/topic/TopicAdapter.java | 71 +++++++++----- .../main/res/drawable/ic_edit_black_24dp.xml | 5 - .../layout/activity_topic_overflow_menu.xml | 2 +- 5 files changed, 180 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/activities/topic/EditTaskDTO.java delete mode 100644 app/src/main/res/drawable/ic_edit_black_24dp.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/EditTaskDTO.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/EditTaskDTO.java new file mode 100644 index 00000000..14393ba4 --- /dev/null +++ b/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; + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 5eae79da..83562311 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -1037,7 +1037,9 @@ public class TopicActivity extends BaseActivity { } } - class PrepareForEdit extends AsyncTask { + class PrepareForEdit extends AsyncTask { + int position; + String numReplies, seqnum, sc, topic, postText = ""; @Override protected void onPreExecute() { @@ -1049,10 +1051,12 @@ public class TopicActivity extends BaseActivity { } @Override - protected String doInBackground(String... strings) { + protected Boolean doInBackground(Integer... positions) { Document document; + position = positions[0]; + String url = postsList.get(position).getPostEditURL(); Request request = new Request.Builder() - .url(strings[0] + ";wap2") + .url(url + ";wap2") .build(); try { @@ -1060,18 +1064,97 @@ public class TopicActivity extends BaseActivity { document = Jsoup.parse(response.body().string()); 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) { 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 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 - 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); + 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); } } } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index c40753bb..65bfac69 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -93,10 +93,11 @@ class TopicAdapter extends RecyclerView.Adapter { private TopicActivity.ReplyTask replyTask; private TopicActivity.DeleteTask deleteTask; private TopicActivity.PrepareForEdit prepareForEditTask; + private TopicActivity.EditTask editTask; private final String[] replyDataHolder = new String[2]; 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; /** @@ -137,6 +138,16 @@ class TopicAdapter extends RecyclerView.Adapter { 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 public int getItemViewType(int position) { return postsList.get(position).getPostType(); @@ -486,8 +497,7 @@ class TopicAdapter extends RecyclerView.Adapter { editPostButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - postsList.get(position).setPostType(Post.TYPE_EDIT); - notifyItemChanged(position); + prepareForEditTask.execute(position); } }); } @@ -546,35 +556,44 @@ class TopicAdapter extends RecyclerView.Adapter { replyDataHolder[replyText] != null && !Objects.equals(replyDataHolder[replyText], "")) { holder.quickReply.setText(replyDataHolder[replyText]); 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) { //post in edit mode 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.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) { holder.quickReply.requestFocus(); backPressHidden = false; diff --git a/app/src/main/res/drawable/ic_edit_black_24dp.xml b/app/src/main/res/drawable/ic_edit_black_24dp.xml deleted file mode 100644 index 46462b57..00000000 --- a/app/src/main/res/drawable/ic_edit_black_24dp.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - diff --git a/app/src/main/res/layout/activity_topic_overflow_menu.xml b/app/src/main/res/layout/activity_topic_overflow_menu.xml index 8a143bb7..324972d4 100644 --- a/app/src/main/res/layout/activity_topic_overflow_menu.xml +++ b/app/src/main/res/layout/activity_topic_overflow_menu.xml @@ -40,7 +40,7 @@ android:layout_height="35dp" android:background="?android:attr/selectableItemBackground" android:drawablePadding="5dp" - android:drawableStart="@drawable/ic_edit_black_24dp" + android:drawableStart="@drawable/ic_edit_white_24dp" android:gravity="center_vertical" android:paddingBottom="6dp" android:paddingEnd="12dp"