From 3228e1763e48c635e0d7863dfb74d9457f7cb323 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Mon, 16 Jul 2018 19:33:43 +0300 Subject: [PATCH] prepare for edit --- .../activities/topic/TopicActivity.java | 42 ++++++++++++++- .../mthmmy/activities/topic/TopicAdapter.java | 53 ++++++++++++++++--- .../mthmmy/activities/topic/TopicParser.java | 13 +++-- .../main/java/gr/thmmy/mthmmy/model/Post.java | 41 ++++++++++++-- 4 files changed, 133 insertions(+), 16 deletions(-) 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 f5747998..5eae79da 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 @@ -262,7 +262,7 @@ public class TopicActivity extends BaseActivity { CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager( getApplicationContext(), loadedPageUrl); recyclerView.setLayoutManager(layoutManager); - topicAdapter = new TopicAdapter(this, postsList, base_url, topicTask); + topicAdapter = new TopicAdapter(this, postsList, base_url, topicTask, new PrepareForEdit()); recyclerView.setAdapter(topicAdapter); replyFAB = findViewById(R.id.topic_fab); @@ -895,7 +895,7 @@ public class TopicActivity extends BaseActivity { @Override protected void onPostExecute(Boolean result) { - postsList.add(null); + postsList.add(Post.newQuickReply()); topicAdapter.notifyItemInserted(postsList.size()); topicAdapter.prepareForReply(new ReplyTask(), topicTitle, numReplies, seqnum, sc, topic, buildedQuotes); @@ -1036,4 +1036,42 @@ public class TopicActivity extends BaseActivity { } } } + + class PrepareForEdit extends AsyncTask { + + @Override + protected void onPreExecute() { + progressBar.setVisibility(ProgressBar.VISIBLE); + paginationEnabled(false); + replyFAB.setEnabled(false); + replyFAB.hide(); + bottomNavBar.setVisibility(View.GONE); + } + + @Override + protected String doInBackground(String... strings) { + Document document; + Request request = new Request.Builder() + .url(strings[0] + ";wap2") + .build(); + + try { + Response response = client.newCall(request).execute(); + document = Jsoup.parse(response.body().string()); + + Element message = document.select("textarea").first(); + Timber.e(message.html()); + + return message.html(); + } catch (IOException | Selector.SelectorParseException e) { + Timber.e(e, "Prepare failed."); + return ""; + } + } + + @Override + protected void onPostExecute(String result) { + progressBar.setVisibility(ProgressBar.GONE); + } + } } \ 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 345f9c5b..c40753bb 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 @@ -38,6 +38,7 @@ import com.squareup.picasso.Picasso; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.concurrent.ExecutionException; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.board.BoardActivity; @@ -91,8 +92,7 @@ class TopicAdapter extends RecyclerView.Adapter { private TopicActivity.TopicTask topicTask; private TopicActivity.ReplyTask replyTask; private TopicActivity.DeleteTask deleteTask; - private final int VIEW_TYPE_POST = 0; - private final int VIEW_TYPE_QUICK_REPLY = 1; + private TopicActivity.PrepareForEdit prepareForEditTask; private final String[] replyDataHolder = new String[2]; private final int replySubject = 0, replyText = 1; @@ -104,7 +104,7 @@ class TopicAdapter extends RecyclerView.Adapter { * @param postsList List of {@link Post} objects to use */ TopicAdapter(Context context, List postsList, String baseUrl, - TopicActivity.TopicTask topicTask) { + TopicActivity.TopicTask topicTask, TopicActivity.PrepareForEdit prepareForEditTask) { this.context = context; this.postsList = postsList; this.baseUrl = baseUrl; @@ -115,6 +115,7 @@ class TopicAdapter extends RecyclerView.Adapter { viewProperties.add(new boolean[3]); } this.topicTask = topicTask; + this.prepareForEditTask = prepareForEditTask; } ArrayList getToQuoteList() { @@ -138,16 +139,16 @@ class TopicAdapter extends RecyclerView.Adapter { @Override public int getItemViewType(int position) { - return postsList.get(position) == null ? VIEW_TYPE_QUICK_REPLY : VIEW_TYPE_POST; + return postsList.get(position).getPostType(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - if (viewType == VIEW_TYPE_POST) { + if (viewType == Post.TYPE_POST) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.activity_topic_post_row, parent, false); return new PostViewHolder(itemView); - } else if (viewType == VIEW_TYPE_QUICK_REPLY) { + } else if (viewType == Post.TYPE_QUICK_REPLY) { View view = LayoutInflater.from(parent.getContext()). inflate(R.layout.activity_topic_quick_reply_row, parent, false); view.findViewById(R.id.quick_reply_submit).setEnabled(true); @@ -175,6 +176,13 @@ class TopicAdapter extends RecyclerView.Adapter { replyDataHolder[replyText] = buildedQuotes; return new QuickReplyViewHolder(view, new CustomEditTextListener(replySubject), new CustomEditTextListener(replyText)); + } else if (viewType == Post.TYPE_EDIT) { + View view = LayoutInflater.from(parent.getContext()). + inflate(R.layout.activity_topic_quick_reply_row, parent, false); + view.findViewById(R.id.quick_reply_submit).setEnabled(true); + + return new QuickReplyViewHolder(view, new CustomEditTextListener(replySubject), + new CustomEditTextListener(replyText)); } return null; } @@ -470,6 +478,20 @@ class TopicAdapter extends RecyclerView.Adapter { }); } + final TextView editPostButton = popUpContent.findViewById(R.id.edit_post); + + if (currentPost.getPostEditURL() == null || currentPost.getPostEditURL().equals("")) { + editPostButton.setVisibility(View.GONE); + } else { + editPostButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + postsList.get(position).setPostType(Post.TYPE_EDIT); + notifyItemChanged(position); + } + }); + } + //Displays the popup popUp.showAsDropDown(holder.overflowButton); } @@ -518,10 +540,25 @@ class TopicAdapter extends RecyclerView.Adapter { .transform(new CircleTransform()) .into(holder.thumbnail); holder.username.setText(getSessionManager().getUsername()); - holder.quickReplySubject.setText(replyDataHolder[replySubject]); - if (replyDataHolder[replyText] != null && !Objects.equals(replyDataHolder[replyText], "")) + + if (postsList.get(position).getPostType() == Post.TYPE_QUICK_REPLY && + replyDataHolder[replyText] != null && !Objects.equals(replyDataHolder[replyText], "")) { holder.quickReply.setText(replyDataHolder[replyText]); + holder.quickReplySubject.setText(replyDataHolder[replySubject]); + } 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 diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index 3ead462c..a753fc8c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java @@ -157,7 +157,7 @@ class TopicParser { //Variables for Post constructor String p_userName, p_thumbnailURL, p_subject, p_post, p_postDate, p_profileURL, p_rank, p_specialRank, p_gender, p_personalText, p_numberOfPosts, p_postLastEditDate, - p_postURL, p_deletePostURL; + p_postURL, p_deletePostURL, p_editPostURL; int p_postNum, p_postIndex, p_numberOfStars, p_userColor; boolean p_isDeleted = false; ArrayList p_attachedFiles; @@ -174,6 +174,7 @@ class TopicParser { p_attachedFiles = new ArrayList<>(); p_postLastEditDate = null; p_deletePostURL = null; + p_editPostURL = null; //Language independent parsing //Finds thumbnail url @@ -306,6 +307,12 @@ class TopicParser { p_deletePostURL = postDelete.attr("href"); } + //Finds post modify url + Element postEdit = thisRow.select("a:has(img[alt='Modify message'])").first(); + if (postEdit != null) { + p_editPostURL = postEdit.attr("href"); + } + //Finds post's submit date Element postDate = thisRow.select("div.smalltext:matches(on:)").first(); p_postDate = postDate.text(); @@ -431,13 +438,13 @@ class TopicParser { parsedPostsList.add(new Post(p_thumbnailURL, p_userName, p_subject, p_post, p_postIndex , p_postNum, p_postDate, p_profileURL, p_rank, p_specialRank, p_gender , p_numberOfPosts, p_personalText, p_numberOfStars, p_userColor - , p_attachedFiles, p_postLastEditDate, p_postURL, p_deletePostURL)); + , p_attachedFiles, p_postLastEditDate, p_postURL, p_deletePostURL, p_editPostURL, Post.TYPE_POST)); } else { //Deleted user //Add new post in postsList, only standard information needed parsedPostsList.add(new Post(p_thumbnailURL, p_userName, p_subject, p_post , p_postIndex , p_postNum, p_postDate, p_userColor, p_attachedFiles - , p_postLastEditDate, p_postURL, p_deletePostURL)); + , p_postLastEditDate, p_postURL, p_deletePostURL, p_editPostURL, Post.TYPE_POST)); } } return parsedPostsList; diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java b/app/src/main/java/gr/thmmy/mthmmy/model/Post.java index 3206061d..8641f288 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Post.java @@ -16,6 +16,10 @@ import java.util.Objects; * previous fields.

*/ public class Post { + public static final int TYPE_POST = 0; + public static final int TYPE_QUICK_REPLY = 1; + public static final int TYPE_EDIT = 2; + //Standard info (exists in every post) private final String thumbnailUrl; private final String author; @@ -30,6 +34,8 @@ public class Post { private final String lastEdit; private final String postURL; private final String postDeleteURL; + private final String postEditURL; + private int postType; //Extra info private final String profileURL; @@ -63,6 +69,8 @@ public class Post { lastEdit = null; postURL = null; postDeleteURL = null; + postEditURL = null; + postType = -1; } /** @@ -87,14 +95,14 @@ public class Post { * @param userColor author's user color * @param attachedFiles post's attached files * @param lastEdit post's last edit date - * @param postURL post's URL + * @param postURL post's URL */ public Post(@Nullable String thumbnailUrl, String author, String subject, String content , int postIndex, int postNumber, String postDate, String profileURl, @Nullable String rank , @Nullable String special_rank, @Nullable String gender, @Nullable String numberOfPosts , @Nullable String personalText, int numberOfStars, int userColor , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL - , @Nullable String postDeleteURL) { + , @Nullable String postDeleteURL, @Nullable String postEditURL, int postType) { if (Objects.equals(thumbnailUrl, "")) this.thumbnailUrl = null; else this.thumbnailUrl = thumbnailUrl; this.author = author; @@ -116,6 +124,8 @@ public class Post { this.numberOfStars = numberOfStars; this.postURL = postURL; this.postDeleteURL = postDeleteURL; + this.postEditURL = postEditURL; + this.postType = postType; } /** @@ -138,7 +148,7 @@ public class Post { public Post(@Nullable String thumbnailUrl, String author, String subject, String content , int postIndex, int postNumber, String postDate, int userColor , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL - , @Nullable String postDeleteURL) { + , @Nullable String postDeleteURL, @Nullable String postEditURL, int postType) { if (Objects.equals(thumbnailUrl, "")) this.thumbnailUrl = null; else this.thumbnailUrl = thumbnailUrl; this.author = author; @@ -160,6 +170,13 @@ public class Post { numberOfStars = 0; this.postURL = postURL; this.postDeleteURL = postDeleteURL; + this.postEditURL = postEditURL; + this.postType = postType; + } + + public static Post newQuickReply() { + return new Post(null, null, null, null, 0, 0, null, + 0, null, null, null, null, null, TYPE_QUICK_REPLY); } //Getters @@ -358,4 +375,22 @@ public class Post { public String getPostDeleteURL() { return postDeleteURL; } + + /** + * Gets this post's modify url. + * + * @return post's edit url + */ + @Nullable + public String getPostEditURL() { + return postEditURL; + } + + public int getPostType() { + return postType; + } + + public void setPostType(int postType) { + this.postType = postType; + } }