|
|
@ -1,5 +1,6 @@ |
|
|
|
package gr.thmmy.mthmmy.activities.topic; |
|
|
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Intent; |
|
|
|
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.ThmmyPage; |
|
|
|
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 okhttp3.MultipartBody; |
|
|
|
import okhttp3.Request; |
|
|
@ -204,6 +206,8 @@ public class TopicActivity extends BaseActivity { |
|
|
|
finish(); |
|
|
|
} |
|
|
|
|
|
|
|
topicPageUrl = ThmmyPage.sanitizeTopicUrl(topicPageUrl); |
|
|
|
|
|
|
|
thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true); |
|
|
|
|
|
|
|
//Initializes graphics
|
|
|
@ -275,7 +279,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
|
|
|
|
//Gets posts
|
|
|
|
topicTask = new TopicTask(); |
|
|
|
topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing
|
|
|
|
topicTask.execute(topicPageUrl); //Attempt data parsing
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@ -413,6 +417,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility") |
|
|
|
private void initIncrementButton(ImageButton increment, final int step) { |
|
|
|
// Increment once for a click
|
|
|
|
increment.setOnClickListener(new View.OnClickListener() { |
|
|
@ -461,6 +466,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility") |
|
|
|
private void initDecrementButton(ImageButton decrement, final int step) { |
|
|
|
// Decrement once for a click
|
|
|
|
decrement.setOnClickListener(new View.OnClickListener() { |
|
|
@ -537,6 +543,10 @@ public class TopicActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
//------------------------------------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 |
|
|
@ -544,12 +554,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
* <p>TopicTask's {@link AsyncTask#execute execute} method needs a topic's url as String |
|
|
|
* parameter.</p> |
|
|
|
*/ |
|
|
|
class TopicTask extends AsyncTask<String, Void, Integer> { |
|
|
|
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; |
|
|
|
|
|
|
|
class TopicTask extends AsyncTask<String, Void, ResultCode> { |
|
|
|
ArrayList<Post> localPostsList; |
|
|
|
|
|
|
|
@Override |
|
|
@ -559,8 +564,8 @@ public class TopicActivity extends BaseActivity { |
|
|
|
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(false); |
|
|
|
} |
|
|
|
|
|
|
|
protected Integer doInBackground(String... strings) { |
|
|
|
Document document; |
|
|
|
protected ResultCode doInBackground(String... strings) { |
|
|
|
Document document = null; |
|
|
|
String newPageUrl = strings[0]; |
|
|
|
|
|
|
|
//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 (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new")) |
|
|
|
if (thisPage == numberOfPages) |
|
|
|
return SAME_PAGE; |
|
|
|
return ResultCode.SAME_PAGE; |
|
|
|
if (newPageUrl.contains("msg")) { |
|
|
|
String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); |
|
|
|
if (tmpUrlSbstr.contains("msg")) |
|
|
@ -586,12 +591,12 @@ public class TopicActivity extends BaseActivity { |
|
|
|
int testAgainst = Integer.parseInt(tmpUrlSbstr); |
|
|
|
for (Post post : postsList) { |
|
|
|
if (post.getPostIndex() == testAgainst) { |
|
|
|
return SAME_PAGE; |
|
|
|
return ResultCode.SAME_PAGE; |
|
|
|
} |
|
|
|
} |
|
|
|
} else if ((Objects.equals(newPageUrl, base_url) && thisPage == 1) || |
|
|
|
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; |
|
|
|
if (reloadingPage) reloadingPage = !reloadingPage; |
|
|
|
|
|
|
@ -614,17 +619,22 @@ public class TopicActivity extends BaseActivity { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
return SUCCESS; |
|
|
|
return ResultCode.SUCCESS; |
|
|
|
} catch (IOException e) { |
|
|
|
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) { |
|
|
|
Timber.e(e, "Exception"); |
|
|
|
return OTHER_ERROR; |
|
|
|
return ResultCode.OTHER_ERROR; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected void onPostExecute(Integer parseResult) { |
|
|
|
protected void onPostExecute(ResultCode parseResult) { |
|
|
|
switch (parseResult) { |
|
|
|
case SUCCESS: |
|
|
|
if (topicTitle == null || Objects.equals(topicTitle, "") |
|
|
@ -635,7 +645,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
invalidateOptionsMenu(); |
|
|
|
} |
|
|
|
|
|
|
|
if (!(postsList.isEmpty() || postsList.size() == 0)) { |
|
|
|
if (!postsList.isEmpty()) { |
|
|
|
recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug
|
|
|
|
postsList.clear(); |
|
|
|
topicAdapter.notifyItemRangeRemoved(0, postsList.size() - 1); |
|
|
@ -661,16 +671,14 @@ public class TopicActivity extends BaseActivity { |
|
|
|
Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); |
|
|
|
break; |
|
|
|
case SAME_PAGE: |
|
|
|
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); |
|
|
|
Toast.makeText(TopicActivity.this, "That's the same page.", Toast.LENGTH_SHORT).show(); |
|
|
|
stopLoading(); |
|
|
|
Toast.makeText(getBaseContext(), "That's the same page", Toast.LENGTH_SHORT).show(); |
|
|
|
//TODO change focus
|
|
|
|
break; |
|
|
|
case UNAUTHORIZED: |
|
|
|
stopLoading(); |
|
|
|
Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show(); |
|
|
|
break; |
|
|
|
default: |
|
|
|
//Parse failed - should never happen
|
|
|
|
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. |
|
|
|
* |
|
|
|
* @param topic {@link Document} object containing this topic's source code |
|
|
|
* @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); |
|
|
|
|
|
|
|
//Finds topic's tree, mods and users viewing
|
|
|
@ -729,6 +748,16 @@ public class TopicActivity extends BaseActivity { |
|
|
|
} |
|
|
|
|
|
|
|
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) { |
|
|
@ -881,7 +910,7 @@ public class TopicActivity extends BaseActivity { |
|
|
|
//TODO this...
|
|
|
|
return true; |
|
|
|
default: |
|
|
|
Timber.e("Malformed post. Request string:\n" + post.toString()); |
|
|
|
Timber.e("Malformed post. Request string: %s", post.toString()); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|