From aeb7a370744fbf60c3233bc8fb578a487af6fd69 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Mon, 6 Aug 2018 16:42:18 +0300 Subject: [PATCH] cleanup --- .../activities/topic/TopicActivity.java | 26 +++++++--------- .../mthmmy/viewmodel/TopicViewModel.java | 30 +++++++++++++------ 2 files changed, 31 insertions(+), 25 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 4af5a99f..2ed8e25b 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 @@ -397,10 +397,10 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb public void run() { long REPEAT_DELAY = 250; if (autoIncrement) { - viewModel.incrementPageRequestValue(step); + viewModel.incrementPageRequestValue(step, false); repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); } else if (autoDecrement) { - viewModel.decrementPageRequestValue(step); + viewModel.decrementPageRequestValue(step, false); 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.setOnClickListener(v -> { if (!autoIncrement && step == LARGE_STEP) { - viewModel.incrementPageRequestValue(viewModel.getPageCount()); - viewModel.changePage(viewModel.getPageCount() - 1); + viewModel.setPageIndicatorIndex(viewModel.getPageCount(), true); } else if (!autoIncrement) { - viewModel.incrementPageRequestValue(step); - viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); + viewModel.incrementPageRequestValue(step, true); } }); @@ -468,12 +466,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb } else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { autoIncrement = false; paginationEnabled(true); - viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); + viewModel.performPageChange(); } else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) { if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { autoIncrement = false; - viewModel.decrementPageRequestValue(viewModel.getPageIndicatorIndex().getValue() - - viewModel.getCurrentPageIndex()); + viewModel.setPageIndicatorIndex(viewModel.getCurrentPageIndex(), false); paginationEnabled(true); } } @@ -487,11 +484,9 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb // Decrement once for a click decrement.setOnClickListener(v -> { if (!autoDecrement && step == LARGE_STEP) { - viewModel.decrementPageRequestValue(viewModel.getPageCount()); - viewModel.changePage(0); + viewModel.setPageIndicatorIndex(1, true); } else if (!autoDecrement) { - viewModel.decrementPageRequestValue(step); - viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); + viewModel.decrementPageRequestValue(step, true); } }); @@ -515,13 +510,12 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { autoDecrement = false; paginationEnabled(true); - viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); + viewModel.performPageChange(); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (rect != null && !rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { autoIncrement = false; - viewModel.incrementPageRequestValue(viewModel.getCurrentPageIndex() - - viewModel.getPageIndicatorIndex().getValue()); + viewModel.setPageIndicatorIndex(viewModel.getCurrentPageIndex(), false); paginationEnabled(true); } } 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 3f5ad4cf..6416a85d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/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()); } - public void changePage(int pageRequested) { - if (topicTaskResult.getValue() == null) + public void performPageChange() { + if (topicTaskResult.getValue() == null || 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)); pageIndicatorIndex.setValue(pageRequested + 1); @@ -96,7 +97,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa if (topicTaskResult.getValue() == null) throw new NullPointerException("Topic task has not finished yet!"); stopLoading(); - changePage(topicTaskResult.getValue().getPageCount() - 1); + setPageIndicatorIndex(getPageCount(), true); currentPrepareForReplyTask = new PrepareForReply(prepareForReplyCallbacks, this, topicTaskResult.getValue().getReplyPageUrl()); currentPrepareForReplyTask.execute(toQuoteList.toArray(new Integer[0])); @@ -188,18 +189,29 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa prepareForEditResult.setValue(result); } - public void incrementPageRequestValue(int step) { - if (pageIndicatorIndex.getValue() < getPageCount() - step) + public void incrementPageRequestValue(int step, boolean changePage) { + if (pageIndicatorIndex.getValue() == null) + throw new NullPointerException("No page has been loaded yet!"); + if (pageIndicatorIndex.getValue() <= getPageCount() - step) { pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() + step); - else + } else pageIndicatorIndex.setValue(getPageCount()); + if (changePage) performPageChange(); } - public void decrementPageRequestValue(int step) { - if (pageIndicatorIndex.getValue() > step) + public void decrementPageRequestValue(int step, boolean changePage) { + if (pageIndicatorIndex.getValue() == null) + throw new NullPointerException("No page has been loaded yet!"); + if (pageIndicatorIndex.getValue() >= step) { pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step); - else + } else 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---------------->