From 9f3d0be3c91842c2d11f4a04f6b24a501bec60b8 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 7 Aug 2018 22:38:44 +0300 Subject: [PATCH] fix issues with topic, cleanup --- .../activities/topic/TopicActivity.java | 118 ++++++++-------- .../mthmmy/activities/topic/TopicAdapter.java | 11 +- .../activities/topic/tasks/TopicTask.java | 53 +++---- .../topic/tasks/TopicTaskResult.java | 31 +---- .../mthmmy/utils/parsing/ParseHelpers.java | 20 +++ .../mthmmy/viewmodel/TopicViewModel.java | 131 ++++++++++-------- 6 files changed, 179 insertions(+), 185 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 2ed8e25b..ddf5f0d7 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 @@ -45,6 +45,7 @@ import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; import gr.thmmy.mthmmy.utils.HTMLUtils; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.viewmodel.TopicViewModel; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import timber.log.Timber; @@ -206,53 +207,56 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" + String.valueOf(viewModel.getPageCount())); }); - viewModel.getTopicTaskResult().observe(this, topicTaskResult -> { - if (topicTaskResult == null) { - progressBar.setVisibility(ProgressBar.VISIBLE); - } else { - switch (topicTaskResult.getResultCode()) { - case SUCCESS: - if (topicTitle == null || Objects.equals(topicTitle, "") - || !Objects.equals(topicTitle, topicTaskResult.getTopicTitle())) { - toolbarTitle.setText(topicTaskResult.getTopicTitle()); - } - - recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug - postsList.clear(); - postsList.addAll(topicTaskResult.getNewPostsList()); - topicAdapter.notifyDataSetChanged(); - - paginationEnabled(true); - - if (topicTaskResult.getCurrentPageIndex() == topicTaskResult.getPageCount()) { - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - if (notificationManager != null) - notificationManager.cancel(NEW_POST_TAG, topicTaskResult.getLoadedPageTopicId()); - } - - progressBar.setVisibility(ProgressBar.GONE); - if (topicTaskResult.getReplyPageUrl() == null) - replyFAB.hide(); - else - replyFAB.show(); - recyclerView.scrollToPosition(topicTaskResult.getFocusedPostIndex()); - break; - case NETWORK_ERROR: - Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); - break; - case UNAUTHORIZED: - progressBar.setVisibility(ProgressBar.GONE); - 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!!! - Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show(); - finish(); - break; - } + viewModel.getTopicTitle().observe(this, newTopicTitle -> { + if (newTopicTitle == null) return; + toolbarTitle.setText(newTopicTitle); + }); + viewModel.getPageTopicId().observe(this, pageTopicId -> { + if (pageTopicId == null) return; + if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager != null) + notificationManager.cancel(NEW_POST_TAG, pageTopicId); + } + }); + viewModel.getReplyPageUrl().observe(this, replyPageUrl -> { + if (replyPageUrl == null) + replyFAB.hide(); + else + replyFAB.show(); + }); + viewModel.getPostsList().observe(this, postList -> { + if (postList == null) onTopicTaskStarted(); + recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug + postsList.clear(); + postsList.addAll(postList); + topicAdapter.notifyDataSetChanged(); + }); + viewModel.getFocusedPostIndex().observe(this, focusedPostIndex -> { + if (focusedPostIndex == null) return; + recyclerView.scrollToPosition(focusedPostIndex); + }); + viewModel.getTopicTaskResultCode().observe(this, resultCode -> { + if (resultCode == null) return; + switch (resultCode) { + case SUCCESS: + paginationEnabled(true); + progressBar.setVisibility(ProgressBar.GONE); + break; + case NETWORK_ERROR: + Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); + break; + case UNAUTHORIZED: + progressBar.setVisibility(ProgressBar.GONE); + 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!!! + Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show(); + finish(); + break; } - }); viewModel.getPrepareForReplyResult().observe(this, prepareForReplyResult -> { if (prepareForReplyResult != null) { @@ -277,7 +281,7 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb bottomNavBar.setVisibility(View.GONE); } }); - viewModel.initialLoad(topicPageUrl); + viewModel.loadUrl(topicPageUrl); } @Override @@ -307,18 +311,14 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb TextView usersViewing = infoDialog.findViewById(R.id.users_viewing); usersViewing.setText(new SpannableStringBuilder("Loading...")); usersViewing.setMovementMethod(LinkMovementMethod.getInstance()); - viewModel.getTopicTaskResult().observe(this, topicTaskResult -> { - if (topicTaskResult == null) { - usersViewing.setText(new SpannableStringBuilder("Loading...")); - treeAndMods.setText(new SpannableStringBuilder("Loading...")); - } else { - String treeAndModsString = topicTaskResult.getTopicTreeAndMods(); - treeAndMods.setText(HTMLUtils.getSpannableFromHtml(this, treeAndModsString)); - String topicViewersString = topicTaskResult.getTopicViewers(); - usersViewing.setText(HTMLUtils.getSpannableFromHtml(this, topicViewersString)); - } + viewModel.getTopicTreeAndMods().observe(this, topicTreeAndMods -> { + if (topicTreeAndMods == null) return; + treeAndMods.setText(HTMLUtils.getSpannableFromHtml(this, topicTreeAndMods)); + }); + viewModel.getTopicViewers().observe(this, topicViewers -> { + if (topicViewers == null) return; + usersViewing.setText(HTMLUtils.getSpannableFromHtml(this, topicViewers)); }); - builder.setView(infoDialog); AlertDialog dialog = builder.create(); dialog.show(); @@ -559,7 +559,7 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb if (success) { if ((postsList.get(postsList.size() - 1).getPostNumber() + 1) % 15 == 0) { - viewModel.loadUrl(viewModel.getBaseUrl() + "." + 2147483647); + viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + "." + 2147483647); } else { viewModel.reloadPage(); } 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 1bc85acc..783c741f 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 @@ -46,6 +46,7 @@ import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyFile; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.utils.CircleTransform; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.viewmodel.TopicViewModel; import timber.log.Timber; @@ -628,8 +629,7 @@ class TopicAdapter extends RecyclerView.Adapter { if (target.is(ThmmyPage.PageCategory.TOPIC)) { //This url points to a topic //Checks if the page to be loaded is the one already shown - if (uriString.contains(viewModel.getBaseUrl())) { - Timber.e("reached here!"); + if (uriString.contains(ParseHelpers.getBaseURL(viewModel.getTopicUrl()))) { if (uriString.contains("topicseen#new") || uriString.contains("#new")) { if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { //same page @@ -643,7 +643,6 @@ class TopicAdapter extends RecyclerView.Adapter { 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 @@ -652,11 +651,11 @@ class TopicAdapter extends RecyclerView.Adapter { return true; } } - } else if ((Objects.equals(uriString, viewModel.getBaseUrl()) && viewModel.getCurrentPageIndex() == 1) || - Integer.parseInt(uriString.substring(viewModel.getBaseUrl().length() + 1)) / 15 + 1 == + } else if ((Objects.equals(uriString, ParseHelpers.getBaseURL(viewModel.getTopicUrl())) && + viewModel.getCurrentPageIndex() == 1) || + Integer.parseInt(uriString.substring(ParseHelpers.getBaseURL(viewModel.getTopicUrl()).length() + 1)) / 15 + 1 == viewModel.getCurrentPageIndex()) { //same page - Timber.e("ha"); return true; } } 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 e233ae1c..28446874 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 @@ -43,18 +43,6 @@ public class TopicTask extends AsyncTask { @Override protected TopicTaskResult doInBackground(String... strings) { - String topicTitle = null; - String topicTreeAndMods = ""; - String topicViewers = ""; - ArrayList newPostsList = null; - int loadedPageTopicId = -1; - int focusedPostIndex = 0; - SparseArray pagesUrls = new SparseArray<>(); - int currentPageIndex = 1; - int pageCount = 1; - String baseUrl = ""; - String lastPageLoadAttemptedUrl = ""; - Document topic = null; String newPageUrl = strings[0]; @@ -70,14 +58,9 @@ public class TopicTask extends AsyncTask { } } - lastPageLoadAttemptedUrl = newPageUrl; - if (strings[0].substring(0, strings[0].lastIndexOf(".")).contains("topic=")) - baseUrl = strings[0].substring(0, strings[0].lastIndexOf(".")); //New topic's base url - String replyPageUrl = null; Request request = new Request.Builder() .url(newPageUrl) .build(); - ResultCode resultCode; try { Response response = BaseApplication.getInstance().getClient().newCall(request).execute(); topic = Jsoup.parse(response.body().string()); @@ -85,17 +68,18 @@ public class TopicTask extends AsyncTask { 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); + String topicTreeAndMods = topic.select("div.nav").first().html(); + String topicViewers = TopicParser.parseUsersViewingThisTopic(topic, language); //Finds reply page url + String replyPageUrl = null; 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(); + String topicTitle = topic.select("td[id=top_subject]").first().text(); if (topicTitle.contains("Topic:")) { topicTitle = topicTitle.substring(topicTitle.indexOf("Topic:") + 7 , topicTitle.indexOf("(Read") - 2); @@ -106,42 +90,39 @@ public class TopicTask extends AsyncTask { } //Finds current page's index - currentPageIndex = TopicParser.parseCurrentPageIndex(topic, language); + int 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)); - } + int pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language); - newPostsList = TopicParser.parseTopic(topic, language); + ArrayList newPostsList = TopicParser.parseTopic(topic, language); - loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(lastPageLoadAttemptedUrl)); + int loadedPageTopicId = Integer.parseInt(ThmmyPage.getTopicId(newPageUrl)); //Finds the position of the focused message if present + int focusedPostIndex = 0; for (int i = 0; i < newPostsList.size(); ++i) { if (newPostsList.get(i).getPostIndex() == postFocus) { focusedPostIndex = i; break; } } - resultCode = ResultCode.SUCCESS; + return new TopicTaskResult(ResultCode.SUCCESS, topicTitle, replyPageUrl, newPostsList, loadedPageTopicId, + currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods, topicViewers); } catch (IOException e) { Timber.i(e, "IO Exception"); - resultCode = ResultCode.NETWORK_ERROR; + return new TopicTaskResult(ResultCode.NETWORK_ERROR, null, null, null, + 0, 0, 0, 0, null, null); } catch (Exception e) { if (isUnauthorized(topic)) { - resultCode = ResultCode.UNAUTHORIZED; + return new TopicTaskResult(ResultCode.UNAUTHORIZED, null, null, null, + 0, 0, 0, 0, null, null); } else { Timber.e(e, "Parsing Error"); - resultCode = ResultCode.PARSING_ERROR; + return new TopicTaskResult(ResultCode.PARSING_ERROR, null, null, null, + 0, 0, 0, 0, null, null); } } - return new TopicTaskResult(resultCode, baseUrl, topicTitle, replyPageUrl, newPostsList, - loadedPageTopicId, currentPageIndex, pageCount, focusedPostIndex, topicTreeAndMods, - topicViewers, lastPageLoadAttemptedUrl, pagesUrls); } private boolean isUnauthorized(Document document) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java index f9b76736..570160d3 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTaskResult.java @@ -9,13 +9,6 @@ import gr.thmmy.mthmmy.model.Post; public class TopicTaskResult { private final TopicTask.ResultCode resultCode; - /** - * Holds this topic's base url. For example a topic with url similar to - * "https://www.thmmy.gr/smf/index.php?topic=1.15;topicseen" or - * "https://www.thmmy.gr/smf/index.php?topic=1.msg1#msg1" - * has the base url "https://www.thmmy.gr/smf/index.php?topic=1" - */ - private final String baseUrl; /** * Holds this topic's title. At first this gets the value of the topic title that came with * bundle and is rendered in the toolbar while parsing this topic. Later, if a different topic @@ -46,18 +39,12 @@ public class TopicTaskResult { //Topic's info related private final String topicTreeAndMods; private final String topicViewers; - /** - * The url of the last page that was attempted to be loaded - */ - private final String lastPageLoadAttemptedUrl; - private final SparseArray pagesUrls; - public TopicTaskResult(TopicTask.ResultCode resultCode, String baseUrl, String topicTitle, + public TopicTaskResult(TopicTask.ResultCode resultCode, String topicTitle, String replyPageUrl, ArrayList newPostsList, int loadedPageTopicId, int currentPageIndex, int pageCount, int focusedPostIndex, String topicTreeAndMods, - String topicViewers, String lastPageLoadAttemptedUrl, SparseArray pagesUrls) { + String topicViewers) { this.resultCode = resultCode; - this.baseUrl = baseUrl; this.topicTitle = topicTitle; this.replyPageUrl = replyPageUrl; this.newPostsList = newPostsList; @@ -67,18 +54,12 @@ public class TopicTaskResult { this.focusedPostIndex = focusedPostIndex; this.topicTreeAndMods = topicTreeAndMods; this.topicViewers = topicViewers; - this.lastPageLoadAttemptedUrl = lastPageLoadAttemptedUrl; - this.pagesUrls = pagesUrls; } public TopicTask.ResultCode getResultCode() { return resultCode; } - public String getBaseUrl() { - return baseUrl; - } - public String getTopicTitle() { return topicTitle; } @@ -114,12 +95,4 @@ public class TopicTaskResult { public String getTopicViewers() { return topicViewers; } - - public String getLastPageLoadAttemptedUrl() { - return lastPageLoadAttemptedUrl; - } - - public SparseArray getPagesUrls() { - return pagesUrls; - } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java index 307efab0..0034ffc4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java @@ -6,6 +6,8 @@ import org.jsoup.select.Elements; import java.util.ArrayList; +import timber.log.Timber; + /** * This class consists exclusively of static classes (enums) and methods (excluding methods of inner * classes). It can be used to resolve a page's language and state or fix embedded videos html code. @@ -166,4 +168,22 @@ public class ParseHelpers { } return fixed; } + + /** + * Method that extracts the base URL from a topic's page URL. For example a topic with url similar to + * "https://www.thmmy.gr/smf/index.php?topic=1.15;topicseen" or + * "https://www.thmmy.gr/smf/index.php?topic=1.msg1#msg1" + * has the base url "https://www.thmmy.gr/smf/index.php?topic=1" + * + * @param topicURL a topic's page URL + * @return the base URL of the given topic + */ + public static String getBaseURL(String topicURL) { + if (topicURL.substring(0, topicURL.lastIndexOf(".")).contains("topic=")) + return topicURL.substring(0, topicURL.lastIndexOf(".")); + else { + Timber.wtf(new ParseException("Could not parse base URL of topic")); + return ""; + } + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index 6416a85d..b74a63a2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -21,6 +21,7 @@ import gr.thmmy.mthmmy.activities.topic.tasks.TopicTaskResult; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.session.SessionManager; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTaskCompleted, PrepareForReply.OnPrepareForReplyFinished, PrepareForEditTask.OnPrepareEditFinished { @@ -59,47 +60,53 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa * navigation bar occurs, aka the value that the page indicator shows */ private MutableLiveData pageIndicatorIndex = new MutableLiveData<>(); - private MutableLiveData topicTaskResult = new MutableLiveData<>(); - private MutableLiveData prepareForReplyResult = new MutableLiveData<>(); - private MutableLiveData prepareForEditResult = new MutableLiveData<>(); - private String firstTopicUrl; + private MutableLiveData replyPageUrl = new MutableLiveData<>(); + private MutableLiveData pageTopicId = new MutableLiveData<>(); + private MutableLiveData topicTitle = new MutableLiveData<>(); + private MutableLiveData> postsList = new MutableLiveData<>(); + private MutableLiveData focusedPostIndex = new MutableLiveData<>(); + private MutableLiveData topicTaskResultCode = new MutableLiveData<>(); + private MutableLiveData topicTreeAndMods = new MutableLiveData<>(); + private MutableLiveData topicViewers = new MutableLiveData<>(); + private String topicUrl; + private int currentPageIndex; + private int pageCount; - public void initialLoad(String pageUrl) { - firstTopicUrl = pageUrl; - currentTopicTask = new TopicTask(topicTaskObserver, this); - currentTopicTask.execute(pageUrl); - } + private MutableLiveData prepareForReplyResult = new MutableLiveData<>(); + private MutableLiveData prepareForEditResult = new MutableLiveData<>(); public void loadUrl(String pageUrl) { stopLoading(); + topicUrl = pageUrl; currentTopicTask = new TopicTask(topicTaskObserver, this); currentTopicTask.execute(pageUrl); } public void reloadPage() { - if (topicTaskResult.getValue() == null) - throw new NullPointerException("No topic task has finished yet!"); - loadUrl(topicTaskResult.getValue().getLastPageLoadAttemptedUrl()); + if (topicUrl == null) throw new NullPointerException("No topic task has finished yet!"); + loadUrl(topicUrl); } public void performPageChange() { - if (topicTaskResult.getValue() == null || pageIndicatorIndex.getValue() == null) + if (pageIndicatorIndex.getValue() == null) throw new NullPointerException("No page has been loaded yet!"); int pageRequested = pageIndicatorIndex.getValue() - 1; - if (pageRequested != topicTaskResult.getValue().getCurrentPageIndex() - 1) { - loadUrl(topicTaskResult.getValue().getPagesUrls().get(pageRequested)); + if (pageRequested != currentPageIndex - 1) { + loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + String.valueOf(pageRequested * 15)); pageIndicatorIndex.setValue(pageRequested + 1); + } else { + stopLoading(); } } public void prepareForReply() { - if (topicTaskResult.getValue() == null) + if (replyPageUrl.getValue() == null) throw new NullPointerException("Topic task has not finished yet!"); stopLoading(); setPageIndicatorIndex(getPageCount(), true); currentPrepareForReplyTask = new PrepareForReply(prepareForReplyCallbacks, this, - topicTaskResult.getValue().getReplyPageUrl()); + replyPageUrl.getValue()); currentPrepareForReplyTask.execute(toQuoteList.toArray(new Integer[0])); } @@ -124,11 +131,11 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa } public void prepareForEdit(int position, String postEditURL) { - if (topicTaskResult.getValue() == null) + if (replyPageUrl.getValue() == null) throw new NullPointerException("Topic task has not finished yet!"); stopLoading(); currentPrepareForEditTask = new PrepareForEditTask(prepareForEditCallbacks, this, position, - topicTaskResult.getValue().getReplyPageUrl()); + replyPageUrl.getValue()); currentPrepareForEditTask.execute(postEditURL); } @@ -166,14 +173,23 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa // callbacks for viewmodel @Override public void onTopicTaskCompleted(TopicTaskResult result) { - topicTaskResult.setValue(result); if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) { + currentPageIndex = result.getCurrentPageIndex(); + pageCount = result.getPageCount(); + topicTreeAndMods.setValue(result.getTopicTreeAndMods()); + topicViewers.setValue(result.getTopicViewers()); + pageTopicId.setValue(result.getLoadedPageTopicId()); + replyPageUrl.setValue(result.getReplyPageUrl()); + topicTitle.setValue(result.getTopicTitle()); pageIndicatorIndex.setValue(result.getCurrentPageIndex()); + postsList.setValue(result.getNewPostsList()); + focusedPostIndex.setValue(result.getFocusedPostIndex()); isUserExtraInfoVisibile.clear(); for (int i = 0; i < result.getNewPostsList().size(); i++) { isUserExtraInfoVisibile.add(false); } } + topicTaskResultCode.setValue(result.getResultCode()); } @Override @@ -216,6 +232,41 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa // <-------------Just getters, setters and helper methods below here----------------> + public MutableLiveData getTopicViewers() { + return topicViewers; + } + + public MutableLiveData getTopicTreeAndMods() { + return topicTreeAndMods; + } + + public MutableLiveData getTopicTaskResultCode() { + return topicTaskResultCode; + } + + public MutableLiveData getFocusedPostIndex() { + return focusedPostIndex; + } + + public MutableLiveData> getPostsList() { + return postsList; + } + + public MutableLiveData getReplyPageUrl() { + return replyPageUrl; + } + + public MutableLiveData getPageTopicId() { + return pageTopicId; + } + + public MutableLiveData getTopicTitle() { + return topicTitle; + } + + public String getTopicUrl() { + return topicUrl; + } public MutableLiveData getPageIndicatorIndex() { return pageIndicatorIndex; @@ -268,10 +319,6 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa this.prepareForReplyCallbacks = prepareForReplyCallbacks; } - public MutableLiveData getTopicTaskResult() { - return topicTaskResult; - } - public MutableLiveData getPrepareForReplyResult() { return prepareForReplyResult; } @@ -293,7 +340,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa } public boolean canReply() { - return topicTaskResult.getValue() != null && topicTaskResult.getValue().getReplyPageUrl() != null; + return replyPageUrl.getValue() != null; } public boolean isWritingReply() { @@ -304,40 +351,14 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa this.writingReply = writingReply; } - public String getBaseUrl() { - if (topicTaskResult.getValue() != null) { - return topicTaskResult.getValue().getBaseUrl(); - } else { - return ""; - } - } - - public String getTopicUrl() { - if (topicTaskResult.getValue() != null) { - return topicTaskResult.getValue().getLastPageLoadAttemptedUrl(); - } else { - // topic task has not finished yet (log? disable menu button until load is finished?) - return firstTopicUrl; - } - } - - public String getTopicTitle() { - if (topicTaskResult.getValue() == null) - throw new NullPointerException("Topic task has not finished yet!"); - return topicTaskResult.getValue().getTopicTitle(); - } - public int getCurrentPageIndex() { - if (topicTaskResult.getValue() == null) - throw new NullPointerException("No page has been loaded yet!"); - return topicTaskResult.getValue().getCurrentPageIndex(); + if (currentPageIndex == 0) throw new NullPointerException("No page has been loaded yet!"); + return currentPageIndex; } public int getPageCount() { - if (topicTaskResult.getValue() == null) - throw new NullPointerException("No page has been loaded yet!"); - - return topicTaskResult.getValue().getPageCount(); + if (pageCount == 0) throw new NullPointerException("No page has been loaded yet!"); + return pageCount; } public String getPostBeingEditedText() {