Browse Source

cleanup

pull/43/head
Thodoris1999 6 years ago
parent
commit
aeb7a37074
  1. 26
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 30
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

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

@ -397,10 +397,10 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
public void run() { public void run() {
long REPEAT_DELAY = 250; long REPEAT_DELAY = 250;
if (autoIncrement) { if (autoIncrement) {
viewModel.incrementPageRequestValue(step); viewModel.incrementPageRequestValue(step, false);
repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY);
} else if (autoDecrement) { } else if (autoDecrement) {
viewModel.decrementPageRequestValue(step); viewModel.decrementPageRequestValue(step, false);
repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY);
} }
} }
@ -440,11 +440,9 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
// Increment once for a click // Increment once for a click
increment.setOnClickListener(v -> { increment.setOnClickListener(v -> {
if (!autoIncrement && step == LARGE_STEP) { if (!autoIncrement && step == LARGE_STEP) {
viewModel.incrementPageRequestValue(viewModel.getPageCount()); viewModel.setPageIndicatorIndex(viewModel.getPageCount(), true);
viewModel.changePage(viewModel.getPageCount() - 1);
} else if (!autoIncrement) { } else if (!autoIncrement) {
viewModel.incrementPageRequestValue(step); viewModel.incrementPageRequestValue(step, true);
viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1);
} }
}); });
@ -468,12 +466,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
} else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { } else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {
autoIncrement = false; autoIncrement = false;
paginationEnabled(true); paginationEnabled(true);
viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); viewModel.performPageChange();
} else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) { } else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) {
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
autoIncrement = false; autoIncrement = false;
viewModel.decrementPageRequestValue(viewModel.getPageIndicatorIndex().getValue() viewModel.setPageIndicatorIndex(viewModel.getCurrentPageIndex(), false);
- viewModel.getCurrentPageIndex());
paginationEnabled(true); paginationEnabled(true);
} }
} }
@ -487,11 +484,9 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
// Decrement once for a click // Decrement once for a click
decrement.setOnClickListener(v -> { decrement.setOnClickListener(v -> {
if (!autoDecrement && step == LARGE_STEP) { if (!autoDecrement && step == LARGE_STEP) {
viewModel.decrementPageRequestValue(viewModel.getPageCount()); viewModel.setPageIndicatorIndex(1, true);
viewModel.changePage(0);
} else if (!autoDecrement) { } else if (!autoDecrement) {
viewModel.decrementPageRequestValue(step); viewModel.decrementPageRequestValue(step, true);
viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1);
} }
}); });
@ -515,13 +510,12 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
} else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) {
autoDecrement = false; autoDecrement = false;
paginationEnabled(true); paginationEnabled(true);
viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); viewModel.performPageChange();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) { } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (rect != null && if (rect != null &&
!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { !rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
autoIncrement = false; autoIncrement = false;
viewModel.incrementPageRequestValue(viewModel.getCurrentPageIndex() viewModel.setPageIndicatorIndex(viewModel.getCurrentPageIndex(), false);
- viewModel.getPageIndicatorIndex().getValue());
paginationEnabled(true); paginationEnabled(true);
} }
} }

30
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -83,9 +83,10 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
loadUrl(topicTaskResult.getValue().getLastPageLoadAttemptedUrl()); loadUrl(topicTaskResult.getValue().getLastPageLoadAttemptedUrl());
} }
public void changePage(int pageRequested) { public void performPageChange() {
if (topicTaskResult.getValue() == null) if (topicTaskResult.getValue() == null || pageIndicatorIndex.getValue() == null)
throw new NullPointerException("No page has been loaded yet!"); throw new NullPointerException("No page has been loaded yet!");
int pageRequested = pageIndicatorIndex.getValue() - 1;
if (pageRequested != topicTaskResult.getValue().getCurrentPageIndex() - 1) { if (pageRequested != topicTaskResult.getValue().getCurrentPageIndex() - 1) {
loadUrl(topicTaskResult.getValue().getPagesUrls().get(pageRequested)); loadUrl(topicTaskResult.getValue().getPagesUrls().get(pageRequested));
pageIndicatorIndex.setValue(pageRequested + 1); pageIndicatorIndex.setValue(pageRequested + 1);
@ -96,7 +97,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
if (topicTaskResult.getValue() == null) if (topicTaskResult.getValue() == null)
throw new NullPointerException("Topic task has not finished yet!"); throw new NullPointerException("Topic task has not finished yet!");
stopLoading(); stopLoading();
changePage(topicTaskResult.getValue().getPageCount() - 1); setPageIndicatorIndex(getPageCount(), true);
currentPrepareForReplyTask = new PrepareForReply(prepareForReplyCallbacks, this, currentPrepareForReplyTask = new PrepareForReply(prepareForReplyCallbacks, this,
topicTaskResult.getValue().getReplyPageUrl()); topicTaskResult.getValue().getReplyPageUrl());
currentPrepareForReplyTask.execute(toQuoteList.toArray(new Integer[0])); currentPrepareForReplyTask.execute(toQuoteList.toArray(new Integer[0]));
@ -188,18 +189,29 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
prepareForEditResult.setValue(result); prepareForEditResult.setValue(result);
} }
public void incrementPageRequestValue(int step) { public void incrementPageRequestValue(int step, boolean changePage) {
if (pageIndicatorIndex.getValue() < getPageCount() - step) if (pageIndicatorIndex.getValue() == null)
throw new NullPointerException("No page has been loaded yet!");
if (pageIndicatorIndex.getValue() <= getPageCount() - step) {
pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() + step); pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() + step);
else } else
pageIndicatorIndex.setValue(getPageCount()); pageIndicatorIndex.setValue(getPageCount());
if (changePage) performPageChange();
} }
public void decrementPageRequestValue(int step) { public void decrementPageRequestValue(int step, boolean changePage) {
if (pageIndicatorIndex.getValue() > step) if (pageIndicatorIndex.getValue() == null)
throw new NullPointerException("No page has been loaded yet!");
if (pageIndicatorIndex.getValue() >= step) {
pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step); pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step);
else } else
pageIndicatorIndex.setValue(1); pageIndicatorIndex.setValue(1);
if (changePage) performPageChange();
}
public void setPageIndicatorIndex(int pageIndicatorIndex, boolean changePage) {
this.pageIndicatorIndex.setValue(pageIndicatorIndex);
if (changePage) performPageChange();
} }
// <-------------Just getters, setters and helper methods below here----------------> // <-------------Just getters, setters and helper methods below here---------------->

Loading…
Cancel
Save