From 77a6b7b96d6a240c9dbbda92c4c867c13a2e3c25 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 20 Jul 2020 21:49:10 +0300 Subject: [PATCH] StatsFragment crash fix --- .../latestPosts/LatestPostsFragment.java | 2 +- .../profile/stats/StatsFragment.java | 127 ++++++++---------- .../res/layout/fragment_profile_stats.xml | 1 - app/src/main/res/values/strings.xml | 1 - build.gradle | 2 +- 5 files changed, 58 insertions(+), 75 deletions(-) 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 c89234ec..35f66a87 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 @@ -175,7 +175,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap protected void onPostExecute(Boolean result) { if (Boolean.FALSE.equals(result)) - Timber.e(new ParseException("Parsing failed(latest posts)"),"ParseException"); + Timber.e(new ParseException("Parsing failed (latest posts)"),"ParseException"); //TODO: This is inaccurate (e.g. can also have an I/O cause) progressBar.setVisibility(ProgressBar.INVISIBLE); latestPostsAdapter.notifyDataSetChanged(); 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 3c084dd5..2fce1ea9 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 @@ -10,7 +10,6 @@ import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; -import android.widget.Toast; import androidx.fragment.app.Fragment; @@ -43,6 +42,7 @@ import javax.net.ssl.SSLHandshakeException; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.Request; import okhttp3.Response; @@ -148,87 +148,73 @@ public class StatsFragment extends Fragment { return false; } - //TODO: better parse error handling (ParseException etc.) @Override protected void onPostExecute(Boolean result) { - if (!result) { //Parse failed! - Timber.d("Parse failed!"); - Toast.makeText(getContext() - , "Fatal error!\n Aborting...", Toast.LENGTH_LONG).show(); - getActivity().finish(); - } - //Parse was successful - populateLayout(); + progressBar.setVisibility(ProgressBar.INVISIBLE); + if (!result) + Timber.e(new ParseException("Parsing failed (user stats)"),"ParseException"); //TODO: This is inaccurate (e.g. can also have an I/O cause) + else + populateLayout(); } + //TODO: better parse error handling (ParseException etc.) 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()) { + if (!statsPage.select("td:contains(No posts to speak of!), 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(); - Pattern pattern = Pattern.compile("(.+)\\s-"); - Matcher matcher = pattern.matcher(generalStatisticsTitle); - if (matcher.find()) - generalStatisticsTitle = matcher.group(1); - - if (userHasPosts) { - postingActivityByTimeTitle = titleRows.get(1).text(); - mostPopularBoardsByPostsTitle = titleRows.last().select("td").first().text(); - mostPopularBoardsByActivityTitle = titleRows.last().select("td").last().text(); - } + + Elements titleRows = statsPage.select("table.bordercolor[align]>tbody>tr.titlebg"); + generalStatisticsTitle = titleRows.first().text(); + Pattern pattern = Pattern.compile("(.+)\\s-"); + Matcher matcher = pattern.matcher(generalStatisticsTitle); + if (matcher.find()) + generalStatisticsTitle = matcher.group(1); + + 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)"); - { - Elements generalStatisticsRows = statsRows.first().select("tbody>tr"); - for (Element generalStatisticsRow : generalStatisticsRows) - generalStatistics += generalStatisticsRow.text() + "\n"; - generalStatistics = generalStatistics.trim(); + + Elements statsRows = statsPage.select("table.bordercolor[align]>tbody>tr:not(.titlebg)"); + Elements generalStatisticsRows = statsRows.first().select("tbody>tr"); + for (Element generalStatisticsRow : generalStatisticsRows) + generalStatistics += generalStatisticsRow.text() + "\n"; + generalStatistics = generalStatistics.trim(); + + 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"); + 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()); } - 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()); - } - 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()); - } - Collections.reverse(mostPopularBoardsByActivityLabels); - } + Collections.reverse(mostPopularBoardsByPostsLabels); + + Elements mostPopularBoardsByActivityRows = statsRows.last().select(">td").last() + .select(">table>tbody>tr"); + 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); } return true; } @@ -243,7 +229,6 @@ public class StatsFragment extends Fragment { if (!userHasPosts) { mainContent.removeViews(2, mainContent.getChildCount() - 2); - //mainContent.removeViews(2, 6); return; } diff --git a/app/src/main/res/layout/fragment_profile_stats.xml b/app/src/main/res/layout/fragment_profile_stats.xml index cf7eefe1..be4ddde2 100644 --- a/app/src/main/res/layout/fragment_profile_stats.xml +++ b/app/src/main/res/layout/fragment_profile_stats.xml @@ -36,7 +36,6 @@ android:id="@+id/general_statistics" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="@string/general_statistics" android:textColor="@color/primary_text"/> Username This user has no posts yet General Statistics - Statistics Posting Activity Most Popular Boards By Posts Most Popular Boards By Activity diff --git a/build.gradle b/build.gradle index 72da44f0..a861f527 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:4.0.1' classpath 'com.google.gms:google-services:4.3.3' - classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.1' + classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0' classpath 'org.ajoberstar.grgit:grgit-core:3.1.1' // Also change in app/gradle/grgit.gradle classpath "com.github.ben-manes:gradle-versions-plugin:0.21.0" }