|
|
@ -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<Integer> pageIndicatorIndex = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<TopicTaskResult> topicTaskResult = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<PrepareForReplyResult> prepareForReplyResult = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<PrepareForEditResult> prepareForEditResult = new MutableLiveData<>(); |
|
|
|
|
|
|
|
private String firstTopicUrl; |
|
|
|
private MutableLiveData<String> replyPageUrl = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<Integer> pageTopicId = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<String> topicTitle = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<ArrayList<Post>> postsList = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<Integer> focusedPostIndex = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<TopicTask.ResultCode> topicTaskResultCode = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<String> topicTreeAndMods = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<String> 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> prepareForReplyResult = new MutableLiveData<>(); |
|
|
|
private MutableLiveData<PrepareForEditResult> 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<String> getTopicViewers() { |
|
|
|
return topicViewers; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<String> getTopicTreeAndMods() { |
|
|
|
return topicTreeAndMods; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<TopicTask.ResultCode> getTopicTaskResultCode() { |
|
|
|
return topicTaskResultCode; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<Integer> getFocusedPostIndex() { |
|
|
|
return focusedPostIndex; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<ArrayList<Post>> getPostsList() { |
|
|
|
return postsList; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<String> getReplyPageUrl() { |
|
|
|
return replyPageUrl; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<Integer> getPageTopicId() { |
|
|
|
return pageTopicId; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<String> getTopicTitle() { |
|
|
|
return topicTitle; |
|
|
|
} |
|
|
|
|
|
|
|
public String getTopicUrl() { |
|
|
|
return topicUrl; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<Integer> getPageIndicatorIndex() { |
|
|
|
return pageIndicatorIndex; |
|
|
@ -268,10 +319,6 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa |
|
|
|
this.prepareForReplyCallbacks = prepareForReplyCallbacks; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<TopicTaskResult> getTopicTaskResult() { |
|
|
|
return topicTaskResult; |
|
|
|
} |
|
|
|
|
|
|
|
public MutableLiveData<PrepareForReplyResult> 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() { |
|
|
|