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 Chapter chapter;
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_IMAGES = 2;
@ -40,7 +40,12 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
() -> getPageListObservable()
.doOnNext(pages -> pageList = pages)
.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 file

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

View file

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

View file

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

View file

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

View file

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

View file

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