Browse Source

General fixes and cleanup, ParseHelpers init

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
7c82f884d8
  1. 1
      app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java
  2. 3
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  3. 7
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java
  4. 22
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  5. 9
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  6. 77
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  7. 2
      app/src/main/java/gr/thmmy/mthmmy/model/Category.java
  8. 47
      app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java
  9. 1
      app/src/main/res/layout-v21/activity_profile.xml
  10. 1
      app/src/main/res/layout/activity_profile.xml
  11. 2
      app/src/main/res/values/strings.xml

1
app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java

@ -4,7 +4,6 @@ import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;

3
app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

@ -125,8 +125,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
.transform(new CircleTransform()) .transform(new CircleTransform())
.into(thumbnailView); .into(thumbnailView);
usernameView = (TextView) findViewById(R.id.profile_activity_username); usernameView = (TextView) findViewById(R.id.profile_activity_username);
if (username == null || Objects.equals(username, "")) usernameView.setText("Username"); if (username != null && !Objects.equals(username, "")) usernameView.setText(username);
else usernameView.setText(username);
personalTextView = (TextView) findViewById(R.id.profile_activity_personal_text); personalTextView = (TextView) findViewById(R.id.profile_activity_personal_text);
viewPager = (ViewPager) findViewById(R.id.profile_tab_container); viewPager = (ViewPager) findViewById(R.id.profile_tab_container);

7
app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java

@ -48,7 +48,6 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap
* are added in {@link LatestPostsTask}. * are added in {@link LatestPostsTask}.
*/ */
private ArrayList<PostSummary> parsedTopicSummaries; private ArrayList<PostSummary> parsedTopicSummaries;
private RecyclerView mainContent;
private LatestPostsAdapter latestPostsAdapter; private LatestPostsAdapter latestPostsAdapter;
private int numberOfPages = -1; private int numberOfPages = -1;
private int pagesLoaded = 0; private int pagesLoaded = 0;
@ -90,7 +89,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap
Bundle savedInstanceState) { Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_latest_posts, container, false); final View rootView = inflater.inflate(R.layout.fragment_latest_posts, container, false);
latestPostsAdapter = new LatestPostsAdapter(fragmentInteractionListener, parsedTopicSummaries); latestPostsAdapter = new LatestPostsAdapter(fragmentInteractionListener, parsedTopicSummaries);
mainContent = (RecyclerView) rootView.findViewById(R.id.profile_latest_posts_recycler); RecyclerView mainContent = (RecyclerView) rootView.findViewById(R.id.profile_latest_posts_recycler);
mainContent.setAdapter(latestPostsAdapter); mainContent.setAdapter(latestPostsAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mainContent.setLayoutManager(layoutManager); mainContent.setLayoutManager(layoutManager);
@ -193,9 +192,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap
} }
//Parse was successful //Parse was successful
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
latestPostsAdapter = new LatestPostsAdapter(fragmentInteractionListener, parsedTopicSummaries); latestPostsAdapter.notifyDataSetChanged();
mainContent.swapAdapter(latestPostsAdapter, false);
//latestPostsAdapter.notifyDataSetChanged();
isLoadingMore = false; isLoadingMore = false;
} }

22
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -23,13 +23,13 @@ import org.jsoup.nodes.Document;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.activities.base.BaseActivity; import gr.thmmy.mthmmy.activities.base.BaseActivity;
import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.utils.ParseHelpers;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report; import mthmmy.utils.Report;
import okhttp3.Request; import okhttp3.Request;
@ -58,7 +58,8 @@ public class TopicActivity extends BaseActivity {
public static final String BUNDLE_TOPIC_TITLE = "TOPIC_TITLE"; public static final String BUNDLE_TOPIC_TITLE = "TOPIC_TITLE";
private static TopicTask topicTask; private static TopicTask topicTask;
//About posts //About posts
private List<Post> postsList; private TopicAdapter topicAdapter;
private ArrayList<Post> postsList;
private static final int NO_POST_FOCUS = -1; private static final int NO_POST_FOCUS = -1;
private static int postFocus = NO_POST_FOCUS; private static int postFocus = NO_POST_FOCUS;
private static int postFocusPosition = 0; private static int postFocusPosition = 0;
@ -119,8 +120,9 @@ public class TopicActivity extends BaseActivity {
recyclerView.setHasFixedSize(true); recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList, topicAdapter = new TopicAdapter(getApplicationContext(), progressBar, postsList,
new TopicTask())); topicTask);
recyclerView.setAdapter(topicAdapter);
replyFAB = (FloatingActionButton) findViewById(R.id.topic_fab); //TODO hide fab while logged out replyFAB = (FloatingActionButton) findViewById(R.id.topic_fab); //TODO hide fab while logged out
replyFAB.setEnabled(false); replyFAB.setEnabled(false);
@ -383,11 +385,7 @@ public class TopicActivity extends BaseActivity {
switch (parseResult) { switch (parseResult) {
case SUCCESS: case SUCCESS:
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
topicAdapter.customNotifyDataSetChanged();
recyclerView.swapAdapter(new TopicAdapter(getApplicationContext(), progressBar,
postsList, new TopicTask()), false);
replyFAB.setEnabled(true); replyFAB.setEnabled(true);
//Set current page //Set current page
@ -421,7 +419,7 @@ public class TopicActivity extends BaseActivity {
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
private void parse(Document topic) { private void parse(Document topic) {
String language = TopicParser.defineLanguage(topic); ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic);
//Finds topic title if missing //Finds topic title if missing
if (topicTitle == null || Objects.equals(topicTitle, "")) { if (topicTitle == null || Objects.equals(topicTitle, "")) {
@ -448,7 +446,9 @@ public class TopicActivity extends BaseActivity {
} }
} }
postsList = TopicParser.parseTopic(topic, language); postsList.clear();
postsList.addAll(TopicParser.parseTopic(topic, language));
//postsList = TopicParser.parseTopic(topic, language);
} }
} }

9
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -425,6 +425,15 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
holder.post.setOnTouchListener(new CustomTouchListener(holder.post, holder.cardView)); holder.post.setOnTouchListener(new CustomTouchListener(holder.post, holder.cardView));
} }
void customNotifyDataSetChanged() {
viewProperties.clear();
for (int i = 0; i < postsList.size(); ++i) {
//Initializes properties, array's values will be false by default
viewProperties.add(new boolean[3]);
}
notifyDataSetChanged();
}
@Override @Override
public int getItemCount() { public int getItemCount() {
return postsList.size(); return postsList.size();

77
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java

@ -21,23 +21,13 @@ import mthmmy.utils.Report;
/** /**
* Singleton used for parsing a topic. * Singleton used for parsing a topic.
* <p>Class contains the methods:<ul><li>{@link #parseUsersViewingThisTopic(Document, String)}</li> * <p>Class contains the methods:<ul><li>{@link #parseUsersViewingThisTopic(Document,
* <li>{@link #parseCurrentPageIndex(Document, String)}</li> * gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li>
* <li>{@link #parseTopicNumberOfPages(Document, int, String)}</li> * <li>{@link #parseCurrentPageIndex(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li>
* <li>{@link #parseTopic(Document, String)}</li> * <li>{@link #parseTopicNumberOfPages(Document, int, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li>
* <li>{@link #defineLanguage(Document)}</li></ul></p> * <li>{@link #parseTopic(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li>
*/ */
class TopicParser { class TopicParser {
//Languages supported
/**
* String constant containing one of the supported forum languages
*/
private static final String LANGUAGE_GREEK = "Greek";
/**
* String constant containing one of the supported forum languages
*/
private static final String LANGUAGE_ENGLISH = "English";
//User colors //User colors
private static final int USER_COLOR_BLACK = Color.parseColor("#000000"); private static final int USER_COLOR_BLACK = Color.parseColor("#000000");
private static final int USER_COLOR_RED = Color.parseColor("#F44336"); private static final int USER_COLOR_RED = Color.parseColor("#F44336");
@ -56,13 +46,14 @@ class TopicParser {
* Returns users currently viewing this topic. * Returns users currently viewing this topic.
* *
* @param topic {@link Document} object containing this topic's source code * @param topic {@link Document} object containing this topic's source code
* @param language a String containing this topic's language set, this is returned by * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's
* {@link #defineLanguage(Document)} * language set, this is returned by
* {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)}
* @return String containing html with the usernames of users * @return String containing html with the usernames of users
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
static String parseUsersViewingThisTopic(Document topic, String language) { static String parseUsersViewingThisTopic(Document topic, ParseHelpers.Language language) {
if (Objects.equals(language, LANGUAGE_GREEK)) if (language.is(ParseHelpers.Language.GREEK))
return topic.select("td:containsOwn(διαβάζουν αυτό το θέμα)").first().html(); return topic.select("td:containsOwn(διαβάζουν αυτό το θέμα)").first().html();
return topic.select("td:containsOwn(are viewing this topic)").first().html(); return topic.select("td:containsOwn(are viewing this topic)").first().html();
} }
@ -71,15 +62,16 @@ class TopicParser {
* Returns current topic's page index. * Returns current topic's page index.
* *
* @param topic {@link Document} object containing this topic's source code * @param topic {@link Document} object containing this topic's source code
* @param language a String containing this topic's language set, this is returned by * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's
* {@link #defineLanguage(Document)} * language set, this is returned by
* {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)}
* @return int containing parsed topic's current page * @return int containing parsed topic's current page
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
static int parseCurrentPageIndex(Document topic, String language) { static int parseCurrentPageIndex(Document topic, ParseHelpers.Language language) {
int parsedPage = 1; int parsedPage = 1;
if (Objects.equals(language, LANGUAGE_GREEK)) { if (language.is(ParseHelpers.Language.GREEK)) {
Elements findCurrentPage = topic.select("td:contains(Σελίδες:)>b"); Elements findCurrentPage = topic.select("td:contains(Σελίδες:)>b");
for (Element item : findCurrentPage) { for (Element item : findCurrentPage) {
@ -108,15 +100,16 @@ class TopicParser {
* *
* @param topic {@link Document} object containing this topic's source code * @param topic {@link Document} object containing this topic's source code
* @param currentPage an int containing current page of this topic * @param currentPage an int containing current page of this topic
* @param language a String containing this topic's language set, this is returned by * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's
* {@link #defineLanguage(Document)} * language set, this is returned by
* {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)}
* @return int containing the number of pages * @return int containing the number of pages
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
static int parseTopicNumberOfPages(Document topic, int currentPage, String language) { static int parseTopicNumberOfPages(Document topic, int currentPage, ParseHelpers.Language language) {
int returnPages = 1; int returnPages = 1;
if (Objects.equals(language, LANGUAGE_GREEK)) { if (language.is(ParseHelpers.Language.GREEK)) {
Elements pages = topic.select("td:contains(Σελίδες:)>a.navPages"); Elements pages = topic.select("td:contains(Σελίδες:)>a.navPages");
if (pages.size() != 0) { if (pages.size() != 0) {
@ -145,19 +138,20 @@ class TopicParser {
* This method parses all the information of a topic and it's posts. * This method parses all the information of a topic and it's posts.
* *
* @param topic {@link Document} object containing this topic's source code * @param topic {@link Document} object containing this topic's source code
* @param language a String containing this topic's language set, this is returned by * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's
* {@link #defineLanguage(Document)} * language set, this is returned by
* {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)}
* @return {@link ArrayList} of {@link Post}s * @return {@link ArrayList} of {@link Post}s
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
static ArrayList<Post> parseTopic(Document topic, String language) { static ArrayList<Post> parseTopic(Document topic, ParseHelpers.Language language) {
//Method's variables //Method's variables
final int NO_INDEX = -1; final int NO_INDEX = -1;
ArrayList<Post> parsedPostsList = new ArrayList<>(); ArrayList<Post> parsedPostsList = new ArrayList<>();
Elements postRows; Elements postRows;
//Each row is a post //Each row is a post
if (Objects.equals(language, LANGUAGE_GREEK)) if (language.is(ParseHelpers.Language.GREEK))
postRows = topic.select("form[id=quickModForm]>table>tbody>tr:matches(στις)"); postRows = topic.select("form[id=quickModForm]>table>tbody>tr:matches(στις)");
else { else {
postRows = topic.select("form[id=quickModForm]>table>tbody>tr:matches(on)"); postRows = topic.select("form[id=quickModForm]>table>tbody>tr:matches(on)");
@ -213,7 +207,7 @@ class TopicParser {
//Language dependent parsing //Language dependent parsing
Element userName; Element userName;
if (Objects.equals(language, LANGUAGE_GREEK)) { if (language.is(ParseHelpers.Language.GREEK)) {
//Finds username and profile's url //Finds username and profile's url
userName = thisRow.select("a[title^=Εμφάνιση προφίλ του μέλους]").first(); userName = thisRow.select("a[title^=Εμφάνιση προφίλ του μέλους]").first();
if (userName == null) { //Deleted profile if (userName == null) { //Deleted profile
@ -343,7 +337,7 @@ class TopicParser {
Element usersExtraInfo = userName.parent().nextElementSibling(); //Get sibling "div" Element usersExtraInfo = userName.parent().nextElementSibling(); //Get sibling "div"
List<String> infoList = Arrays.asList(usersExtraInfo.html().split("<br>")); List<String> infoList = Arrays.asList(usersExtraInfo.html().split("<br>"));
if (Objects.equals(language, LANGUAGE_GREEK)) { if (language.is(ParseHelpers.Language.GREEK)) {
for (String line : infoList) { for (String line : infoList) {
if (line.contains("Μηνύματα:")) { if (line.contains("Μηνύματα:")) {
postsLineIndex = infoList.indexOf(line); postsLineIndex = infoList.indexOf(line);
@ -423,23 +417,6 @@ class TopicParser {
return parsedPostsList; return parsedPostsList;
} }
/**
* Returns one of the supported forum languages.
* <p>Forum supports: <ul><li>{@link #LANGUAGE_ENGLISH}</li>
* <li>{@link #LANGUAGE_GREEK}</li></ul></p>
*
* @param topic {@link Document} object containing this topic's source code
* @return String containing the language of a topic
* @see org.jsoup.Jsoup Jsoup
*/
static String defineLanguage(Document topic) {
if (topic.select("h3").text().contains("Καλώς ορίσατε")) {
return LANGUAGE_GREEK;
} else { //Default is english (eg. guest's language)
return LANGUAGE_ENGLISH;
}
}
/** /**
* Returns the color of a user according to user's rank on forum. * Returns the color of a user according to user's rank on forum.
* *

2
app/src/main/java/gr/thmmy/mthmmy/model/Category.java

@ -12,7 +12,7 @@ public class Category implements Parent<Board>
private final String title; private final String title;
private final String categoryURL; private final String categoryURL;
private boolean expanded = false; private boolean expanded = false;
private List<Board> boards; private final List<Board> boards;
public Category(String title, String categoryURL) { public Category(String title, String categoryURL) {
this.title = title; this.title = title;

47
app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java

@ -1,5 +1,6 @@
package gr.thmmy.mthmmy.utils; package gr.thmmy.mthmmy.utils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
@ -8,6 +9,52 @@ import java.util.ArrayList;
public class ParseHelpers { public class ParseHelpers {
private static final String TAG = "ParseHelpers"; private static final String TAG = "ParseHelpers";
public enum Language {
GREEK, ENGLISH, ENGLISH_GUEST, PAGE_INCOMPLETE, UNDEFINED_LANGUAGE;
/**
* Returns one of the supported forum languages.
* <p>Forum supports: <ul><li>{@link #ENGLISH}</li>
* <li>{@link #GREEK}</li></ul></p>
*
* @param page {@link Document} object containing this page's source code
* @return String containing the language of a topic
* @see org.jsoup.Jsoup Jsoup
*/
public static Language getLanguage(Document page) {
Element welcoming = page.select("h3").first();
if (welcoming == null) {
if (page.select("div[id=myuser]").first().text().contains("Welcome"))
return ENGLISH_GUEST;
return PAGE_INCOMPLETE;
} else if (welcoming.text().contains("Καλώς ορίσατε")) return GREEK;
else if (welcoming.text().contains("Hey")) return ENGLISH;
else return UNDEFINED_LANGUAGE;
}
public boolean is(Language other) {
return this == ENGLISH && other == ENGLISH_GUEST ||
this == ENGLISH_GUEST && other == ENGLISH ||
this == other;
}
}
public enum State {
UNAUTHORIZED_OR_MISSING, NEW_PM, READY;
public static State getState(Document page) {
Elements warnings = page.select("form[id=frmLogin] tr.catbg~tr>td.windowbg");
if (warnings != null) {
for (Element warning : warnings) {
if (warning.text().contains("The topic or board you are looking for appears " +
"to be either missing or off limits to you."))
return UNAUTHORIZED_OR_MISSING;
}
}
return READY;
}
}
public static String youtubeEmbeddedFix(Element html) { public static String youtubeEmbeddedFix(Element html) {
//Fixes embedded videos //Fixes embedded videos
Elements noembedTag = html.select("noembed"); Elements noembedTag = html.select("noembed");

1
app/src/main/res/layout-v21/activity_profile.xml

@ -68,6 +68,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:text="@string/username"
android:textColor="@color/accent" android:textColor="@color/accent"
android:textSize="25sp"/> android:textSize="25sp"/>
</android.support.v7.widget.Toolbar> </android.support.v7.widget.Toolbar>

1
app/src/main/res/layout/activity_profile.xml

@ -67,6 +67,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:text="@string/username"
android:textColor="@color/accent" android:textColor="@color/accent"
android:textSize="25sp"/> android:textSize="25sp"/>
</android.support.v7.widget.Toolbar> </android.support.v7.widget.Toolbar>

2
app/src/main/res/values/strings.xml

@ -25,7 +25,6 @@
<string name="child_board_last_post">Last post</string> <string name="child_board_last_post">Last post</string>
<string name="topic_title">Topics</string> <string name="topic_title">Topics</string>
<string name="topic_subject">Subject</string> <string name="topic_subject">Subject</string>
<string name="topic_locked_sticky">Locked/Sticky</string>
<string name="topic_started_by">Started by: %1$s</string> <string name="topic_started_by">Started by: %1$s</string>
<string name="topic_stats">Stats</string> <string name="topic_stats">Stats</string>
<string name="topic_last_post">Last post on: %1$s</string> <string name="topic_last_post">Last post on: %1$s</string>
@ -44,6 +43,7 @@
<string name="button_last">last</string> <string name="button_last">last</string>
<!--Profile Activity--> <!--Profile Activity-->
<string name="username">Username</string>
<string name="general_statistics_title">General Statistics</string> <string name="general_statistics_title">General Statistics</string>
<string name="general_statistics">Statistics</string> <string name="general_statistics">Statistics</string>
<string name="posting_activity_by_time_title">Posting Activity</string> <string name="posting_activity_by_time_title">Posting Activity</string>

Loading…
Cancel
Save