|
@ -1,5 +1,6 @@ |
|
|
package gr.thmmy.mthmmy.activities.topic; |
|
|
package gr.thmmy.mthmmy.activities.topic; |
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.content.Intent; |
|
|
import android.content.Intent; |
|
|
import android.graphics.Rect; |
|
|
import android.graphics.Rect; |
|
@ -47,7 +48,8 @@ import gr.thmmy.mthmmy.model.Bookmark; |
|
|
import gr.thmmy.mthmmy.model.Post; |
|
|
import gr.thmmy.mthmmy.model.Post; |
|
|
import gr.thmmy.mthmmy.model.ThmmyPage; |
|
|
import gr.thmmy.mthmmy.model.ThmmyPage; |
|
|
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; |
|
|
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; |
|
|
import gr.thmmy.mthmmy.utils.ParseHelpers; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
|
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; |
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import okhttp3.MultipartBody; |
|
|
import okhttp3.MultipartBody; |
|
|
import okhttp3.Request; |
|
|
import okhttp3.Request; |
|
@ -204,6 +206,8 @@ public class TopicActivity extends BaseActivity { |
|
|
finish(); |
|
|
finish(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
topicPageUrl = ThmmyPage.sanitizeTopicUrl(topicPageUrl); |
|
|
|
|
|
|
|
|
thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true); |
|
|
thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true); |
|
|
|
|
|
|
|
|
//Initializes graphics
|
|
|
//Initializes graphics
|
|
@ -275,7 +279,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
//Gets posts
|
|
|
//Gets posts
|
|
|
topicTask = new TopicTask(); |
|
|
topicTask = new TopicTask(); |
|
|
topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing
|
|
|
topicTask.execute(topicPageUrl); //Attempt data parsing
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -413,6 +417,7 @@ public class TopicActivity extends BaseActivity { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility") |
|
|
private void initIncrementButton(ImageButton increment, final int step) { |
|
|
private void initIncrementButton(ImageButton increment, final int step) { |
|
|
// Increment once for a click
|
|
|
// Increment once for a click
|
|
|
increment.setOnClickListener(new View.OnClickListener() { |
|
|
increment.setOnClickListener(new View.OnClickListener() { |
|
@ -461,6 +466,7 @@ public class TopicActivity extends BaseActivity { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility") |
|
|
private void initDecrementButton(ImageButton decrement, final int step) { |
|
|
private void initDecrementButton(ImageButton decrement, final int step) { |
|
|
// Decrement once for a click
|
|
|
// Decrement once for a click
|
|
|
decrement.setOnClickListener(new View.OnClickListener() { |
|
|
decrement.setOnClickListener(new View.OnClickListener() { |
|
@ -537,6 +543,10 @@ public class TopicActivity extends BaseActivity { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
//------------------------------------BOTTOM NAV BAR METHODS END------------------------------------
|
|
|
//------------------------------------BOTTOM NAV BAR METHODS END------------------------------------
|
|
|
|
|
|
private enum ResultCode { |
|
|
|
|
|
SUCCESS, NETWORK_ERROR, PARSING_ERROR, OTHER_ERROR, SAME_PAGE, UNAUTHORIZED |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* An {@link AsyncTask} that handles asynchronous fetching of this topic page and parsing of its |
|
|
* An {@link AsyncTask} that handles asynchronous fetching of this topic page and parsing of its |
|
@ -544,12 +554,7 @@ public class TopicActivity extends BaseActivity { |
|
|
* <p>TopicTask's {@link AsyncTask#execute execute} method needs a topic's url as String |
|
|
* <p>TopicTask's {@link AsyncTask#execute execute} method needs a topic's url as String |
|
|
* parameter.</p> |
|
|
* parameter.</p> |
|
|
*/ |
|
|
*/ |
|
|
class TopicTask extends AsyncTask<String, Void, Integer> { |
|
|
class TopicTask extends AsyncTask<String, Void, ResultCode> { |
|
|
private static final int SUCCESS = 0; |
|
|
|
|
|
private static final int NETWORK_ERROR = 1; |
|
|
|
|
|
private static final int OTHER_ERROR = 2; |
|
|
|
|
|
private static final int SAME_PAGE = 3; |
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Post> localPostsList; |
|
|
ArrayList<Post> localPostsList; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -559,8 +564,8 @@ public class TopicActivity extends BaseActivity { |
|
|
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(false); |
|
|
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Integer doInBackground(String... strings) { |
|
|
protected ResultCode doInBackground(String... strings) { |
|
|
Document document; |
|
|
Document document = null; |
|
|
String newPageUrl = strings[0]; |
|
|
String newPageUrl = strings[0]; |
|
|
|
|
|
|
|
|
//Finds the index of message focus if present
|
|
|
//Finds the index of message focus if present
|
|
@ -578,7 +583,7 @@ public class TopicActivity extends BaseActivity { |
|
|
if (!reloadingPage && !Objects.equals(loadedPageUrl, "") && newPageUrl.contains(base_url)) { |
|
|
if (!reloadingPage && !Objects.equals(loadedPageUrl, "") && newPageUrl.contains(base_url)) { |
|
|
if (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new")) |
|
|
if (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new")) |
|
|
if (thisPage == numberOfPages) |
|
|
if (thisPage == numberOfPages) |
|
|
return SAME_PAGE; |
|
|
return ResultCode.SAME_PAGE; |
|
|
if (newPageUrl.contains("msg")) { |
|
|
if (newPageUrl.contains("msg")) { |
|
|
String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); |
|
|
String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); |
|
|
if (tmpUrlSbstr.contains("msg")) |
|
|
if (tmpUrlSbstr.contains("msg")) |
|
@ -586,12 +591,12 @@ public class TopicActivity extends BaseActivity { |
|
|
int testAgainst = Integer.parseInt(tmpUrlSbstr); |
|
|
int testAgainst = Integer.parseInt(tmpUrlSbstr); |
|
|
for (Post post : postsList) { |
|
|
for (Post post : postsList) { |
|
|
if (post.getPostIndex() == testAgainst) { |
|
|
if (post.getPostIndex() == testAgainst) { |
|
|
return SAME_PAGE; |
|
|
return ResultCode.SAME_PAGE; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else if ((Objects.equals(newPageUrl, base_url) && thisPage == 1) || |
|
|
} else if ((Objects.equals(newPageUrl, base_url) && thisPage == 1) || |
|
|
Integer.parseInt(newPageUrl.substring(base_url.length() + 1)) / 15 + 1 == thisPage) |
|
|
Integer.parseInt(newPageUrl.substring(base_url.length() + 1)) / 15 + 1 == thisPage) |
|
|
return SAME_PAGE; |
|
|
return ResultCode.SAME_PAGE; |
|
|
} else if (!Objects.equals(loadedPageUrl, "")) topicTitle = null; |
|
|
} else if (!Objects.equals(loadedPageUrl, "")) topicTitle = null; |
|
|
if (reloadingPage) reloadingPage = !reloadingPage; |
|
|
if (reloadingPage) reloadingPage = !reloadingPage; |
|
|
|
|
|
|
|
@ -614,17 +619,22 @@ public class TopicActivity extends BaseActivity { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return SUCCESS; |
|
|
return ResultCode.SUCCESS; |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
Timber.i(e, "IO Exception"); |
|
|
Timber.i(e, "IO Exception"); |
|
|
return NETWORK_ERROR; |
|
|
return ResultCode.NETWORK_ERROR; |
|
|
|
|
|
} catch (ParseException e) { |
|
|
|
|
|
if(isUnauthorized(document)) |
|
|
|
|
|
return ResultCode.UNAUTHORIZED; |
|
|
|
|
|
Timber.e(e, "Parsing Error"); |
|
|
|
|
|
return ResultCode.PARSING_ERROR; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
Timber.e(e, "Exception"); |
|
|
Timber.e(e, "Exception"); |
|
|
return OTHER_ERROR; |
|
|
return ResultCode.OTHER_ERROR; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void onPostExecute(Integer parseResult) { |
|
|
protected void onPostExecute(ResultCode parseResult) { |
|
|
switch (parseResult) { |
|
|
switch (parseResult) { |
|
|
case SUCCESS: |
|
|
case SUCCESS: |
|
|
if (topicTitle == null || Objects.equals(topicTitle, "") |
|
|
if (topicTitle == null || Objects.equals(topicTitle, "") |
|
@ -635,7 +645,7 @@ public class TopicActivity extends BaseActivity { |
|
|
invalidateOptionsMenu(); |
|
|
invalidateOptionsMenu(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!(postsList.isEmpty() || postsList.size() == 0)) { |
|
|
if (!postsList.isEmpty()) { |
|
|
recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug
|
|
|
recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug
|
|
|
postsList.clear(); |
|
|
postsList.clear(); |
|
|
topicAdapter.notifyItemRangeRemoved(0, postsList.size() - 1); |
|
|
topicAdapter.notifyItemRangeRemoved(0, postsList.size() - 1); |
|
@ -661,16 +671,14 @@ public class TopicActivity extends BaseActivity { |
|
|
Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); |
|
|
break; |
|
|
break; |
|
|
case SAME_PAGE: |
|
|
case SAME_PAGE: |
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
stopLoading(); |
|
|
if (replyPageUrl == null) { |
|
|
Toast.makeText(getBaseContext(), "That's the same page", Toast.LENGTH_SHORT).show(); |
|
|
replyFAB.hide(); |
|
|
|
|
|
topicAdapter.resetTopic(base_url, new TopicTask(), false); |
|
|
|
|
|
} else topicAdapter.resetTopic(base_url, new TopicTask(), true); |
|
|
|
|
|
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(true); |
|
|
|
|
|
paginationEnabled(true); |
|
|
|
|
|
Toast.makeText(TopicActivity.this, "That's the same page.", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
//TODO change focus
|
|
|
//TODO change focus
|
|
|
break; |
|
|
break; |
|
|
|
|
|
case UNAUTHORIZED: |
|
|
|
|
|
stopLoading(); |
|
|
|
|
|
Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
break; |
|
|
default: |
|
|
default: |
|
|
//Parse failed - should never happen
|
|
|
//Parse failed - should never happen
|
|
|
Timber.d("Parse failed!"); //TODO report ParseException!!!
|
|
|
Timber.d("Parse failed!"); //TODO report ParseException!!!
|
|
@ -680,13 +688,24 @@ public class TopicActivity extends BaseActivity { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void stopLoading(){ |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
|
|
|
if (replyPageUrl == null) { |
|
|
|
|
|
replyFAB.hide(); |
|
|
|
|
|
topicAdapter.resetTopic(base_url, new TopicTask(), false); |
|
|
|
|
|
} else topicAdapter.resetTopic(base_url, new TopicTask(), true); |
|
|
|
|
|
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(true); |
|
|
|
|
|
paginationEnabled(true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* All the parsing a topic needs. |
|
|
* All the parsing a topic needs. |
|
|
* |
|
|
* |
|
|
* @param topic {@link Document} object containing this topic's source code |
|
|
* @param topic {@link Document} object containing this topic's source code |
|
|
* @see org.jsoup.Jsoup Jsoup |
|
|
* @see org.jsoup.Jsoup Jsoup |
|
|
*/ |
|
|
*/ |
|
|
private ArrayList<Post> parse(Document topic) { |
|
|
private ArrayList<Post> parse(Document topic) throws ParseException{ |
|
|
|
|
|
try { |
|
|
ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); |
|
|
ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); |
|
|
|
|
|
|
|
|
//Finds topic's tree, mods and users viewing
|
|
|
//Finds topic's tree, mods and users viewing
|
|
@ -729,6 +748,16 @@ public class TopicActivity extends BaseActivity { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return TopicParser.parseTopic(topic, language); |
|
|
return TopicParser.parseTopic(topic, language); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new ParseException("Parsing failed (TopicTask)"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean isUnauthorized(Document document) { |
|
|
|
|
|
return document != null && document.select("body:contains(The topic or board you" + |
|
|
|
|
|
" are looking for appears to be either missing or off limits to you.)," + |
|
|
|
|
|
"body:contains(Το θέμα ή πίνακας που ψάχνετε ή δεν υπάρχει ή δεν " + |
|
|
|
|
|
"είναι προσβάσιμο από εσάς.)").size() > 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) { |
|
|
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) { |
|
@ -881,7 +910,7 @@ public class TopicActivity extends BaseActivity { |
|
|
//TODO this...
|
|
|
//TODO this...
|
|
|
return true; |
|
|
return true; |
|
|
default: |
|
|
default: |
|
|
Timber.e("Malformed post. Request string:\n" + post.toString()); |
|
|
Timber.e("Malformed post. Request string: %s", post.toString()); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|