Browse Source

move same page checks and add scrolling when posts are focused functionality

pull/34/head
Thodoris1999 7 years ago
parent
commit
a6fd66d561
  1. 13
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 50
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 124
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java

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

@ -58,7 +58,7 @@ import static gr.thmmy.mthmmy.services.NotificationService.NEW_POST_TAG;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskObserver, public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskObserver,
DeleteTask.DeleteTaskCallbacks, ReplyTask.ReplyTaskCallbacks, PrepareForEditTask.PrepareForEditCallbacks, DeleteTask.DeleteTaskCallbacks, ReplyTask.ReplyTaskCallbacks, PrepareForEditTask.PrepareForEditCallbacks,
EditTask.EditTaskCallbacks, PrepareForReply.PrepareForReplyCallbacks { EditTask.EditTaskCallbacks, PrepareForReply.PrepareForReplyCallbacks, TopicAdapter.OnPostFocusChangeListener {
//Activity's variables //Activity's variables
/** /**
* The key to use when putting topic's url String to {@link TopicActivity}'s Bundle. * The key to use when putting topic's url String to {@link TopicActivity}'s Bundle.
@ -238,15 +238,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
replyFAB.show(); replyFAB.show();
} }
topicAdapter.resetTopic(); topicAdapter.resetTopic();
recyclerView.scrollToPosition(topicTaskResult.getFocusedPostIndex());
break; break;
case NETWORK_ERROR: case NETWORK_ERROR:
Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show();
break; break;
case SAME_PAGE:
progressBar.setVisibility(ProgressBar.GONE);
Toast.makeText(getBaseContext(), "That's the same page", Toast.LENGTH_SHORT).show();
//TODO change focus
break;
case UNAUTHORIZED: case UNAUTHORIZED:
progressBar.setVisibility(ProgressBar.GONE); progressBar.setVisibility(ProgressBar.GONE);
Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show();
@ -380,6 +376,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
viewModel.stopLoading(); viewModel.stopLoading();
} }
@Override
public void onPostFocusChange(int position) {
recyclerView.scrollToPosition(position);
}
//--------------------------------------BOTTOM NAV BAR METHODS---------------------------------- //--------------------------------------BOTTOM NAV BAR METHODS----------------------------------
/** /**

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

@ -31,6 +31,7 @@ import android.widget.LinearLayout;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
@ -69,6 +70,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
*/ */
private static int THUMBNAIL_SIZE; private static int THUMBNAIL_SIZE;
private final Context context; private final Context context;
private final OnPostFocusChangeListener postFocusListener;
private final ArrayList<Integer> toQuoteList = new ArrayList<>(); private final ArrayList<Integer> toQuoteList = new ArrayList<>();
private final List<Post> postsList; private final List<Post> postsList;
/** /**
@ -95,6 +97,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
TopicAdapter(TopicActivity context, List<Post> postsList) { TopicAdapter(TopicActivity context, List<Post> postsList) {
this.context = context; this.context = context;
this.postsList = postsList; this.postsList = postsList;
this.postFocusListener = context;
viewModel = ViewModelProviders.of(context).get(TopicViewModel.class); viewModel = ViewModelProviders.of(context).get(TopicViewModel.class);
@ -683,22 +686,38 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
viewModel.stopLoading(); viewModel.stopLoading();
if (target.is(ThmmyPage.PageCategory.TOPIC)) { if (target.is(ThmmyPage.PageCategory.TOPIC)) {
//This url points to a topic //This url points to a topic
//Checks if this is the current topic //Checks if the page to be loaded is the one already shown
if (Objects.equals(uriString.substring(0, uriString.lastIndexOf(".")), viewModel.getBaseUrl())) { if (uriString.contains(viewModel.getBaseUrl())) {
//Gets uri's targeted message's index number Timber.e("reached here!");
String msgIndexReq = uriString.substring(uriString.indexOf("msg") + 3); if (uriString.contains("topicseen#new") || uriString.contains("#new")) {
if (msgIndexReq.contains("#")) if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) {
msgIndexReq = msgIndexReq.substring(0, msgIndexReq.indexOf("#")); //same page
else postFocusListener.onPostFocusChange(getItemCount() - 1);
msgIndexReq = msgIndexReq.substring(0, msgIndexReq.indexOf(";")); Timber.e("new");
//Checks if this post is in the current topic's page
for (Post post : postsList) {
if (post.getPostIndex() == Integer.parseInt(msgIndexReq)) {
// TODO Don't restart Activity, Just change post focus
return true; return true;
} }
} }
if (uriString.contains("msg")) {
String tmpUrlSbstr = uriString.substring(uriString.indexOf("msg") + 3);
if (tmpUrlSbstr.contains("msg"))
tmpUrlSbstr = tmpUrlSbstr.substring(0, tmpUrlSbstr.indexOf("msg") - 1);
int testAgainst = Integer.parseInt(tmpUrlSbstr);
Timber.e("reached tthere! %s", testAgainst);
for (int i = 0; i < postsList.size(); i++) {
if (postsList.get(i).getPostIndex() == testAgainst) {
//same page
Timber.e(Integer.toString(i));
postFocusListener.onPostFocusChange(i);
return true;
}
}
} else if ((Objects.equals(uriString, viewModel.getBaseUrl()) && viewModel.getCurrentPageIndex() == 1) ||
Integer.parseInt(uriString.substring(viewModel.getBaseUrl().length() + 1)) / 15 + 1 ==
viewModel.getCurrentPageIndex()) {
//same page
Timber.e("ha");
return true;
}
} }
Intent intent = new Intent(context, TopicActivity.class); Intent intent = new Intent(context, TopicActivity.class);
@ -739,6 +758,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} }
//we need to set a callback to topic activity to scroll the recyclerview when post focus is requested
public interface OnPostFocusChangeListener {
void onPostFocusChange(int position);
}
/** /**
* Returns a String with a single FontAwesome typeface character corresponding to this file's * Returns a String with a single FontAwesome typeface character corresponding to this file's
* extension. * extension.

124
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java

@ -31,11 +31,6 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
//----------------------------input data----------------------------- //----------------------------input data-----------------------------
private TopicTaskObserver topicTaskObserver; private TopicTaskObserver topicTaskObserver;
private OnTopicTaskCompleted finishListener; private OnTopicTaskCompleted finishListener;
/**
* Becomes true when a page is being reloaded
*/
private boolean reloadingPage;
private ArrayList<Post> lastPostsList;
//-----------------------------output data---------------------------- //-----------------------------output data----------------------------
private ResultCode resultCode; private ResultCode resultCode;
/** /**
@ -86,31 +81,14 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
*/ */
private String lastPageLoadAttemptedUrl; private String lastPageLoadAttemptedUrl;
//consecutive load constructor
public TopicTask(TopicTaskObserver topicTaskObserver, OnTopicTaskCompleted finishListener,
boolean reloadingPage, String baseUrl, int currentPageIndex, int pageCount,
String lastPageLoadAttemptedUrl,
ArrayList<Post> lastPostsList) {
this.topicTaskObserver = topicTaskObserver;
this.finishListener = finishListener;
this.reloadingPage = reloadingPage;
this.baseUrl = baseUrl;
this.currentPageIndex = currentPageIndex;
this.pageCount = pageCount;
this.lastPageLoadAttemptedUrl = lastPageLoadAttemptedUrl;
this.lastPostsList = lastPostsList;
}
// first load or reload constructor // first load or reload constructor
public TopicTask(TopicTaskObserver topicTaskObserver, OnTopicTaskCompleted finishListener) { public TopicTask(TopicTaskObserver topicTaskObserver, OnTopicTaskCompleted finishListener) {
this.topicTaskObserver = topicTaskObserver; this.topicTaskObserver = topicTaskObserver;
this.finishListener = finishListener; this.finishListener = finishListener;
this.reloadingPage = false;
this.baseUrl = ""; this.baseUrl = "";
this.currentPageIndex = 1; this.currentPageIndex = 1;
this.pageCount = 1; this.pageCount = 1;
this.lastPageLoadAttemptedUrl = ""; this.lastPageLoadAttemptedUrl = "";
this.lastPostsList = null;
} }
@Override @Override
@ -120,11 +98,11 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
@Override @Override
protected TopicTaskResult doInBackground(String... strings) { protected TopicTaskResult doInBackground(String... strings) {
Document document = null; Document topic = 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
int postFocus = -1; int postFocus = 0;
{ {
if (newPageUrl.contains("msg")) { if (newPageUrl.contains("msg")) {
String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
@ -134,26 +112,7 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf("#"))); postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf("#")));
} }
} }
//Checks if the page to be loaded is the one already shown if (!Objects.equals(lastPageLoadAttemptedUrl, "")) topicTitle = null;
if (!reloadingPage && !Objects.equals(lastPageLoadAttemptedUrl, "") && newPageUrl.contains(baseUrl)) {
if (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new"))
if (currentPageIndex == pageCount)
resultCode = ResultCode.SAME_PAGE;
if (newPageUrl.contains("msg")) {
String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
if (tmpUrlSbstr.contains("msg"))
tmpUrlSbstr = tmpUrlSbstr.substring(0, tmpUrlSbstr.indexOf("msg") - 1);
int testAgainst = Integer.parseInt(tmpUrlSbstr);
for (Post post : lastPostsList) {
if (post.getPostIndex() == testAgainst) {
resultCode = ResultCode.SAME_PAGE;
}
}
} else if ((Objects.equals(newPageUrl, baseUrl) && currentPageIndex == 1) ||
Integer.parseInt(newPageUrl.substring(baseUrl.length() + 1)) / 15 + 1 ==currentPageIndex)
resultCode = ResultCode.SAME_PAGE;
} else if (!Objects.equals(lastPageLoadAttemptedUrl, "")) topicTitle = null;
lastPageLoadAttemptedUrl = newPageUrl; lastPageLoadAttemptedUrl = newPageUrl;
if (strings[0].substring(0, strings[0].lastIndexOf(".")).contains("topic=")) if (strings[0].substring(0, strings[0].lastIndexOf(".")).contains("topic="))
@ -164,62 +123,21 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
.build(); .build();
try { try {
Response response = BaseApplication.getInstance().getClient().newCall(request).execute(); Response response = BaseApplication.getInstance().getClient().newCall(request).execute();
document = Jsoup.parse(response.body().string()); topic = Jsoup.parse(response.body().string());
newPostsList = parse(document);
loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(lastPageLoadAttemptedUrl));
//Finds the position of the focused message if present
for (int i = 0; i < newPostsList.size(); ++i) {
if (newPostsList.get(i).getPostIndex() == postFocus) {
focusedPostIndex = i;
break;
}
}
resultCode = ResultCode.SUCCESS;
} catch (IOException e) {
Timber.i(e, "IO Exception");
resultCode = ResultCode.NETWORK_ERROR;
} catch (ParseException e) {
if (isUnauthorized(document))
resultCode = ResultCode.UNAUTHORIZED;
Timber.e(e, "Parsing Error");
resultCode = ResultCode.PARSING_ERROR;
} catch (Exception e) {
Timber.e(e, "Exception");
resultCode = ResultCode.OTHER_ERROR;
}
return new TopicTaskResult(resultCode, baseUrl, topicTitle, replyPageUrl, newPostsList,
loadedPageTopicId, currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods,
topicViewers, lastPageLoadAttemptedUrl, pagesUrls);
}
/**
* 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) 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
{
topicTreeAndMods = topic.select("div.nav").first().html(); topicTreeAndMods = topic.select("div.nav").first().html();
topicViewers = TopicParser.parseUsersViewingThisTopic(topic, language); topicViewers = TopicParser.parseUsersViewingThisTopic(topic, language);
}
//Finds reply page url //Finds reply page url
{
Element replyButton = topic.select("a:has(img[alt=Reply])").first(); Element replyButton = topic.select("a:has(img[alt=Reply])").first();
if (replyButton == null) if (replyButton == null)
replyButton = topic.select("a:has(img[alt=Απάντηση])").first(); replyButton = topic.select("a:has(img[alt=Απάντηση])").first();
if (replyButton != null) replyPageUrl = replyButton.attr("href"); if (replyButton != null) replyPageUrl = replyButton.attr("href");
}
//Finds topic title if missing //Finds topic title if missing
{
topicTitle = topic.select("td[id=top_subject]").first().text(); topicTitle = topic.select("td[id=top_subject]").first().text();
if (topicTitle.contains("Topic:")) { if (topicTitle.contains("Topic:")) {
topicTitle = topicTitle.substring(topicTitle.indexOf("Topic:") + 7 topicTitle = topicTitle.substring(topicTitle.indexOf("Topic:") + 7
@ -229,23 +147,42 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
, topicTitle.indexOf("(Αναγνώστηκε") - 2); , topicTitle.indexOf("(Αναγνώστηκε") - 2);
Timber.d("Parsed title: %s", topicTitle); Timber.d("Parsed title: %s", topicTitle);
} }
}
{ //Finds current page's index //Finds current page's index
currentPageIndex = TopicParser.parseCurrentPageIndex(topic, language); currentPageIndex = TopicParser.parseCurrentPageIndex(topic, language);
}
{ //Finds number of pages //Finds number of pages
pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language); pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language);
for (int i = 0; i < pageCount; i++) { for (int i = 0; i < pageCount; i++) {
//Generate each page's url from topic's base url +".15*numberOfPage" //Generate each page's url from topic's base url +".15*numberOfPage"
pagesUrls.put(i, baseUrl + "." + String.valueOf(i * 15)); pagesUrls.put(i, baseUrl + "." + String.valueOf(i * 15));
} }
newPostsList = TopicParser.parseTopic(topic, language);
loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(lastPageLoadAttemptedUrl));
//Finds the position of the focused message if present
for (int i = 0; i < newPostsList.size(); ++i) {
if (newPostsList.get(i).getPostIndex() == postFocus) {
focusedPostIndex = i;
break;
}
} }
return TopicParser.parseTopic(topic, language); resultCode = ResultCode.SUCCESS;
} catch (IOException e) {
Timber.i(e, "IO Exception");
resultCode = ResultCode.NETWORK_ERROR;
} catch (Exception e) { } catch (Exception e) {
throw new ParseException("Parsing failed (TopicTask)"); if (isUnauthorized(topic))
resultCode = ResultCode.UNAUTHORIZED;
Timber.e(e, "Parsing Error");
resultCode = ResultCode.PARSING_ERROR;
} }
return new TopicTaskResult(resultCode, baseUrl, topicTitle, replyPageUrl, newPostsList,
loadedPageTopicId, currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods,
topicViewers, lastPageLoadAttemptedUrl, pagesUrls);
} }
private boolean isUnauthorized(Document document) { private boolean isUnauthorized(Document document) {
@ -261,11 +198,12 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
} }
public enum ResultCode { public enum ResultCode {
SUCCESS, NETWORK_ERROR, PARSING_ERROR, OTHER_ERROR, SAME_PAGE, UNAUTHORIZED SUCCESS, NETWORK_ERROR, PARSING_ERROR, UNAUTHORIZED
} }
public interface TopicTaskObserver { public interface TopicTaskObserver {
void onTopicTaskStarted(); void onTopicTaskStarted();
void onTopicTaskCancelled(); void onTopicTaskCancelled();
} }

Loading…
Cancel
Save