Browse Source

fix page indicator issue (needs cleanup)

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

55
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 * long click is held in either first or last buttons
*/ */
private static final int LARGE_STEP = 10; 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 //Bottom navigation bar graphics related
private LinearLayout bottomNavBar; private LinearLayout bottomNavBar;
@ -206,6 +201,11 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
paginationEnabled(false); 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 -> { viewModel.getTopicTaskResult().observe(this, topicTaskResult -> {
if (topicTaskResult == null) { if (topicTaskResult == null) {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE);
@ -222,9 +222,6 @@ public class TopicActivity extends BaseActivity implements TopicTask.TopicTaskOb
postsList.addAll(topicTaskResult.getNewPostsList()); postsList.addAll(topicTaskResult.getNewPostsList());
topicAdapter.notifyDataSetChanged(); topicAdapter.notifyDataSetChanged();
pageIndicator.setText(String.valueOf(topicTaskResult.getCurrentPageIndex()) + "/" +
String.valueOf(topicTaskResult.getPageCount()));
pageRequestValue = topicTaskResult.getCurrentPageIndex();
paginationEnabled(true); paginationEnabled(true);
if (topicTaskResult.getCurrentPageIndex() == topicTaskResult.getPageCount()) { if (topicTaskResult.getCurrentPageIndex() == topicTaskResult.getPageCount()) {
@ -400,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) {
incrementPageRequestValue(step); viewModel.incrementPageRequestValue(step);
repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY);
} else if (autoDecrement) { } else if (autoDecrement) {
decrementPageRequestValue(step); viewModel.decrementPageRequestValue(step);
repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); 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 once for a click
increment.setOnClickListener(v -> { increment.setOnClickListener(v -> {
if (!autoIncrement && step == LARGE_STEP) { if (!autoIncrement && step == LARGE_STEP) {
incrementPageRequestValue(viewModel.getPageCount()); viewModel.incrementPageRequestValue(viewModel.getPageCount());
viewModel.changePage(viewModel.getPageCount() - 1); viewModel.changePage(viewModel.getPageCount() - 1);
} else if (!autoIncrement) { } else if (!autoIncrement) {
incrementPageRequestValue(step); viewModel.incrementPageRequestValue(step);
viewModel.changePage(pageRequestValue - 1); 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) { } else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {
autoIncrement = false; autoIncrement = false;
paginationEnabled(true); paginationEnabled(true);
viewModel.changePage(pageRequestValue - 1); viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1);
} 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;
decrementPageRequestValue(pageRequestValue - viewModel.getCurrentPageIndex()); viewModel.decrementPageRequestValue(viewModel.getPageIndicatorIndex().getValue()
- viewModel.getCurrentPageIndex());
paginationEnabled(true); paginationEnabled(true);
} }
} }
@ -489,11 +487,11 @@ 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) {
decrementPageRequestValue(viewModel.getPageCount()); viewModel.decrementPageRequestValue(viewModel.getPageCount());
viewModel.changePage(0); viewModel.changePage(0);
} else if (!autoDecrement) { } else if (!autoDecrement) {
decrementPageRequestValue(step); viewModel.decrementPageRequestValue(step);
viewModel.changePage(pageRequestValue - 1); 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) { } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) {
autoDecrement = false; autoDecrement = false;
paginationEnabled(true); paginationEnabled(true);
viewModel.changePage(pageRequestValue - 1); viewModel.changePage(viewModel.getPageIndicatorIndex().getValue() - 1);
} 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;
incrementPageRequestValue(viewModel.getCurrentPageIndex() - pageRequestValue); viewModel.incrementPageRequestValue(viewModel.getCurrentPageIndex()
- viewModel.getPageIndicatorIndex().getValue());
paginationEnabled(true); 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------------------------------------ //------------------------------------BOTTOM NAV BAR METHODS END------------------------------------
@Override @Override

34
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 EditTask.EditTaskCallbacks editTaskCallbacks;
private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; 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<Integer> pageIndicatorIndex = new MutableLiveData<>();
private MutableLiveData<TopicTaskResult> topicTaskResult = new MutableLiveData<>(); private MutableLiveData<TopicTaskResult> topicTaskResult = new MutableLiveData<>();
private MutableLiveData<PrepareForReplyResult> prepareForReplyResult = new MutableLiveData<>(); private MutableLiveData<PrepareForReplyResult> prepareForReplyResult = new MutableLiveData<>();
private MutableLiveData<PrepareForEditResult> prepareForEditResult = new MutableLiveData<>(); private MutableLiveData<PrepareForEditResult> prepareForEditResult = new MutableLiveData<>();
@ -81,8 +86,10 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
public void changePage(int pageRequested) { public void changePage(int pageRequested) {
if (topicTaskResult.getValue() == null) if (topicTaskResult.getValue() == null)
throw new NullPointerException("No page has been loaded yet!"); 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)); loadUrl(topicTaskResult.getValue().getPagesUrls().get(pageRequested));
pageIndicatorIndex.setValue(pageRequested + 1);
}
} }
public void prepareForReply() { public void prepareForReply() {
@ -140,6 +147,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
public void stopLoading() { public void stopLoading() {
if (currentTopicTask != null && currentTopicTask.getStatus() == AsyncTask.Status.RUNNING) { if (currentTopicTask != null && currentTopicTask.getStatus() == AsyncTask.Status.RUNNING) {
currentTopicTask.cancel(true); currentTopicTask.cancel(true);
pageIndicatorIndex.setValue(getCurrentPageIndex());
topicTaskObserver.onTopicTaskCancelled(); topicTaskObserver.onTopicTaskCancelled();
} }
if (currentPrepareForEditTask != null && currentPrepareForEditTask.getStatus() == AsyncTask.Status.RUNNING) { 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) { public void onTopicTaskCompleted(TopicTaskResult result) {
topicTaskResult.setValue(result); topicTaskResult.setValue(result);
if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) { if (result.getResultCode() == TopicTask.ResultCode.SUCCESS) {
pageIndicatorIndex.setValue(result.getCurrentPageIndex());
isUserExtraInfoVisibile.clear(); isUserExtraInfoVisibile.clear();
for (int i = 0; i < result.getNewPostsList().size(); i++) { for (int i = 0; i < result.getNewPostsList().size(); i++) {
isUserExtraInfoVisibile.add(false); isUserExtraInfoVisibile.add(false);
@ -179,8 +188,31 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
prepareForEditResult.setValue(result); 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----------------> // <-------------Just getters, setters and helper methods below here---------------->
public MutableLiveData<Integer> getPageIndicatorIndex() {
return pageIndicatorIndex;
}
public void setPageIndicatorIndex(Integer pageIndicatorIndex) {
this.pageIndicatorIndex.setValue(pageIndicatorIndex);
}
public boolean isUserExtraInfoVisible(int position) { public boolean isUserExtraInfoVisible(int position) {
return isUserExtraInfoVisibile.get(position); return isUserExtraInfoVisibile.get(position);
} }

Loading…
Cancel
Save