From ee78d9acfeeae6f7ce6ccc65b468dc674045ab1d Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sun, 5 Aug 2018 19:41:27 +0300 Subject: [PATCH] fix page indicator issue (needs cleanup) --- .../activities/topic/TopicActivity.java | 55 +++++++------------ .../mthmmy/viewmodel/TopicViewModel.java | 34 +++++++++++- 2 files changed, 52 insertions(+), 37 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 e2f92054..4af5a99f 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 @@ -103,11 +103,6 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb * long click is held in either first or last buttons */ private static final int LARGE_STEP = 10; - /** - * Holds the value (index) of the page to be requested when a user interaction with bottom - * navigation bar occurs - */ - private Integer pageRequestValue; //Bottom navigation bar graphics related private LinearLayout bottomNavBar; @@ -206,6 +201,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb paginationEnabled(false); + viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { + if (pageIndicatorIndex == null) return; + pageIndicator.setText(String.valueOf(pageIndicatorIndex) + "/" + + String.valueOf(viewModel.getPageCount())); + }); viewModel.getTopicTaskResult().observe(this, topicTaskResult -> { if (topicTaskResult == null) { progressBar.setVisibility(ProgressBar.VISIBLE); @@ -222,9 +222,6 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb postsList.addAll(topicTaskResult.getNewPostsList()); topicAdapter.notifyDataSetChanged(); - pageIndicator.setText(String.valueOf(topicTaskResult.getCurrentPageIndex()) + "/" + - String.valueOf(topicTaskResult.getPageCount())); - pageRequestValue = topicTaskResult.getCurrentPageIndex(); paginationEnabled(true); if (topicTaskResult.getCurrentPageIndex() == topicTaskResult.getPageCount()) { @@ -400,10 +397,10 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb public void run() { long REPEAT_DELAY = 250; if (autoIncrement) { - incrementPageRequestValue(step); + viewModel.incrementPageRequestValue(step); repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); } else if (autoDecrement) { - decrementPageRequestValue(step); + viewModel.decrementPageRequestValue(step); repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); } } @@ -443,11 +440,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb // Increment once for a click increment.setOnClickListener(v -> { if (!autoIncrement && step == LARGE_STEP) { - incrementPageRequestValue(viewModel.getPageCount()); + viewModel.incrementPageRequestValue(viewModel.getPageCount()); viewModel.changePage(viewModel.getPageCount() - 1); } else if (!autoIncrement) { - incrementPageRequestValue(step); - viewModel.changePage(pageRequestValue - 1); + viewModel.incrementPageRequestValue(step); + viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); } }); @@ -471,11 +468,12 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb } else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { autoIncrement = false; paginationEnabled(true); - viewModel.changePage(pageRequestValue - 1); + viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); } else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) { if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { autoIncrement = false; - decrementPageRequestValue(pageRequestValue - viewModel.getCurrentPageIndex()); + viewModel.decrementPageRequestValue(viewModel.getPageIndicatorIndex().getValue() + - viewModel.getCurrentPageIndex()); paginationEnabled(true); } } @@ -489,11 +487,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb // Decrement once for a click decrement.setOnClickListener(v -> { if (!autoDecrement && step == LARGE_STEP) { - decrementPageRequestValue(viewModel.getPageCount()); + viewModel.decrementPageRequestValue(viewModel.getPageCount()); viewModel.changePage(0); } else if (!autoDecrement) { - decrementPageRequestValue(step); - viewModel.changePage(pageRequestValue - 1); + viewModel.decrementPageRequestValue(step); + viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); } }); @@ -517,12 +515,13 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { autoDecrement = false; paginationEnabled(true); - viewModel.changePage(pageRequestValue - 1); + viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (rect != null && !rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { autoIncrement = false; - incrementPageRequestValue(viewModel.getCurrentPageIndex() - pageRequestValue); + viewModel.incrementPageRequestValue(viewModel.getCurrentPageIndex() + - viewModel.getPageIndicatorIndex().getValue()); paginationEnabled(true); } } @@ -531,22 +530,6 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb }); } - private void incrementPageRequestValue(int step) { - if (pageRequestValue < viewModel.getPageCount() - step) { - pageRequestValue = pageRequestValue + step; - } else - pageRequestValue = viewModel.getPageCount(); - pageIndicator.setText(pageRequestValue + "/" + String.valueOf(viewModel.getPageCount())); - } - - private void decrementPageRequestValue(int step) { - if (pageRequestValue > step) - pageRequestValue = pageRequestValue - step; - else - pageRequestValue = 1; - pageIndicator.setText(pageRequestValue + "/" + String.valueOf(viewModel.getPageCount())); - } - //------------------------------------BOTTOM NAV BAR METHODS END------------------------------------ @Override 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 1fb97407..ae1ed048 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -54,6 +54,11 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa private EditTask.EditTaskCallbacks editTaskCallbacks; private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; + /** + * Holds the value (index) of the page to be requested when a user interaction with bottom + * 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<>(); @@ -81,8 +86,10 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa public void changePage(int pageRequested) { if (topicTaskResult.getValue() == null) throw new NullPointerException("No page has been loaded yet!"); - if (pageRequested != topicTaskResult.getValue().getCurrentPageIndex() - 1) + if (pageRequested != topicTaskResult.getValue().getCurrentPageIndex() - 1) { loadUrl(topicTaskResult.getValue().getPagesUrls().get(pageRequested)); + pageIndicatorIndex.setValue(pageRequested + 1); + } } public void prepareForReply() { @@ -140,6 +147,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa public void stopLoading() { if (currentTopicTask != null && currentTopicTask.getStatus() == AsyncTask.Status.RUNNING) { currentTopicTask.cancel(true); + pageIndicatorIndex.setValue(getCurrentPageIndex()); topicTaskObserver.onTopicTaskCancelled(); } if (currentPrepareForEditTask != null && currentPrepareForEditTask.getStatus() == AsyncTask.Status.RUNNING) { @@ -159,6 +167,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa public void onTopicTaskCompleted(TopicTaskResult result) { topicTaskResult.setValue(result); if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) { + pageIndicatorIndex.setValue(result.getCurrentPageIndex()); isUserExtraInfoVisibile.clear(); for (int i = 0; i < result.getNewPostsList().size(); i++) { isUserExtraInfoVisibile.add(false); @@ -179,8 +188,31 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa prepareForEditResult.setValue(result); } + public void incrementPageRequestValue(int step) { + if (pageIndicatorIndex.getValue() < getPageCount() - step) + pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step); + else + pageIndicatorIndex.setValue(getPageCount()); + } + + public void decrementPageRequestValue(int step) { + if (pageIndicatorIndex.getValue() > step) + pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step); + else + pageIndicatorIndex.setValue(1); + } + // <-------------Just getters, setters and helper methods below here----------------> + + public MutableLiveData getPageIndicatorIndex() { + return pageIndicatorIndex; + } + + public void setPageIndicatorIndex(Integer pageIndicatorIndex) { + this.pageIndicatorIndex.setValue(pageIndicatorIndex); + } + public boolean isUserExtraInfoVisible(int position) { return isUserExtraInfoVisibile.get(position); }