Recover page position when configuration changes

This commit is contained in:
inorichi 2015-10-28 21:29:28 +01:00
parent cd456d2186
commit 5e01937cce
7 changed files with 38 additions and 22 deletions

View file

@ -27,7 +27,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private Source source; private Source source;
private Chapter chapter; private Chapter chapter;
private List<Page> pageList; private List<Page> pageList;
@State int savedSelectedPage = -1; @State int savedSelectedPage = 0;
private static final int GET_PAGE_LIST = 1; private static final int GET_PAGE_LIST = 1;
private static final int GET_PAGE_IMAGES = 2; private static final int GET_PAGE_IMAGES = 2;
@ -40,7 +40,12 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
() -> getPageListObservable() () -> getPageListObservable()
.doOnNext(pages -> pageList = pages) .doOnNext(pages -> pageList = pages)
.doOnCompleted(() -> start(GET_PAGE_IMAGES)), .doOnCompleted(() -> start(GET_PAGE_IMAGES)),
(view, pages) -> view.onPageListReady(pages), (view, pages) -> {
view.onPageListReady(pages);
if (savedSelectedPage != 0) {
view.setSelectedPage(savedSelectedPage);
}
},
(view, error) -> Timber.e("An error occurred while downloading page list") (view, error) -> Timber.e("An error occurred while downloading page list")
); );

View file

@ -71,13 +71,14 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
viewer.onPageListReady(pages); viewer.onPageListReady(pages);
} }
public void onPageChanged(int currentPage, int totalPages) { public void onPageChanged(int currentPageIndex, int totalPages) {
String page = currentPage + "/" + totalPages; currentPage = currentPageIndex;
String page = (currentPageIndex + 1) + "/" + totalPages;
pageNumber.setText(page); pageNumber.setText(page);
} }
public void setCurrentPage(int page) { public void setSelectedPage(int pageIndex) {
currentPage = page; viewer.setSelectedPage(pageIndex);
} }
public void hideStatusBar() { public void hideStatusBar() {
@ -98,7 +99,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
private BaseViewer getViewer() { private BaseViewer getViewer() {
switch (prefs.getDefaultViewer()) { switch (prefs.getDefaultViewer()) {
case LEFT_TO_RIGHT: case LEFT_TO_RIGHT: default:
return new LeftToRightViewer(this, container); return new LeftToRightViewer(this, container);
case RIGHT_TO_LEFT: case RIGHT_TO_LEFT:
return new RightToLeftViewer(this, container); return new RightToLeftViewer(this, container);
@ -107,7 +108,6 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
case WEBTOON: case WEBTOON:
return new WebtoonViewer(this, container); return new WebtoonViewer(this, container);
} }
return null;
} }
} }

View file

@ -25,13 +25,8 @@ public class RightToLeftViewer extends ViewPagerViewer {
} }
@Override @Override
public int getCurrentPageFromPos(int position) { public int getCurrentPageIndex(int viewerPosition) {
return getTotalPages() - position; return getTotalPages() - viewerPosition - 1;
}
@Override
public int getPosFromPage(Page page) {
return getTotalPages() - (page.getPageNumber() + 1);
} }
@Override @Override

View file

@ -52,6 +52,12 @@ public class VerticalViewer extends BaseViewer {
return adapter.getCount(); return adapter.getCount();
} }
@Override
public void setSelectedPage(int pageNumber) {
// TODO
return;
}
@Override @Override
public void onPageListReady(List<Page> pages) { public void onPageListReady(List<Page> pages) {
adapter.setPages(pages); adapter.setPages(pages);

View file

@ -33,6 +33,12 @@ public class WebtoonViewer extends BaseViewer {
return adapter.getItemCount(); return adapter.getItemCount();
} }
@Override
public void setSelectedPage(int pageNumber) {
// TODO
return;
}
@Override @Override
public void onPageListReady(List<Page> pages) { public void onPageListReady(List<Page> pages) {
adapter.setPages(pages); adapter.setPages(pages);

View file

@ -20,18 +20,17 @@ public abstract class BaseViewer {
} }
public void updatePageNumber() { public void updatePageNumber() {
activity.onPageChanged(getCurrentPageFromPos(currentPosition), getTotalPages()); activity.onPageChanged(getCurrentPageIndex(currentPosition), getTotalPages());
} }
public int getCurrentPageFromPos(int position) { // Returns the page index given a position in the viewer. Useful por a right to left viewer,
return position + 1; // where the current page is the inverse of the position
} public int getCurrentPageIndex(int viewerPosition) {
return viewerPosition;
public int getPosFromPage(Page page) {
return page.getPageNumber();
} }
public abstract int getTotalPages(); public abstract int getTotalPages();
public abstract void setSelectedPage(int pageNumber);
public abstract void onPageListReady(List<Page> pages); public abstract void onPageListReady(List<Page> pages);
public abstract boolean onImageTouch(MotionEvent motionEvent); public abstract boolean onImageTouch(MotionEvent motionEvent);
} }

View file

@ -65,6 +65,11 @@ public abstract class ViewPagerViewer extends BaseViewer {
return adapter.getCount(); return adapter.getCount();
} }
@Override
public void setSelectedPage(int pageNumber) {
viewPager.setCurrentItem(getCurrentPageIndex(pageNumber));
}
@Override @Override
public void onPageListReady(List<Page> pages) { public void onPageListReady(List<Page> pages) {
adapter.setPages(pages); adapter.setPages(pages);