From d9428cb4069ead89ce14e5c2bb26a29b2145ac0c Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sun, 27 Nov 2016 14:46:46 +0200 Subject: [PATCH] Added feature for extra user info (rank, personal text etc). A view expands/collapses with animation when post's header is clicked. --- .../mthmmy/activities/TopicActivity.java | 317 ++++++++++++++---- .../main/java/gr/thmmy/mthmmy/data/Post.java | 79 ++++- .../res/layout/activity_topic_post_row.xml | 79 ++++- 3 files changed, 399 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java index 02f85ac9..12d15865 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java @@ -24,6 +24,7 @@ import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; @@ -35,6 +36,7 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -74,6 +76,7 @@ public class TopicActivity extends BaseActivity { /* --Topic's pages end-- */ /* --Thumbnail-- */ private static final int THUMBNAIL_SIZE = 80; + private static final int THUMBNAIL_PADDING = 16; private ImageLoader imageLoader = ImageController.getInstance().getImageLoader(); /* --Thumbnail end-- */ @@ -343,12 +346,24 @@ public class TopicActivity extends BaseActivity { for (Element item : rows) { //For every post //Variables to pass - String p_userName, p_thumbnailUrl, p_subject, p_post, p_postDate; - int p_postNum, p_postIndex; + String p_userName, p_thumbnailUrl, p_subject, p_post, p_postDate, p_rank, + p_specialRank, p_gender, p_personalText, p_numberOfPosts, p_urlOfStars; + int p_postNum, p_postIndex, p_numberOfStars; + boolean p_isDeleted = false; + + //Initialize variables + p_rank = "Rank"; + p_specialRank = "Special rank"; + p_gender = ""; + p_personalText = ""; + p_numberOfPosts = ""; + p_urlOfStars = ""; + p_numberOfStars = 0; //Find the Username Element userName = item.select("a[title^=View the profile of]").first(); if (userName == null) { //Deleted profile + p_isDeleted = true; p_userName = item .select("td:has(div.smalltext:containsOwn(Guest))[style^=overflow]") .first().text(); @@ -376,7 +391,7 @@ public class TopicActivity extends BaseActivity { Element postDate = item.select("div.smalltext:matches(on:)").first(); p_postDate = postDate.text(); p_postDate = p_postDate.substring(p_postDate.indexOf("on:") + 4 - ,p_postDate.indexOf(" »")); + , p_postDate.indexOf(" »")); //Find post's number Element postNum = item.select("div.smalltext:matches(Reply #)").first(); @@ -389,16 +404,79 @@ public class TopicActivity extends BaseActivity { //Find post's index Element postIndex = item.select("a[name^=msg]").first(); - if(postIndex == null) + if (postIndex == null) p_postIndex = NO_INDEX; else { String tmp = postIndex.attr("name"); p_postIndex = Integer.parseInt(tmp.substring(tmp.indexOf("msg") + 3)); } - //Add new post in postsList - postsList.add(new Post(p_thumbnailUrl, p_userName, p_subject - , p_post, p_postDate, p_postNum, p_postIndex)); + if (!p_isDeleted) { //Active user + //Get extra info + int postsLineIndex = -1; + int starsLineIndex = -1; + + Element info = userName.parent().nextElementSibling(); //Get sibling "div" + List infoList = Arrays.asList(info.html().split("
")); + + for (String line : infoList) { + Log.i(TAG, line); + if (line.contains("Posts:")) { + postsLineIndex = infoList.indexOf(line); + //Remove any line breaks and spaces on the start and end + p_numberOfPosts = p_personalText.replace("\n", "") + .replace("\r", "").trim(); + } + if (line.contains("Gender:")) { + if (line.contains("alt=\"Male\"")) + p_gender = "Gender: Male"; + else + p_gender = "Gender: Female"; + } + if (line.contains("alt=\"*\"")) { + starsLineIndex = infoList.indexOf(line); + Document starsHtml = Jsoup.parse(line); + p_numberOfStars = starsHtml.select("img[alt]").size(); + p_urlOfStars = starsHtml.select("img[alt]").first().attr("abs:src"); + } + } + + //If this member has no stars yet ==> New member, + //or is just a member + if (starsLineIndex == -1 || starsLineIndex == 1) { + //In this case: + p_rank = infoList.get(0).trim(); //First line has the rank + p_specialRank = null; //They don't have a special rank + } else if (starsLineIndex == 2) { //This member has a special rank + p_specialRank = infoList.get(0).trim(); //First line has the special rank + p_rank = infoList.get(1).trim(); //Second line has the rank + } + for (int i = postsLineIndex + 1; i < infoList.size() - 1; ++i) { + //Search under "Posts:" + //and above "Personal Message", "View Profile" etc buttons + + String thisLine = infoList.get(i); + //If this line isn't empty and doesn't contain user's avatar + if (!Objects.equals(thisLine, "") && thisLine != null + && !Objects.equals(thisLine, " \n") + && !thisLine.contains("avatar")) { + p_personalText = thisLine; //Then this line has user's personal text + //Remove any line breaks and spaces on the start and end + p_personalText = p_personalText.replace("\n", "") + .replace("\r", "").trim(); + } + } + //Add new post in postsList, extended information needed + postsList.add(new Post(p_thumbnailUrl, p_userName, p_subject, p_post + , p_postIndex, p_postNum, p_postDate, p_isDeleted, p_rank + , p_specialRank, p_gender, p_numberOfPosts, p_personalText + , p_urlOfStars, p_numberOfStars)); + + } else{ //Deleted user + //Add new post in postsList, only standard information needed + postsList.add(new Post(p_thumbnailUrl, p_userName, p_subject + , p_post, p_postIndex, p_postNum, p_postDate, p_isDeleted)); + } } } /* Parse method end */ @@ -435,7 +513,9 @@ public class TopicActivity extends BaseActivity { TextView username = (TextView) convertView.findViewById(R.id.username); TextView subject = (TextView) convertView.findViewById(R.id.subject); final WebView post = (WebView) convertView.findViewById(R.id.post); - CardView cardView = (CardView) convertView.findViewById(R.id.card_view); + final CardView cardView = (CardView) convertView.findViewById(R.id.card_view); + RelativeLayout header = (RelativeLayout) convertView.findViewById(R.id.header); + final LinearLayout userExtraInfo = (LinearLayout) convertView.findViewById(R.id.user_extra_info); //Post's WebView parameters set post.setClickable(true); @@ -462,6 +542,8 @@ public class TopicActivity extends BaseActivity { //Post's index number set if (item.getPostNumber() != 0) postNum.setText("#" + item.getPostNumber()); + else + postNum.setText(""); //Subject set subject.setText(item.getSubject()); @@ -469,64 +551,131 @@ public class TopicActivity extends BaseActivity { //Post's text set post.loadDataWithBaseURL("file:///android_asset/", item.getContent(), "text/html", "UTF-8", null); - /* --"Card expand/collapse"-like functionality-- */ - if (item.getPostNumber() != 0) { - //Should expand/collapse when card is touched - cardView.setOnClickListener(new View.OnClickListener() { + if(!item.isDeleted()) { //Set extra info + //Variables for Graphics + TextView g_specialRank, g_rank, g_gender, g_numberOfPosts, g_personalText; + LinearLayout g_stars_holder = (LinearLayout) convertView.findViewById(R.id.stars); + + //Variables for content + String c_specialRank = item.getSpecialRank() + , c_rank = item.getRank() + , c_gender = item.getGender() + , c_numberOfPosts = item.getNumberOfPosts() + , c_personalText = item.getPersonalText() + , c_urlOfStars = item.getUrlOfStars(); + int c_numberOfStars = item.getNumberOfStars(); + + if(!Objects.equals(c_specialRank, "") && c_specialRank != null){ + g_specialRank = (TextView) convertView.findViewById(R.id.special_rank); + g_specialRank.setText(c_specialRank); + g_specialRank.setVisibility(View.VISIBLE); + } + if(!Objects.equals(c_rank, "") && c_rank != null){ + g_rank = (TextView) convertView.findViewById(R.id.rank); + g_rank.setText(c_rank); + g_rank.setVisibility(View.VISIBLE); + } + if(!Objects.equals(c_gender, "") && c_gender != null){ + g_gender = (TextView) convertView.findViewById(R.id.gender); + g_gender.setText(c_gender); + g_gender.setVisibility(View.VISIBLE); + } + if(!Objects.equals(c_numberOfPosts, "") && c_numberOfPosts != null){ + g_numberOfPosts = (TextView) convertView.findViewById(R.id.number_of_posts); + g_numberOfPosts.setText(c_numberOfPosts); + g_numberOfPosts.setVisibility(View.VISIBLE); + } + if(!Objects.equals(c_personalText, "") && c_personalText != null){ + g_personalText = (TextView) convertView.findViewById(R.id.personal_text); + g_personalText.setText("\"" + c_personalText + "\""); + g_personalText.setVisibility(View.VISIBLE); + } + for(int i=0; i + android:paddingLeft="16dp" + android:paddingRight="16dp" + android:paddingTop="16dp"> + android:layout_centerVertical="true" + android:layout_marginEnd="16dp"> - + + + + + + + + + + + + + + + + +