@ -8,9 +8,7 @@ import android.view.LayoutInflater;
import android.view.View ;
import android.view.View ;
import android.view.ViewGroup ;
import android.view.ViewGroup ;
import android.widget.LinearLayout ;
import android.widget.LinearLayout ;
import android.widget.ProgressBar ;
import android.widget.TextView ;
import android.widget.TextView ;
import android.widget.Toast ;
import androidx.fragment.app.Fragment ;
import androidx.fragment.app.Fragment ;
@ -43,7 +41,7 @@ import javax.net.ssl.SSLHandshakeException;
import gr.thmmy.mthmmy.R ;
import gr.thmmy.mthmmy.R ;
import gr.thmmy.mthmmy.base.BaseActivity ;
import gr.thmmy.mthmmy.base.BaseActivity ;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar ;
import gr.thmmy.mthmmy.utils.parsing.ParseException ;
import okhttp3.Request ;
import okhttp3.Request ;
import okhttp3.Response ;
import okhttp3.Response ;
import timber.log.Timber ;
import timber.log.Timber ;
@ -56,7 +54,6 @@ public class StatsFragment extends Fragment {
private String profileUrl ;
private String profileUrl ;
private ProfileStatsTask profileStatsTask ;
private ProfileStatsTask profileStatsTask ;
private LinearLayout mainContent ;
private LinearLayout mainContent ;
private MaterialProgressBar progressBar ;
private boolean userHasPosts = true ;
private boolean userHasPosts = true ;
private String generalStatisticsTitle = "" , generalStatistics = "" , postingActivityByTimeTitle = "" , mostPopularBoardsByPostsTitle = "" , mostPopularBoardsByActivityTitle = "" ;
private String generalStatisticsTitle = "" , generalStatistics = "" , postingActivityByTimeTitle = "" , mostPopularBoardsByPostsTitle = "" , mostPopularBoardsByActivityTitle = "" ;
@ -64,6 +61,8 @@ public class StatsFragment extends Fragment {
private final List < BarEntry > mostPopularBoardsByPosts = new ArrayList < > ( ) , mostPopularBoardsByActivity = new ArrayList < > ( ) ;
private final List < BarEntry > mostPopularBoardsByPosts = new ArrayList < > ( ) , mostPopularBoardsByActivity = new ArrayList < > ( ) ;
private final ArrayList < String > mostPopularBoardsByPostsLabels = new ArrayList < > ( ) , mostPopularBoardsByActivityLabels = new ArrayList < > ( ) ;
private final ArrayList < String > mostPopularBoardsByPostsLabels = new ArrayList < > ( ) , mostPopularBoardsByActivityLabels = new ArrayList < > ( ) ;
private OnLoadingListener onLoadingListener ;
public StatsFragment ( ) {
public StatsFragment ( ) {
// Required empty public constructor
// Required empty public constructor
}
}
@ -94,7 +93,6 @@ public class StatsFragment extends Fragment {
Bundle savedInstanceState ) {
Bundle savedInstanceState ) {
final View rootView = inflater . inflate ( R . layout . fragment_profile_stats , container , false ) ;
final View rootView = inflater . inflate ( R . layout . fragment_profile_stats , container , false ) ;
mainContent = rootView . findViewById ( R . id . main_content ) ;
mainContent = rootView . findViewById ( R . id . main_content ) ;
progressBar = rootView . findViewById ( R . id . progressBar ) ;
if ( profileStatsTask ! = null & & profileStatsTask . getStatus ( ) = = AsyncTask . Status . FINISHED )
if ( profileStatsTask ! = null & & profileStatsTask . getStatus ( ) = = AsyncTask . Status . FINISHED )
populateLayout ( ) ;
populateLayout ( ) ;
return rootView ;
return rootView ;
@ -117,6 +115,14 @@ public class StatsFragment extends Fragment {
profileStatsTask . cancel ( true ) ;
profileStatsTask . cancel ( true ) ;
}
}
public interface OnLoadingListener {
void onLoadingStats ( boolean loading ) ;
}
public void setOnLoadingListener ( OnLoadingListener onLoadingListener ) {
this . onLoadingListener = onLoadingListener ;
}
/ * *
/ * *
* An { @link AsyncTask } that handles asynchronous parsing of a profile page ' s data .
* An { @link AsyncTask } that handles asynchronous parsing of a profile page ' s data .
* { @link AsyncTask # onPostExecute ( Object ) OnPostExecute } method calls { @link # ( ) }
* { @link AsyncTask # onPostExecute ( Object ) OnPostExecute } method calls { @link # ( ) }
@ -129,7 +135,7 @@ public class StatsFragment extends Fragment {
@Override
@Override
protected void onPreExecute ( ) {
protected void onPreExecute ( ) {
progressBar . setVisibility ( ProgressBar . VISIBLE ) ;
onLoadingListener . onLoadingStats ( true ) ;
}
}
@Override
@Override
@ -148,30 +154,24 @@ public class StatsFragment extends Fragment {
return false ;
return false ;
}
}
//TODO: better parse error handling (ParseException etc.)
@Override
@Override
protected void onPostExecute ( Boolean result ) {
protected void onPostExecute ( Boolean result ) {
if ( ! result ) { //Parse failed!
onLoadingListener . onLoadingStats ( false ) ;
Timber . d ( "Parse failed!" ) ;
if ( ! result )
Toast . makeText ( getContext ( )
Timber . e ( new ParseException ( "Parsing failed (user stats)" ) , "ParseException" ) ; //TODO: This is inaccurate (e.g. can also have an I/O cause)
, "Fatal error!\n Aborting..." , Toast . LENGTH_LONG ) . show ( ) ;
else
getActivity ( ) . finish ( ) ;
}
//Parse was successful
populateLayout ( ) ;
populateLayout ( ) ;
}
}
//TODO: better parse error handling (ParseException etc.)
private boolean parseStats ( Document statsPage ) {
private boolean parseStats ( Document statsPage ) {
//Doesn't go through all the parsing if this user has no posts
//Doesn't go through all the parsing if this user has no posts
if ( ! statsPage . select ( "td:contains(No posts to speak of!)" ) . isEmpty ( ) ) {
if ( ! statsPage . select ( "td:contains(No posts to speak of!), td:contains(Δεν υπάρχει καμία αποστολή μηνύματος!)" ) . isEmpty ( ) )
userHasPosts = false ;
}
if ( ! statsPage . select ( "td:contains(Δεν υπάρχει καμία αποστολή μηνύματος!)" ) . isEmpty ( ) ) {
userHasPosts = false ;
userHasPosts = false ;
}
if ( statsPage . select ( "table.bordercolor[align]>tbody>tr" ) . size ( ) ! = 6 )
if ( statsPage . select ( "table.bordercolor[align]>tbody>tr" ) . size ( ) ! = 6 )
return false ;
return false ;
{
Elements titleRows = statsPage . select ( "table.bordercolor[align]>tbody>tr.titlebg" ) ;
Elements titleRows = statsPage . select ( "table.bordercolor[align]>tbody>tr.titlebg" ) ;
generalStatisticsTitle = titleRows . first ( ) . text ( ) ;
generalStatisticsTitle = titleRows . first ( ) . text ( ) ;
Pattern pattern = Pattern . compile ( "(.+)\\s-" ) ;
Pattern pattern = Pattern . compile ( "(.+)\\s-" ) ;
@ -184,29 +184,24 @@ public class StatsFragment extends Fragment {
mostPopularBoardsByPostsTitle = titleRows . last ( ) . select ( "td" ) . first ( ) . text ( ) ;
mostPopularBoardsByPostsTitle = titleRows . last ( ) . select ( "td" ) . first ( ) . text ( ) ;
mostPopularBoardsByActivityTitle = titleRows . last ( ) . select ( "td" ) . last ( ) . text ( ) ;
mostPopularBoardsByActivityTitle = titleRows . last ( ) . select ( "td" ) . last ( ) . text ( ) ;
}
}
}
{
Elements statsRows = statsPage . select ( "table.bordercolor[align]>tbody>tr:not(.titlebg)" ) ;
Elements statsRows = statsPage . select ( "table.bordercolor[align]>tbody>tr:not(.titlebg)" ) ;
{
Elements generalStatisticsRows = statsRows . first ( ) . select ( "tbody>tr" ) ;
Elements generalStatisticsRows = statsRows . first ( ) . select ( "tbody>tr" ) ;
for ( Element generalStatisticsRow : generalStatisticsRows )
for ( Element generalStatisticsRow : generalStatisticsRows )
generalStatistics + = generalStatisticsRow . text ( ) + "\n" ;
generalStatistics + = generalStatisticsRow . text ( ) + "\n" ;
generalStatistics = generalStatistics . trim ( ) ;
generalStatistics = generalStatistics . trim ( ) ;
}
if ( userHasPosts ) {
if ( userHasPosts ) {
{
Elements postingActivityByTimeCols = statsRows . get ( 1 ) . select ( ">td" ) . last ( )
Elements postingActivityByTimeCols = statsRows . get ( 1 ) . select ( ">td" ) . last ( )
. select ( "tr" ) . first ( ) . select ( "td[width=4%]" ) ;
. select ( "tr" ) . first ( ) . select ( "td[width=4%]" ) ;
int i = - 1 ;
int i = - 1 ;
for ( Element postingActivityByTimeColumn : postingActivityByTimeCols ) {
for ( Element postingActivityByTimeColumn : postingActivityByTimeCols )
postingActivityByTime . add ( new Entry ( + + i , Float . parseFloat ( postingActivityByTimeColumn
postingActivityByTime . add ( new Entry ( + + i , Float . parseFloat ( postingActivityByTimeColumn
. select ( "img" ) . first ( ) . attr ( "height" ) ) ) ) ;
. select ( "img" ) . first ( ) . attr ( "height" ) ) ) ) ;
}
}
{
Elements mostPopularBoardsByPostsRows = statsRows . last ( ) . select ( ">td" ) . get ( 1 )
Elements mostPopularBoardsByPostsRows = statsRows . last ( ) . select ( ">td" ) . get ( 1 )
. select ( ">table>tbody>tr" ) ;
. select ( ">table>tbody>tr" ) ;
int i = mostPopularBoardsByPostsRows . size ( ) ;
i = mostPopularBoardsByPostsRows . size ( ) ;
for ( Element mostPopularBoardsByPostsRow : mostPopularBoardsByPostsRows ) {
for ( Element mostPopularBoardsByPostsRow : mostPopularBoardsByPostsRows ) {
Elements dataCols = mostPopularBoardsByPostsRow . select ( "td" ) ;
Elements dataCols = mostPopularBoardsByPostsRow . select ( "td" ) ;
mostPopularBoardsByPosts . add ( new BarEntry ( - - i ,
mostPopularBoardsByPosts . add ( new BarEntry ( - - i ,
@ -214,11 +209,10 @@ public class StatsFragment extends Fragment {
mostPopularBoardsByPostsLabels . add ( dataCols . first ( ) . text ( ) ) ;
mostPopularBoardsByPostsLabels . add ( dataCols . first ( ) . text ( ) ) ;
}
}
Collections . reverse ( mostPopularBoardsByPostsLabels ) ;
Collections . reverse ( mostPopularBoardsByPostsLabels ) ;
}
{
Elements mostPopularBoardsByActivityRows = statsRows . last ( ) . select ( ">td" ) . last ( )
Elements mostPopularBoardsByActivityRows = statsRows . last ( ) . select ( ">td" ) . last ( )
. select ( ">table>tbody>tr" ) ;
. select ( ">table>tbody>tr" ) ;
int i = mostPopularBoardsByActivityRows . size ( ) ;
i = mostPopularBoardsByActivityRows . size ( ) ;
for ( Element mostPopularBoardsByActivityRow : mostPopularBoardsByActivityRows ) {
for ( Element mostPopularBoardsByActivityRow : mostPopularBoardsByActivityRows ) {
Elements dataCols = mostPopularBoardsByActivityRow . select ( "td" ) ;
Elements dataCols = mostPopularBoardsByActivityRow . select ( "td" ) ;
String tmp = dataCols . last ( ) . text ( ) ;
String tmp = dataCols . last ( ) . text ( ) ;
@ -228,14 +222,12 @@ public class StatsFragment extends Fragment {
}
}
Collections . reverse ( mostPopularBoardsByActivityLabels ) ;
Collections . reverse ( mostPopularBoardsByActivityLabels ) ;
}
}
}
}
return true ;
return true ;
}
}
}
}
private void populateLayout ( ) {
private void populateLayout ( ) {
progressBar . setVisibility ( ProgressBar . VISIBLE ) ;
onLoadingListener . onLoadingStats ( true ) ; ;
( ( TextView ) mainContent . findViewById ( R . id . general_statistics_title ) )
( ( TextView ) mainContent . findViewById ( R . id . general_statistics_title ) )
. setText ( generalStatisticsTitle ) ;
. setText ( generalStatisticsTitle ) ;
( ( TextView ) mainContent . findViewById ( R . id . general_statistics ) )
( ( TextView ) mainContent . findViewById ( R . id . general_statistics ) )
@ -243,7 +235,7 @@ public class StatsFragment extends Fragment {
if ( ! userHasPosts ) {
if ( ! userHasPosts ) {
mainContent . removeViews ( 2 , mainContent . getChildCount ( ) - 2 ) ;
mainContent . removeViews ( 2 , mainContent . getChildCount ( ) - 2 ) ;
//mainContent.removeViews(2, 6);
onLoadingListener . onLoadingStats ( false ) ;
return ;
return ;
}
}
@ -361,7 +353,7 @@ public class StatsFragment extends Fragment {
mostPopularBoardsByActivityData . setValueTextColor ( Color . WHITE ) ;
mostPopularBoardsByActivityData . setValueTextColor ( Color . WHITE ) ;
mostPopularBoardsByActivityChart . setData ( mostPopularBoardsByActivityData ) ;
mostPopularBoardsByActivityChart . setData ( mostPopularBoardsByActivityData ) ;
mostPopularBoardsByActivityChart . invalidate ( ) ;
mostPopularBoardsByActivityChart . invalidate ( ) ;
progressBar . setVisibility ( ProgressBar . INVISIBLE ) ;
onLoadingListener . onLoadingStats ( false ) ;
}
}
private class MyXAxisValueFormatter implements IAxisValueFormatter {
private class MyXAxisValueFormatter implements IAxisValueFormatter {
@ -373,7 +365,10 @@ public class StatsFragment extends Fragment {
@Override
@Override
public String getFormattedValue ( float value , AxisBase axis ) {
public String getFormattedValue ( float value , AxisBase axis ) {
if ( ( ( int ) value ) < mValues . size ( ) )
return mValues . get ( ( int ) value ) ;
return mValues . get ( ( int ) value ) ;
else
return "0" ;
}
}
}
}
}
}