From 497d57681aecbcd38c0afda60fa18b44bacb0c84 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Wed, 1 Aug 2018 15:05:47 +0300 Subject: [PATCH] Add yellow border to posts that mention logged in user --- .../mthmmy/activities/topic/TopicAdapter.java | 10 ++++++ .../mthmmy/activities/topic/TopicParser.java | 34 ++++++++++++++----- .../main/java/gr/thmmy/mthmmy/model/Post.java | 18 +++++++--- app/src/main/res/drawable/mention_card.xml | 13 +++++++ app/src/main/res/values/colors.xml | 1 + 5 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 app/src/main/res/drawable/mention_card.xml 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 1bc85acc..cb4e7176 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 @@ -16,6 +16,7 @@ import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AlertDialog; import android.support.v7.content.res.AppCompatResources; import android.support.v7.widget.AppCompatImageButton; +import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.view.LayoutInflater; @@ -294,6 +295,15 @@ class TopicAdapter extends RecyclerView.Adapter { getDrawable(R.drawable.member_of_the_month_card)); } else holder.cardChildLinear.setBackground(null); + if (currentPost.isUserMentionedInPost()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + holder.cardChildLinear.setBackground(context.getResources(). + getDrawable(R.drawable.mention_card, null)); + } else //noinspection deprecation + holder.cardChildLinear.setBackground(context.getResources(). + getDrawable(R.drawable.mention_card)); + } else holder.cardChildLinear.setBackground(null); + //Avoid's view's visibility recycling if (!currentPost.isDeleted() && viewModel.isUserExtraInfoVisible(holder.getAdapterPosition())) { holder.userExtraInfo.setVisibility(View.VISIBLE); 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 dd354bf7..6563a715 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 @@ -1,7 +1,9 @@ package gr.thmmy.mthmmy.activities.topic; import android.graphics.Color; +import android.util.Log; +import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -13,7 +15,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.regex.Pattern; +import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyFile; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; @@ -29,6 +33,10 @@ import timber.log.Timber; *
  • {@link #parseTopic(Document, ParseHelpers.Language)}
  • */ public class TopicParser { + private static Pattern mentionsPattern = Pattern. + compile("
    \\n\\s+?Quote from: " + + BaseActivity.getSessionManager().getUsername()); + //User colors private static final int USER_COLOR_BLACK = Color.parseColor("#000000"); private static final int USER_COLOR_RED = Color.parseColor("#F44336"); @@ -159,7 +167,7 @@ public class TopicParser { p_specialRank, p_gender, p_personalText, p_numberOfPosts, p_postLastEditDate, p_postURL, p_deletePostURL, p_editPostURL; int p_postNum, p_postIndex, p_numberOfStars, p_userColor; - boolean p_isDeleted = false; + boolean p_isDeleted = false, p_isUserMentionedInPost = false; ArrayList p_attachedFiles; //Initialize variables @@ -188,7 +196,7 @@ public class TopicParser { p_subject = thisRow.select("div[id^=subject_]").first().select("a").first().text(); //Finds post's link - p_postURL = thisRow.select("div[id^=subject_]").first().select("a").first() .attr("href"); + p_postURL = thisRow.select("div[id^=subject_]").first().select("a").first().attr("href"); //Finds post's text p_post = ParseHelpers.youtubeEmbeddedFix(thisRow.select("div").select(".post").first()); @@ -204,11 +212,11 @@ public class TopicParser { if (postIndex != null) { String tmp = postIndex.attr("name"); p_postIndex = Integer.parseInt(tmp.substring(tmp.indexOf("msg") + 3)); - } else{ + } else { postIndex = thisRow.select("div[id^=subject]").first(); if (postIndex == null) p_postIndex = NO_INDEX; - else{ + else { String tmp = postIndex.attr("id"); p_postIndex = Integer.parseInt(tmp.substring(tmp.indexOf("subject") + 8)); } @@ -237,7 +245,7 @@ public class TopicParser { //Finds post delete url Element postDelete = thisRow.select("a:has(img[alt='Διαγραφή'])").first(); - if (postDelete!=null){ + if (postDelete != null) { p_deletePostURL = postDelete.attr("href"); } @@ -303,7 +311,7 @@ public class TopicParser { //Finds post delete url Element postDelete = thisRow.select("a:has(img[alt='Remove message'])").first(); - if (postDelete!=null){ + if (postDelete != null) { p_deletePostURL = postDelete.attr("href"); } @@ -434,17 +442,25 @@ public class TopicParser { } } + //Checks post for mentions of this user (if the user is logged in) + if (BaseActivity.getSessionManager().isLoggedIn() && + mentionsPattern.matcher(p_post).find()) { + p_isUserMentionedInPost = true; + } + //Add new post in postsList, extended information needed 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_editPostURL, Post.TYPE_POST)); + , p_attachedFiles, p_postLastEditDate, p_postURL, p_deletePostURL, p_editPostURL + , p_isUserMentionedInPost, 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_editPostURL, Post.TYPE_POST)); + , p_postIndex, p_postNum, p_postDate, p_userColor, p_attachedFiles + , p_postLastEditDate, p_postURL, p_deletePostURL, p_editPostURL + , p_isUserMentionedInPost, 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 8641f288..078386dc 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Post.java @@ -45,6 +45,7 @@ public class Post { private final String numberOfPosts; private final String personalText; private final int numberOfStars; + private final boolean isUserMentionedInPost; // Suppresses default constructor @SuppressWarnings("unused") @@ -70,6 +71,7 @@ public class Post { postURL = null; postDeleteURL = null; postEditURL = null; + isUserMentionedInPost = false; postType = -1; } @@ -102,7 +104,8 @@ public class Post { , @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 postEditURL, int postType) { + , @Nullable String postDeleteURL, @Nullable String postEditURL, boolean isUserMentionedInPost + , int postType) { if (Objects.equals(thumbnailUrl, "")) this.thumbnailUrl = null; else this.thumbnailUrl = thumbnailUrl; this.author = author; @@ -125,6 +128,7 @@ public class Post { this.postURL = postURL; this.postDeleteURL = postDeleteURL; this.postEditURL = postEditURL; + this.isUserMentionedInPost = isUserMentionedInPost; this.postType = postType; } @@ -143,12 +147,13 @@ 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, int userColor , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL - , @Nullable String postDeleteURL, @Nullable String postEditURL, int postType) { + , @Nullable String postDeleteURL, @Nullable String postEditURL, boolean isUserMentionedInPost + , int postType) { if (Objects.equals(thumbnailUrl, "")) this.thumbnailUrl = null; else this.thumbnailUrl = thumbnailUrl; this.author = author; @@ -171,12 +176,13 @@ public class Post { this.postURL = postURL; this.postDeleteURL = postDeleteURL; this.postEditURL = postEditURL; + this.isUserMentionedInPost = isUserMentionedInPost; 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); + 0, null, null, null, null, null, false, TYPE_QUICK_REPLY); } //Getters @@ -390,6 +396,10 @@ public class Post { return postType; } + public boolean isUserMentionedInPost() { + return isUserMentionedInPost; + } + public void setPostType(int postType) { this.postType = postType; } diff --git a/app/src/main/res/drawable/mention_card.xml b/app/src/main/res/drawable/mention_card.xml new file mode 100644 index 00000000..974d3d67 --- /dev/null +++ b/app/src/main/res/drawable/mention_card.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 257e7f22..ddf97608 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -17,6 +17,7 @@ #3C3F41 #8B8B8B #FF9800 + #FAA61A #FFFFFF #CCCCCC