From 7c82f884d848405dae20311051e6be2802e57973 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sun, 15 Jan 2017 17:39:18 +0200 Subject: [PATCH] General fixes and cleanup, ParseHelpers init --- .../mthmmy/activities/base/BaseActivity.java | 1 - .../activities/profile/ProfileActivity.java | 3 +- .../latestPosts/LatestPostsFragment.java | 7 +- .../activities/topic/TopicActivity.java | 22 +++--- .../mthmmy/activities/topic/TopicAdapter.java | 9 +++ .../mthmmy/activities/topic/TopicParser.java | 77 +++++++------------ .../java/gr/thmmy/mthmmy/model/Category.java | 2 +- .../gr/thmmy/mthmmy/utils/ParseHelpers.java | 47 +++++++++++ .../main/res/layout-v21/activity_profile.xml | 1 + app/src/main/res/layout/activity_profile.xml | 1 + app/src/main/res/values/strings.xml | 2 +- 11 files changed, 101 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java index 16483ea2..4794cf9e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java +++ b/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.Intent; import android.content.SharedPreferences; -import android.graphics.Color; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index 92e505cb..c68befd8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/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()) .into(thumbnailView); usernameView = (TextView) findViewById(R.id.profile_activity_username); - if (username == null || Objects.equals(username, "")) usernameView.setText("Username"); - else usernameView.setText(username); + if (username != null && !Objects.equals(username, "")) usernameView.setText(username); personalTextView = (TextView) findViewById(R.id.profile_activity_personal_text); viewPager = (ViewPager) findViewById(R.id.profile_tab_container); 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 99b52db1..01786f3b 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 @@ -48,7 +48,6 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap * are added in {@link LatestPostsTask}. */ private ArrayList parsedTopicSummaries; - private RecyclerView mainContent; private LatestPostsAdapter latestPostsAdapter; private int numberOfPages = -1; private int pagesLoaded = 0; @@ -90,7 +89,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_latest_posts, container, false); 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); final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); mainContent.setLayoutManager(layoutManager); @@ -193,9 +192,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap } //Parse was successful progressBar.setVisibility(ProgressBar.INVISIBLE); - latestPostsAdapter = new LatestPostsAdapter(fragmentInteractionListener, parsedTopicSummaries); - mainContent.swapAdapter(latestPostsAdapter, false); - //latestPostsAdapter.notifyDataSetChanged(); + latestPostsAdapter.notifyDataSetChanged(); isLoadingMore = false; } 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 85766993..0a2d1e66 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 @@ -23,13 +23,13 @@ import org.jsoup.nodes.Document; import java.io.IOException; import java.util.ArrayList; -import java.util.List; import java.util.Objects; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.activities.base.BaseActivity; import gr.thmmy.mthmmy.model.Post; +import gr.thmmy.mthmmy.utils.ParseHelpers; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import mthmmy.utils.Report; import okhttp3.Request; @@ -58,7 +58,8 @@ public class TopicActivity extends BaseActivity { public static final String BUNDLE_TOPIC_TITLE = "TOPIC_TITLE"; private static TopicTask topicTask; //About posts - private List postsList; + private TopicAdapter topicAdapter; + private ArrayList postsList; private static final int NO_POST_FOCUS = -1; private static int postFocus = NO_POST_FOCUS; private static int postFocusPosition = 0; @@ -119,8 +120,9 @@ public class TopicActivity extends BaseActivity { recyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList, - new TopicTask())); + topicAdapter = new TopicAdapter(getApplicationContext(), progressBar, postsList, + topicTask); + recyclerView.setAdapter(topicAdapter); replyFAB = (FloatingActionButton) findViewById(R.id.topic_fab); //TODO hide fab while logged out replyFAB.setEnabled(false); @@ -383,11 +385,7 @@ public class TopicActivity extends BaseActivity { switch (parseResult) { case SUCCESS: progressBar.setVisibility(ProgressBar.INVISIBLE); - - - recyclerView.swapAdapter(new TopicAdapter(getApplicationContext(), progressBar, - postsList, new TopicTask()), false); - + topicAdapter.customNotifyDataSetChanged(); replyFAB.setEnabled(true); //Set current page @@ -421,7 +419,7 @@ public class TopicActivity extends BaseActivity { * @see org.jsoup.Jsoup Jsoup */ private void parse(Document topic) { - String language = TopicParser.defineLanguage(topic); + ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); //Finds topic title if missing 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); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 64278cab..dae50a99 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -425,6 +425,15 @@ class TopicAdapter extends RecyclerView.Adapter { 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 public int getItemCount() { return postsList.size(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index 34066a3c..538dec17 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/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. - *

Class contains the methods:

  • {@link #parseUsersViewingThisTopic(Document, String)}
  • - *
  • {@link #parseCurrentPageIndex(Document, String)}
  • - *
  • {@link #parseTopicNumberOfPages(Document, int, String)}
  • - *
  • {@link #parseTopic(Document, String)}
  • - *
  • {@link #defineLanguage(Document)}

+ *

Class contains the methods:

  • {@link #parseUsersViewingThisTopic(Document, + * gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • + *
  • {@link #parseCurrentPageIndex(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • + *
  • {@link #parseTopicNumberOfPages(Document, int, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • + *
  • {@link #parseTopic(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • */ 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 private static final int USER_COLOR_BLACK = Color.parseColor("#000000"); private static final int USER_COLOR_RED = Color.parseColor("#F44336"); @@ -56,13 +46,14 @@ class TopicParser { * Returns users currently viewing this topic. * * @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 - * {@link #defineLanguage(Document)} + * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * language set, this is returned by + * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} * @return String containing html with the usernames of users * @see org.jsoup.Jsoup Jsoup */ - static String parseUsersViewingThisTopic(Document topic, String language) { - if (Objects.equals(language, LANGUAGE_GREEK)) + static String parseUsersViewingThisTopic(Document topic, ParseHelpers.Language language) { + if (language.is(ParseHelpers.Language.GREEK)) return topic.select("td:containsOwn(διαβάζουν αυτό το θέμα)").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. * * @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 - * {@link #defineLanguage(Document)} + * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * language set, this is returned by + * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} * @return int containing parsed topic's current page * @see org.jsoup.Jsoup Jsoup */ - static int parseCurrentPageIndex(Document topic, String language) { + static int parseCurrentPageIndex(Document topic, ParseHelpers.Language language) { int parsedPage = 1; - if (Objects.equals(language, LANGUAGE_GREEK)) { + if (language.is(ParseHelpers.Language.GREEK)) { Elements findCurrentPage = topic.select("td:contains(Σελίδες:)>b"); for (Element item : findCurrentPage) { @@ -108,15 +100,16 @@ class TopicParser { * * @param topic {@link Document} object containing this topic's source code * @param currentPage an int containing current page of this topic - * @param language a String containing this topic's language set, this is returned by - * {@link #defineLanguage(Document)} + * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * language set, this is returned by + * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} * @return int containing the number of pages * @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; - if (Objects.equals(language, LANGUAGE_GREEK)) { + if (language.is(ParseHelpers.Language.GREEK)) { Elements pages = topic.select("td:contains(Σελίδες:)>a.navPages"); if (pages.size() != 0) { @@ -145,19 +138,20 @@ class TopicParser { * 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 language a String containing this topic's language set, this is returned by - * {@link #defineLanguage(Document)} + * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * language set, this is returned by + * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} * @return {@link ArrayList} of {@link Post}s * @see org.jsoup.Jsoup Jsoup */ - static ArrayList parseTopic(Document topic, String language) { + static ArrayList parseTopic(Document topic, ParseHelpers.Language language) { //Method's variables final int NO_INDEX = -1; ArrayList parsedPostsList = new ArrayList<>(); Elements postRows; //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(στις)"); else { postRows = topic.select("form[id=quickModForm]>table>tbody>tr:matches(on)"); @@ -213,7 +207,7 @@ class TopicParser { //Language dependent parsing Element userName; - if (Objects.equals(language, LANGUAGE_GREEK)) { + if (language.is(ParseHelpers.Language.GREEK)) { //Finds username and profile's url userName = thisRow.select("a[title^=Εμφάνιση προφίλ του μέλους]").first(); if (userName == null) { //Deleted profile @@ -343,7 +337,7 @@ class TopicParser { Element usersExtraInfo = userName.parent().nextElementSibling(); //Get sibling "div" List infoList = Arrays.asList(usersExtraInfo.html().split("
    ")); - if (Objects.equals(language, LANGUAGE_GREEK)) { + if (language.is(ParseHelpers.Language.GREEK)) { for (String line : infoList) { if (line.contains("Μηνύματα:")) { postsLineIndex = infoList.indexOf(line); @@ -423,23 +417,6 @@ class TopicParser { return parsedPostsList; } - /** - * Returns one of the supported forum languages. - *

    Forum supports:

    • {@link #LANGUAGE_ENGLISH}
    • - *
    • {@link #LANGUAGE_GREEK}

    - * - * @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. * diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Category.java b/app/src/main/java/gr/thmmy/mthmmy/model/Category.java index 648c0bb9..ce43c42d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Category.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Category.java @@ -12,7 +12,7 @@ public class Category implements Parent private final String title; private final String categoryURL; private boolean expanded = false; - private List boards; + private final List boards; public Category(String title, String categoryURL) { this.title = title; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java index 24fb958e..83da1c7c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java @@ -1,5 +1,6 @@ package gr.thmmy.mthmmy.utils; +import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; @@ -8,6 +9,52 @@ import java.util.ArrayList; public class 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. + *

    Forum supports:

    • {@link #ENGLISH}
    • + *
    • {@link #GREEK}

    + * + * @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) { //Fixes embedded videos Elements noembedTag = html.select("noembed"); diff --git a/app/src/main/res/layout-v21/activity_profile.xml b/app/src/main/res/layout-v21/activity_profile.xml index 288ac7ba..9c057147 100644 --- a/app/src/main/res/layout-v21/activity_profile.xml +++ b/app/src/main/res/layout-v21/activity_profile.xml @@ -68,6 +68,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + android:text="@string/username" android:textColor="@color/accent" android:textSize="25sp"/> diff --git a/app/src/main/res/layout/activity_profile.xml b/app/src/main/res/layout/activity_profile.xml index 51b3db32..ea3999e3 100644 --- a/app/src/main/res/layout/activity_profile.xml +++ b/app/src/main/res/layout/activity_profile.xml @@ -67,6 +67,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + android:text="@string/username" android:textColor="@color/accent" android:textSize="25sp"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7226450f..dc72f670 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,7 +25,6 @@ Last post Topics Subject - Locked/Sticky Started by: %1$s Stats Last post on: %1$s @@ -44,6 +43,7 @@ last + Username General Statistics Statistics Posting Activity