Skip to content

Commit b0f2ae9

Browse files
committed
Store page number last open and reopen at it
1 parent 1174a4a commit b0f2ae9

File tree

3 files changed

+106
-18
lines changed

3 files changed

+106
-18
lines changed

OCEBook/app/src/main/java/com/devgrapher/ocebook/WebViewFragment.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Bitmap;
66
import android.os.Bundle;
77
import android.app.Fragment;
8+
import android.preference.PreferenceManager;
89
import android.util.Log;
910
import android.view.LayoutInflater;
1011
import android.view.View;
@@ -21,6 +22,7 @@
2122
import com.devgrapher.ocebook.model.Page;
2223
import com.devgrapher.ocebook.model.PaginationInfo;
2324
import com.devgrapher.ocebook.model.ViewerSettings;
25+
import com.devgrapher.ocebook.util.PaginationPrefs;
2426
import com.devgrapher.ocebook.util.WebViewMotionHandler;
2527

2628
import org.readium.sdk.android.Container;
@@ -46,6 +48,7 @@ public class WebViewFragment extends Fragment {
4648

4749
private OnFragmentInteractionListener mListener;
4850
private Container mContainer;
51+
private PaginationPrefs mLastPageinfo;
4952
// protected to be accessed in HiddenRendererFragment
5053
protected ReadiumContext mReadiumCtx;
5154
protected ViewerSettings mViewerSettings;
@@ -99,7 +102,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
99102
return view;
100103
}
101104

102-
103105
private void initWebView() {
104106
if ((getContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)
105107
!= 0) {
@@ -272,35 +274,51 @@ public ReadiumContext.PageEventListener createPageEventListener() {
272274
@Override
273275
public void onReaderInitialized() {
274276
getActivity().runOnUiThread(() -> {
275-
final Package pkcg = mReadiumCtx.getPackage();
276-
pkcg.getSpineItems().stream()
277-
.findAny()
278-
.ifPresent(item -> {
279-
mReadiumCtx.getApi().openBook(pkcg, mViewerSettings,
280-
OpenPageRequest.fromIdref(item.getIdRef()));
281-
282-
mListener.onPackageOpen(mReadiumCtx);
283-
});
277+
final Package pckg = mReadiumCtx.getPackage();
278+
279+
// Get last open page number.
280+
mLastPageinfo = new PaginationPrefs(getContext());
281+
SpineItem spine = pckg.getSpineItems().get(mLastPageinfo.getSpineIndex());
282+
283+
mReadiumCtx.getApi().openBook(pckg, mViewerSettings,
284+
OpenPageRequest.fromIdref(spine.getIdRef()));
285+
286+
mListener.onPackageOpen(mReadiumCtx);
284287
});
285288
}
286289

287290
@Override
288291
public void onPaginationChanged(PaginationInfo currentPagesInfo) {
289292
Log.d(TAG, "onPaginationChanged: " + currentPagesInfo);
293+
290294
List<Page> openPages = currentPagesInfo.getOpenPages();
291295
if (openPages.isEmpty())
292296
return;
293297

294298
getActivity().runOnUiThread(() -> {
299+
final Package pckg = mReadiumCtx.getPackage();
295300
final Page page = openPages.get(0);
296-
SpineItem spineItem = mReadiumCtx.getPackage().getSpineItem(page
297-
.getIdref());
298-
boolean isFixedLayout = spineItem
299-
.isFixedLayout(mReadiumCtx.getPackage());
300-
mWebView.getSettings().setBuiltInZoomControls(
301-
isFixedLayout);
302-
mWebView.getSettings()
303-
.setDisplayZoomControls(false);
301+
302+
SpineItem spineItem = pckg.getSpineItem(page.getIdref());
303+
boolean isFixedLayout = spineItem.isFixedLayout(pckg);
304+
mWebView.getSettings().setBuiltInZoomControls(isFixedLayout);
305+
mWebView.getSettings().setDisplayZoomControls(false);
306+
307+
if (mLastPageinfo != null) {
308+
int newPage = mLastPageinfo.recalculateSpinePage(
309+
page.getSpineItemPageCount());
310+
Log.d("PaginationPref", "" + newPage);
311+
312+
mReadiumCtx.getApi().openSpineItemPage(spineItem.getIdRef(), newPage);
313+
314+
// Set null not to reuse.
315+
mLastPageinfo = null;
316+
} else {
317+
PaginationPrefs.save(getContext(),
318+
page.getSpineItemIndex(),
319+
page.getSpineItemPageIndex(),
320+
page.getSpineItemPageCount());
321+
}
304322

305323
mListener.onPageChanged(page.getSpineItemPageIndex(), page.getSpineItemIndex());
306324
});

OCEBook/app/src/main/java/com/devgrapher/ocebook/util/PageCounts.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.devgrapher.ocebook.util;
22

3+
import com.devgrapher.ocebook.model.PaginationInfo;
4+
35
/**
46
* Created by Brent on 2/19/17.
57
*/
@@ -37,4 +39,5 @@ public int calculateCurrentPage(int spineIndex, int spainePage) {
3739
return 0;
3840
}
3941
}
42+
4043
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.devgrapher.ocebook.util;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.preference.PreferenceManager;
6+
import android.util.Log;
7+
8+
/**
9+
* Created by Brent on 2/20/17.
10+
*/
11+
12+
public class PaginationPrefs {
13+
private static final String TAG = PaginationPrefs.class.toString();
14+
private static final String PREF_SPINE_INDEX = "pref_spine_index";
15+
private static final String PREF_SPINE_PAGE = "pref_spine_page";
16+
private static final String PREF_SPINE_TOTAL_PAGE = "pref_spine_total_page";
17+
private final SharedPreferences mPrefs;
18+
19+
public PaginationPrefs(Context context) {
20+
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
21+
Log.d(TAG, String.format("%d, %d, %d",
22+
getSpineIndex(),
23+
getSpinePage(),
24+
getSpineTotalPage()));
25+
}
26+
27+
public static void save(Context context, int spineIndex, int spinePage, int spineTotalPage) {
28+
SharedPreferences prefs
29+
= PreferenceManager.getDefaultSharedPreferences(context);
30+
31+
prefs.edit().putInt(PREF_SPINE_INDEX, spineIndex)
32+
.putInt(PREF_SPINE_PAGE, spinePage)
33+
.putInt(PREF_SPINE_TOTAL_PAGE, spineTotalPage)
34+
.apply();
35+
}
36+
37+
public int getSpineIndex() {
38+
return mPrefs.getInt(PREF_SPINE_INDEX, 0);
39+
}
40+
41+
public int getSpinePage() {
42+
return mPrefs.getInt(PREF_SPINE_PAGE, 0);
43+
}
44+
45+
public int getSpineTotalPage() {
46+
return mPrefs.getInt(PREF_SPINE_TOTAL_PAGE, 1);
47+
}
48+
49+
/**
50+
* @return new page in the spine based on new total page count.
51+
*/
52+
public int recalculateSpinePage(int newTotalSpinePage) {
53+
float spineTotalPageCount = getSpineTotalPage();
54+
if (spineTotalPageCount == 0) {
55+
// Prevent divide by zero.
56+
spineTotalPageCount = 1;
57+
}
58+
59+
int newPage = (int) (newTotalSpinePage * (getSpinePage() / spineTotalPageCount));
60+
61+
// Prevent out of range.
62+
if (newPage >= newTotalSpinePage) {
63+
newPage = newTotalSpinePage - 1;
64+
}
65+
return newPage;
66+
}
67+
}

0 commit comments

Comments
 (0)