From d03cd6d0827d3f0fdcf83d67b76e3aff6113c8fa Mon Sep 17 00:00:00 2001 From: Apostolof Date: Thu, 5 Oct 2017 18:03:36 +0300 Subject: [PATCH] User profiles with zero posts fix --- .../latestPosts/LatestPostsAdapter.java | 9 ++ .../latestPosts/LatestPostsFragment.java | 14 ++- .../profile/stats/StatsFragment.java | 86 ++++++++++++------- .../activities/topic/TopicActivity.java | 3 +- .../fragment_latest_posts_empty_message.xml | 14 +++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 app/src/main/res/layout/fragment_latest_posts_empty_message.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java index 9a2696ac..9e8c40ea 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.activities.profile.latestPosts; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,6 +22,7 @@ import me.zhanghai.android.materialprogressbar.MaterialProgressBar; * specified {@link LatestPostsFragment.LatestPostsFragmentInteractionListener}. */ class LatestPostsAdapter extends RecyclerView.Adapter { + private final int VIEW_TYPE_EMPTY = -1; private final int VIEW_TYPE_ITEM = 0; private final int VIEW_TYPE_LOADING = 1; final private LatestPostsFragment.LatestPostsFragmentInteractionListener interactionListener; @@ -38,11 +40,18 @@ class LatestPostsAdapter extends RecyclerView.Adapter { @Override public int getItemViewType(int position) { + if (parsedTopicSummaries.get(position) == null && position == 0) return VIEW_TYPE_EMPTY; return parsedTopicSummaries.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType == VIEW_TYPE_EMPTY) { + Log.d("This", "in"); + View view = LayoutInflater.from(parent.getContext()). + inflate(R.layout.fragment_latest_posts_empty_message, parent, false); + return new RecyclerView.ViewHolder(view){}; + } if (viewType == VIEW_TYPE_ITEM) { View view = LayoutInflater.from(parent.getContext()). inflate(R.layout.fragment_latest_posts_row, parent, false); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java index 11cb671e..a8d75451 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java @@ -50,6 +50,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap private LatestPostsTask profileLatestPostsTask; private MaterialProgressBar progressBar; private boolean isLoadingMore; + private boolean userHasPosts = true; private static final int visibleThreshold = 5; private int lastVisibleItem, totalItemCount; @@ -100,7 +101,8 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap totalItemCount = layoutManager.getItemCount(); lastVisibleItem = layoutManager.findLastVisibleItemPosition(); - if (!isLoadingMore && totalItemCount <= (lastVisibleItem + visibleThreshold)) { + if (userHasPosts && !isLoadingMore && + totalItemCount <= (lastVisibleItem + visibleThreshold)) { isLoadingMore = true; onLoadMore(); } @@ -126,7 +128,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - if (parsedTopicSummaries.isEmpty()) { + if (parsedTopicSummaries.isEmpty() && userHasPosts) { profileLatestPostsTask = new LatestPostsTask(); profileLatestPostsTask.execute(profileUrl + ";sa=showPosts"); pagesLoaded = 1; @@ -186,6 +188,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap //TODO: better parse error handling (ParseException etc.) private boolean parseLatestPosts(Document latestPostsPage) { + //td:contains( Sorry, no matches were found) Elements latestPostsRows = latestPostsPage. select("td:has(table:Contains(Show Posts)):not([style]) > table"); if (latestPostsRows.isEmpty()) { @@ -197,6 +200,13 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap parsedTopicSummaries.remove(parsedTopicSummaries.size() - 1); } + if (!latestPostsRows.select("td:contains(Sorry, no matches were found)").isEmpty() || + !latestPostsRows.select("td:contains(Δυστυχώς δεν βρέθηκε τίποτα)").isEmpty()){ + userHasPosts = false; + parsedTopicSummaries.add(null); + return true; + } + for (Element row : latestPostsRows) { String pTopicUrl, pTopicTitle, pDateTime, pPost; if (Integer.parseInt(row.attr("cellpadding")) == 4) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java index fc1867c0..5822d635 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java @@ -56,6 +56,7 @@ public class StatsFragment extends Fragment { private MaterialProgressBar progressBar; private boolean haveParsed = false; + private boolean userHasPosts = true; private String generalStatisticsTitle = "", generalStatistics = "", postingActivityByTimeTitle = "", mostPopularBoardsByPostsTitle = "", mostPopularBoardsByActivityTitle = ""; final private List postingActivityByTime = new ArrayList<>(); final private List mostPopularBoardsByPosts = new ArrayList<>(), mostPopularBoardsByActivity = new ArrayList<>(); @@ -123,6 +124,7 @@ public class StatsFragment extends Fragment { * as String parameter!

*/ private class ProfileStatsTask extends AsyncTask { + @Override protected void onPreExecute() { progressBar.setVisibility(ProgressBar.VISIBLE); @@ -160,14 +162,23 @@ public class StatsFragment extends Fragment { } private boolean parseStats(Document statsPage) { + //Doesn't go through all the parsing if this user has no posts + if (!statsPage.select("td:contains(No posts to speak of!)").isEmpty()) { + userHasPosts = false; + } + if (!statsPage.select("td:contains(Δεν υπάρχει καμία αποστολή μηνύματος!)").isEmpty()) { + userHasPosts = false; + } if (statsPage.select("table.bordercolor[align]>tbody>tr").size() != 6) return false; { Elements titleRows = statsPage.select("table.bordercolor[align]>tbody>tr.titlebg"); generalStatisticsTitle = titleRows.first().text(); - postingActivityByTimeTitle = titleRows.get(1).text(); - mostPopularBoardsByPostsTitle = titleRows.last().select("td").first().text(); - mostPopularBoardsByActivityTitle = titleRows.last().select("td").last().text(); + if (userHasPosts) { + postingActivityByTimeTitle = titleRows.get(1).text(); + mostPopularBoardsByPostsTitle = titleRows.last().select("td").first().text(); + mostPopularBoardsByActivityTitle = titleRows.last().select("td").last().text(); + } } { Elements statsRows = statsPage.select("table.bordercolor[align]>tbody>tr:not(.titlebg)"); @@ -177,39 +188,41 @@ public class StatsFragment extends Fragment { generalStatistics += generalStatisticsRow.text() + "\n"; generalStatistics = generalStatistics.trim(); } - { - Elements postingActivityByTimeCols = statsRows.get(1).select(">td").last() - .select("tr").first().select("td[width=4%]"); - int i = -1; - for (Element postingActivityByTimeColumn : postingActivityByTimeCols) { - postingActivityByTime.add(new Entry(++i, Float.parseFloat(postingActivityByTimeColumn - .select("img").first().attr("height")))); + if (userHasPosts) { + { + Elements postingActivityByTimeCols = statsRows.get(1).select(">td").last() + .select("tr").first().select("td[width=4%]"); + int i = -1; + for (Element postingActivityByTimeColumn : postingActivityByTimeCols) { + postingActivityByTime.add(new Entry(++i, Float.parseFloat(postingActivityByTimeColumn + .select("img").first().attr("height")))); + } } - } - { - Elements mostPopularBoardsByPostsRows = statsRows.last().select(">td").get(1) - .select(">table>tbody>tr"); - int i = mostPopularBoardsByPostsRows.size(); - for (Element mostPopularBoardsByPostsRow : mostPopularBoardsByPostsRows) { - Elements dataCols = mostPopularBoardsByPostsRow.select("td"); - mostPopularBoardsByPosts.add(new BarEntry(--i, - Integer.parseInt(dataCols.last().text()))); - mostPopularBoardsByPostsLabels.add(dataCols.first().text()); + { + Elements mostPopularBoardsByPostsRows = statsRows.last().select(">td").get(1) + .select(">table>tbody>tr"); + int i = mostPopularBoardsByPostsRows.size(); + for (Element mostPopularBoardsByPostsRow : mostPopularBoardsByPostsRows) { + Elements dataCols = mostPopularBoardsByPostsRow.select("td"); + mostPopularBoardsByPosts.add(new BarEntry(--i, + Integer.parseInt(dataCols.last().text()))); + mostPopularBoardsByPostsLabels.add(dataCols.first().text()); + } + Collections.reverse(mostPopularBoardsByPostsLabels); } - Collections.reverse(mostPopularBoardsByPostsLabels); - } - { - Elements mostPopularBoardsByActivityRows = statsRows.last().select(">td").last() - .select(">table>tbody>tr"); - int i = mostPopularBoardsByActivityRows.size(); - for (Element mostPopularBoardsByActivityRow : mostPopularBoardsByActivityRows) { - Elements dataCols = mostPopularBoardsByActivityRow.select("td"); - String tmp = dataCols.last().text(); - mostPopularBoardsByActivity.add(new BarEntry(--i, - Float.parseFloat(tmp.substring(0, tmp.indexOf("%"))))); - mostPopularBoardsByActivityLabels.add(dataCols.first().text()); + { + Elements mostPopularBoardsByActivityRows = statsRows.last().select(">td").last() + .select(">table>tbody>tr"); + int i = mostPopularBoardsByActivityRows.size(); + for (Element mostPopularBoardsByActivityRow : mostPopularBoardsByActivityRows) { + Elements dataCols = mostPopularBoardsByActivityRow.select("td"); + String tmp = dataCols.last().text(); + mostPopularBoardsByActivity.add(new BarEntry(--i, + Float.parseFloat(tmp.substring(0, tmp.indexOf("%"))))); + mostPopularBoardsByActivityLabels.add(dataCols.first().text()); + } + Collections.reverse(mostPopularBoardsByActivityLabels); } - Collections.reverse(mostPopularBoardsByActivityLabels); } } return true; @@ -221,6 +234,13 @@ public class StatsFragment extends Fragment { .setText(generalStatisticsTitle); ((TextView) mainContent.findViewById(R.id.general_statistics)) .setText(generalStatistics); + + if (!userHasPosts) { + mainContent.removeViews(2, mainContent.getChildCount() - 2); + //mainContent.removeViews(2, 6); + return; + } + ((TextView) mainContent.findViewById(R.id.posting_activity_by_time_title)) .setText(postingActivityByTimeTitle); 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 c8d6ea9c..c95971b0 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 @@ -710,8 +710,7 @@ public class TopicActivity extends BaseActivity { } postsList.clear(); - int oldSize = postsList.size(); - topicAdapter.notifyItemRangeRemoved(0, oldSize); + topicAdapter.notifyItemRangeRemoved(0, postsList.size()); recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug postsList.addAll(TopicParser.parseTopic(topic, language)); } diff --git a/app/src/main/res/layout/fragment_latest_posts_empty_message.xml b/app/src/main/res/layout/fragment_latest_posts_empty_message.xml new file mode 100644 index 00000000..54dd346b --- /dev/null +++ b/app/src/main/res/layout/fragment_latest_posts_empty_message.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a9334851..dbe6cfce 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -51,6 +51,7 @@ Username + This user has no posts yet General Statistics Statistics Posting Activity