From a6fd66d561367dbde300f76bcde66e68a9b88873 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sat, 28 Jul 2018 12:04:16 +0300 Subject: [PATCH] move same page checks and add scrolling when posts are focused functionality --- .../activities/topic/TopicActivity.java | 13 +- .../mthmmy/activities/topic/TopicAdapter.java | 50 ++++-- .../activities/topic/tasks/TopicTask.java | 150 +++++------------- 3 files changed, 88 insertions(+), 125 deletions(-) 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 4a8ceb44..cce74faa 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 @@ -58,7 +58,7 @@ import static gr.thmmy.mthmmy.services.NotificationService.NEW_POST_TAG; @SuppressWarnings("unchecked") public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskObserver, DeleteTask.DeleteTaskCallbacks, ReplyTask.ReplyTaskCallbacks, PrepareForEditTask.PrepareForEditCallbacks, - EditTask.EditTaskCallbacks, PrepareForReply.PrepareForReplyCallbacks { + EditTask.EditTaskCallbacks, PrepareForReply.PrepareForReplyCallbacks, TopicAdapter.OnPostFocusChangeListener { //Activity's variables /** * 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(); } topicAdapter.resetTopic(); + recyclerView.scrollToPosition(topicTaskResult.getFocusedPostIndex()); break; case NETWORK_ERROR: Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); 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: progressBar.setVisibility(ProgressBar.GONE); 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(); } + @Override + public void onPostFocusChange(int position) { + recyclerView.scrollToPosition(position); + } + //--------------------------------------BOTTOM NAV BAR METHODS---------------------------------- /** 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 776876d9..6f6b1ff4 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 @@ -31,6 +31,7 @@ import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; +import android.widget.Toast; import com.squareup.picasso.Picasso; @@ -69,6 +70,7 @@ class TopicAdapter extends RecyclerView.Adapter { */ private static int THUMBNAIL_SIZE; private final Context context; + private final OnPostFocusChangeListener postFocusListener; private final ArrayList toQuoteList = new ArrayList<>(); private final List postsList; /** @@ -95,6 +97,7 @@ class TopicAdapter extends RecyclerView.Adapter { TopicAdapter(TopicActivity context, List postsList) { this.context = context; this.postsList = postsList; + this.postFocusListener = context; viewModel = ViewModelProviders.of(context).get(TopicViewModel.class); @@ -683,22 +686,38 @@ class TopicAdapter extends RecyclerView.Adapter { viewModel.stopLoading(); if (target.is(ThmmyPage.PageCategory.TOPIC)) { //This url points to a topic - //Checks if this is the current topic - if (Objects.equals(uriString.substring(0, uriString.lastIndexOf(".")), viewModel.getBaseUrl())) { - //Gets uri's targeted message's index number - String msgIndexReq = uriString.substring(uriString.indexOf("msg") + 3); - if (msgIndexReq.contains("#")) - msgIndexReq = msgIndexReq.substring(0, msgIndexReq.indexOf("#")); - else - msgIndexReq = msgIndexReq.substring(0, msgIndexReq.indexOf(";")); - - //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 + //Checks if the page to be loaded is the one already shown + if (uriString.contains(viewModel.getBaseUrl())) { + Timber.e("reached here!"); + if (uriString.contains("topicseen#new") || uriString.contains("#new")) { + if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { + //same page + postFocusListener.onPostFocusChange(getItemCount() - 1); + Timber.e("new"); 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); @@ -739,6 +758,11 @@ class TopicAdapter extends RecyclerView.Adapter { } + //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 * extension. diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java index c262a326..3ff3cc7a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java @@ -31,11 +31,6 @@ public class TopicTask extends AsyncTask { //----------------------------input data----------------------------- private TopicTaskObserver topicTaskObserver; private OnTopicTaskCompleted finishListener; - /** - * Becomes true when a page is being reloaded - */ - private boolean reloadingPage; - private ArrayList lastPostsList; //-----------------------------output data---------------------------- private ResultCode resultCode; /** @@ -86,31 +81,14 @@ public class TopicTask extends AsyncTask { */ private String lastPageLoadAttemptedUrl; - //consecutive load constructor - public TopicTask(TopicTaskObserver topicTaskObserver, OnTopicTaskCompleted finishListener, - boolean reloadingPage, String baseUrl, int currentPageIndex, int pageCount, - String lastPageLoadAttemptedUrl, - ArrayList 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 public TopicTask(TopicTaskObserver topicTaskObserver, OnTopicTaskCompleted finishListener) { this.topicTaskObserver = topicTaskObserver; this.finishListener = finishListener; - this.reloadingPage = false; this.baseUrl = ""; this.currentPageIndex = 1; this.pageCount = 1; this.lastPageLoadAttemptedUrl = ""; - this.lastPostsList = null; } @Override @@ -120,11 +98,11 @@ public class TopicTask extends AsyncTask { @Override protected TopicTaskResult doInBackground(String... strings) { - Document document = null; + Document topic = null; String newPageUrl = strings[0]; //Finds the index of message focus if present - int postFocus = -1; + int postFocus = 0; { if (newPageUrl.contains("msg")) { String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); @@ -134,26 +112,7 @@ public class TopicTask extends AsyncTask { postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf("#"))); } } - //Checks if the page to be loaded is the one already shown - 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; + if (!Objects.equals(lastPageLoadAttemptedUrl, "")) topicTitle = null; lastPageLoadAttemptedUrl = newPageUrl; if (strings[0].substring(0, strings[0].lastIndexOf(".")).contains("topic=")) @@ -164,8 +123,43 @@ public class TopicTask extends AsyncTask { .build(); try { Response response = BaseApplication.getInstance().getClient().newCall(request).execute(); - document = Jsoup.parse(response.body().string()); - newPostsList = parse(document); + topic = Jsoup.parse(response.body().string()); + + ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); + + //Finds topic's tree, mods and users viewing + topicTreeAndMods = topic.select("div.nav").first().html(); + topicViewers = TopicParser.parseUsersViewingThisTopic(topic, language); + + //Finds reply page url + Element replyButton = topic.select("a:has(img[alt=Reply])").first(); + if (replyButton == null) + replyButton = topic.select("a:has(img[alt=Απάντηση])").first(); + if (replyButton != null) replyPageUrl = replyButton.attr("href"); + + //Finds topic title if missing + topicTitle = topic.select("td[id=top_subject]").first().text(); + if (topicTitle.contains("Topic:")) { + topicTitle = topicTitle.substring(topicTitle.indexOf("Topic:") + 7 + , topicTitle.indexOf("(Read") - 2); + } else { + topicTitle = topicTitle.substring(topicTitle.indexOf("Θέμα:") + 6 + , topicTitle.indexOf("(Αναγνώστηκε") - 2); + Timber.d("Parsed title: %s", topicTitle); + } + + //Finds current page's index + currentPageIndex = TopicParser.parseCurrentPageIndex(topic, language); + + //Finds number of pages + pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language); + + for (int i = 0; i < pageCount; i++) { + //Generate each page's url from topic's base url +".15*numberOfPage" + pagesUrls.put(i, baseUrl + "." + String.valueOf(i * 15)); + } + + newPostsList = TopicParser.parseTopic(topic, language); loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(lastPageLoadAttemptedUrl)); @@ -180,74 +174,17 @@ public class TopicTask extends AsyncTask { } catch (IOException e) { Timber.i(e, "IO Exception"); resultCode = ResultCode.NETWORK_ERROR; - } catch (ParseException e) { - if (isUnauthorized(document)) + } catch (Exception e) { + if (isUnauthorized(topic)) 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 parse(Document topic) throws ParseException { - try { - ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); - - //Finds topic's tree, mods and users viewing - { - topicTreeAndMods = topic.select("div.nav").first().html(); - topicViewers = TopicParser.parseUsersViewingThisTopic(topic, language); - } - - //Finds reply page url - { - Element replyButton = topic.select("a:has(img[alt=Reply])").first(); - if (replyButton == null) - replyButton = topic.select("a:has(img[alt=Απάντηση])").first(); - if (replyButton != null) replyPageUrl = replyButton.attr("href"); - } - - //Finds topic title if missing - { - topicTitle = topic.select("td[id=top_subject]").first().text(); - if (topicTitle.contains("Topic:")) { - topicTitle = topicTitle.substring(topicTitle.indexOf("Topic:") + 7 - , topicTitle.indexOf("(Read") - 2); - } else { - topicTitle = topicTitle.substring(topicTitle.indexOf("Θέμα:") + 6 - , topicTitle.indexOf("(Αναγνώστηκε") - 2); - Timber.d("Parsed title: %s", topicTitle); - } - } - - { //Finds current page's index - currentPageIndex = TopicParser.parseCurrentPageIndex(topic, language); - } - { //Finds number of pages - pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language); - - for (int i = 0; i < pageCount; i++) { - //Generate each page's url from topic's base url +".15*numberOfPage" - pagesUrls.put(i, baseUrl + "." + String.valueOf(i * 15)); - } - } - 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.)," + @@ -261,11 +198,12 @@ public class TopicTask extends AsyncTask { } public enum ResultCode { - SUCCESS, NETWORK_ERROR, PARSING_ERROR, OTHER_ERROR, SAME_PAGE, UNAUTHORIZED + SUCCESS, NETWORK_ERROR, PARSING_ERROR, UNAUTHORIZED } public interface TopicTaskObserver { void onTopicTaskStarted(); + void onTopicTaskCancelled(); }