Skip to content

Commit 5181cf3

Browse files
author
Mahavir Jain
authored
Merge pull request #90 from codetoart/dev
ReadPosition interface and implementation code
2 parents 40a191d + 3f698cc commit 5181cf3

File tree

13 files changed

+352
-223
lines changed

13 files changed

+352
-223
lines changed

folioreader/src/main/assets/js/Bridge.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -575,42 +575,56 @@ function isElementVisible(element, isHorizontal) {
575575
}
576576

577577
/**
578-
Gets the first visible span index from the displayed chapter and passes the iOS compatible last read
579-
span json string in FolioPageFragment#storeFirstVisibleSpanIndex(String) JavascriptInterface
578+
Gets the first visible span from the displayed chapter and if it has id then usingId is true with
579+
value as span id else usingId is false with value as span index. usingId and value is forwarded to
580+
FolioPageFragment#storeFirstVisibleSpan(boolean, String) JavascriptInterface.
580581
581-
@param {boolean} isHorizontal Scrolling type of DirectionalViewpager#mDirection
582+
@param {boolean} isHorizontal - scrolling type of DirectionalViewpager#mDirection
582583
*/
583-
function getFirstVisibleSpanIndex(isHorizontal) {
584+
function getFirstVisibleSpan(isHorizontal) {
584585

585-
//Can be more specific with document.querySelectorAll('span.sentence')
586586
var spanCollection = document.getElementsByTagName("span");
587-
var json = {usingId: false, value: 0};
588587

589-
for (var i = 0 ; i < spanCollection.length ; i++) {
588+
if (spanCollection.length == 0) {
589+
FolioPageFragment.storeFirstVisibleSpan(false, 0);
590+
return;
591+
}
590592

591-
if (isElementVisible(spanCollection[i], isHorizontal)) {
593+
var spanIndex = 0;
594+
var spanElement;
592595

593-
json.value = i;
596+
for (var i = 0 ; i < spanCollection.length ; i++) {
597+
if (isElementVisible(spanCollection[i], isHorizontal)) {
598+
spanIndex = i;
599+
spanElement = spanCollection[i];
594600
break;
595601
}
596602
}
597603

598-
FolioPageFragment.storeFirstVisibleSpanIndex(JSON.stringify(json));
604+
var usingId = spanElement.id ? true : false;
605+
var value = usingId ? spanElement.id : spanIndex;
606+
FolioPageFragment.storeFirstVisibleSpan(usingId, value);
599607
}
600608

601609
/**
602-
Scrolls the webpage to particular span index
610+
Scrolls the web page to particular span using id or index
603611
604-
@param {number} index
612+
@param {boolean} usingId - if span tag has id then true or else false
613+
@param {number} value - if usingId true then span id else span index
605614
*/
606-
function scrollToSpanIndex(index) {
615+
function scrollToSpan(usingId, value) {
607616

608-
var spanCollection = document.getElementsByTagName("span");
609-
610-
if (spanCollection.length == 0 || index < 0 || index >= spanCollection.length)
611-
return;
612-
613-
goToEl(spanCollection[index]);
617+
if (usingId) {
618+
var spanElement = document.getElementById(value);
619+
if (spanElement)
620+
goToEl(spanElement);
621+
} else {
622+
var spanCollection = document.getElementsByTagName("span");
623+
if (spanCollection.length == 0 || value < 0 || value >= spanCollection.length
624+
|| value == null)
625+
return;
626+
goToEl(spanCollection[value]);
627+
}
614628
}
615629

616630
// Class based onClick listener

folioreader/src/main/java/com/folioreader/Constants.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ public class Constants {
1111
public static final String CHAPTER_SELECTED = "chapter_selected";
1212
public static final String HIGHLIGHT_SELECTED = "highlight_selected";
1313
public static final String BOOK_TITLE = "book_title";
14-
public static final String BOOK_ID = "book_id";
15-
public static final String LAST_READ_SPAN_INDEX = "last_read_span_index";
16-
public static final String LAST_READ_CHAPTER_INDEX = "last_read_chapter_index";
17-
public static final String LAST_READ_STATE = "_last_read_state";
1814
public static final int PORT_NUMBER = 8080;
1915
public static final String LOCALHOST = "http://127.0.0.1:" + PORT_NUMBER + "/";
2016
public static final String SELECTED_WORD = "selected_word";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.folioreader.model;
2+
3+
/**
4+
* Created by Hrishikesh Kadam on 20/04/2018.
5+
*/
6+
public interface ReadPosition {
7+
8+
String getBookId();
9+
10+
int getChapterIndex();
11+
12+
String getChapterHref();
13+
14+
boolean isUsingId();
15+
16+
String getValue();
17+
18+
String toJson();
19+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.folioreader.model;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
import android.util.Log;
6+
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import com.fasterxml.jackson.databind.ObjectWriter;
9+
import com.folioreader.util.ObjectMapperSingleton;
10+
11+
/**
12+
* Created by Hrishikesh Kadam on 20/04/2018.
13+
*/
14+
public class ReadPositionImpl implements ReadPosition, Parcelable {
15+
16+
public static final Creator<ReadPositionImpl> CREATOR = new Creator<ReadPositionImpl>() {
17+
@Override
18+
public ReadPositionImpl createFromParcel(Parcel in) {
19+
return new ReadPositionImpl(in);
20+
}
21+
22+
@Override
23+
public ReadPositionImpl[] newArray(int size) {
24+
return new ReadPositionImpl[size];
25+
}
26+
};
27+
28+
private static final String LOG_TAG = ReadPositionImpl.class.getSimpleName();
29+
private String bookId;
30+
private int chapterIndex = -1;
31+
private String chapterHref;
32+
private boolean usingId;
33+
private String value;
34+
35+
public ReadPositionImpl() {
36+
}
37+
38+
public ReadPositionImpl(String bookId, int chapterIndex, String chapterHref, boolean usingId, String value) {
39+
this.bookId = bookId;
40+
this.chapterIndex = chapterIndex;
41+
this.chapterHref = chapterHref;
42+
this.usingId = usingId;
43+
this.value = value;
44+
}
45+
46+
protected ReadPositionImpl(Parcel in) {
47+
bookId = in.readString();
48+
chapterIndex = in.readInt();
49+
chapterHref = in.readString();
50+
usingId = in.readByte() != 0;
51+
value = in.readString();
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "ReadPositionImpl{" +
57+
"bookId='" + bookId + '\'' +
58+
", chapterIndex=" + chapterIndex +
59+
", chapterHref='" + chapterHref + '\'' +
60+
", usingId=" + usingId +
61+
", value='" + value + '\'' +
62+
'}';
63+
}
64+
65+
@Override
66+
public String getBookId() {
67+
return bookId;
68+
}
69+
70+
public void setBookId(String bookId) {
71+
this.bookId = bookId;
72+
}
73+
74+
@Override
75+
public int getChapterIndex() {
76+
return chapterIndex;
77+
}
78+
79+
public void setChapterIndex(int chapterIndex) {
80+
this.chapterIndex = chapterIndex;
81+
}
82+
83+
@Override
84+
public String getChapterHref() {
85+
return chapterHref;
86+
}
87+
88+
public void setChapterHref(String chapterHref) {
89+
this.chapterHref = chapterHref;
90+
}
91+
92+
@Override
93+
public boolean isUsingId() {
94+
return usingId;
95+
}
96+
97+
public void setUsingId(boolean usingId) {
98+
this.usingId = usingId;
99+
}
100+
101+
@Override
102+
public String getValue() {
103+
return value;
104+
}
105+
106+
public void setValue(String value) {
107+
this.value = value;
108+
}
109+
110+
@Override
111+
public String toJson() {
112+
try {
113+
ObjectWriter objectWriter = ObjectMapperSingleton.getObjectMapper().writer();
114+
return objectWriter.writeValueAsString(this);
115+
} catch (JsonProcessingException e) {
116+
Log.e(LOG_TAG, "-> " + e);
117+
return null;
118+
}
119+
}
120+
121+
@Override
122+
public int describeContents() {
123+
return 0;
124+
}
125+
126+
@Override
127+
public void writeToParcel(Parcel dest, int flags) {
128+
129+
dest.writeString(bookId);
130+
dest.writeInt(chapterIndex);
131+
dest.writeString(chapterHref);
132+
dest.writeByte((byte) (usingId ? 1 : 0));
133+
dest.writeString(value);
134+
}
135+
}

folioreader/src/main/java/com/folioreader/ui/folio/activity/FolioActivity.java

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import android.support.annotation.NonNull;
2626
import android.support.v4.app.ActivityCompat;
2727
import android.support.v4.content.ContextCompat;
28-
import android.support.v4.content.LocalBroadcastManager;
2928
import android.support.v7.app.AppCompatActivity;
3029
import android.support.v7.widget.Toolbar;
3130
import android.text.Html;
32-
import android.text.TextUtils;
3331
import android.util.Log;
3432
import android.view.View;
3533
import android.view.animation.AccelerateInterpolator;
@@ -46,6 +44,7 @@
4644
import com.folioreader.Constants;
4745
import com.folioreader.R;
4846
import com.folioreader.model.HighlightImpl;
47+
import com.folioreader.model.ReadPosition;
4948
import com.folioreader.model.event.AnchorIdEvent;
5049
import com.folioreader.model.event.MediaOverlayHighlightStyleEvent;
5150
import com.folioreader.model.event.MediaOverlayPlayPauseEvent;
@@ -93,8 +92,7 @@ public class FolioActivity
9392
public static final String INTENT_EPUB_SOURCE_PATH = "com.folioreader.epub_asset_path";
9493
public static final String INTENT_EPUB_SOURCE_TYPE = "epub_source_type";
9594
public static final String INTENT_HIGHLIGHTS_LIST = "highlight_list";
96-
public static final String EXTRA_LAST_READ_CHAPTER_INDEX = "com.folioreader.extra_last_read_chapter_position";
97-
public static final String EXTRA_LAST_READ_SPAN_INDEX = "com.folioreader.extra_last_read_span_index";
95+
public static final String EXTRA_READ_POSITION = "com.folioreader.extra.READ_POSITION";
9896

9997
public enum EpubSourceType {
10098
RAW,
@@ -114,7 +112,7 @@ public enum EpubSourceType {
114112

115113
private int mChapterPosition;
116114
private FolioPageFragmentAdapter mFolioPageFragmentAdapter;
117-
private String lastReadSpanIndex;
115+
private ReadPosition entryReadPosition;
118116
private ConfigBottomSheetDialogFragment mConfigBottomSheetDialogFragment;
119117
private TextView title;
120118

@@ -283,17 +281,28 @@ public void onPageScrollStateChanged(int state) {
283281
if (mSpineReferenceList != null) {
284282
mFolioPageFragmentAdapter = new FolioPageFragmentAdapter(getSupportFragmentManager(), mSpineReferenceList, bookFileName, mBookId);
285283
mFolioPageViewPager.setAdapter(mFolioPageFragmentAdapter);
284+
285+
entryReadPosition = getIntent().getParcelableExtra(FolioActivity.EXTRA_READ_POSITION);
286+
if (entryReadPosition == null ||
287+
(entryReadPosition.getChapterIndex() == -1 &&
288+
entryReadPosition.getChapterHref() == null)) {
289+
mFolioPageViewPager.setCurrentItem(0);
290+
} else if (entryReadPosition.getChapterIndex() != -1) {
291+
mFolioPageViewPager.setCurrentItem(entryReadPosition.getChapterIndex());
292+
} else {
293+
mFolioPageViewPager.setCurrentItem(
294+
getChapterIndex(entryReadPosition.getChapterHref()));
295+
}
286296
}
297+
}
287298

288-
int lastReadChapterIndex =
289-
getIntent().getIntExtra(FolioActivity.EXTRA_LAST_READ_CHAPTER_INDEX, 0);
290-
String lastReadSpanIndex =
291-
getIntent().getStringExtra(FolioActivity.EXTRA_LAST_READ_SPAN_INDEX);
292-
if (TextUtils.isEmpty(lastReadSpanIndex))
293-
lastReadSpanIndex = "{\"usingId\":false,\"value\":0}";
294-
mFolioPageViewPager.setCurrentItem(lastReadChapterIndex);
295-
AppUtil.saveLastReadState(
296-
getApplicationContext(), mBookId, lastReadChapterIndex, lastReadSpanIndex);
299+
private int getChapterIndex(String chapterHref) {
300+
301+
for (int i = 0; i < mSpineReferenceList.size(); i++) {
302+
if (mSpineReferenceList.get(i).getHref().equals(chapterHref))
303+
return i;
304+
}
305+
return 0;
297306
}
298307

299308
private void configDrawerLayoutButtons() {
@@ -334,43 +343,14 @@ public void setPagerToPosition(String href) {
334343
}
335344

336345
@Override
337-
public void setLastReadSpanIndex(String json) {
338-
lastReadSpanIndex = json;
339-
}
340-
341-
/**
342-
* FolioActivity is waiting to get lastReadSpanIndex stored from
343-
* /assets/js/Bridge.js#getFirstVisibleSpanIndex(boolean) ->
344-
* {@link FolioPageFragment#storeFirstVisibleSpanIndex(String)} ->
345-
* {@link #setLastReadSpanIndex(String)} to avoid any race condition.
346-
* This delay on UI Thread goes unnoticed as it is onStop() and not in onPause()
347-
*/
348-
@Override
349-
protected void onStop() {
350-
super.onStop();
351-
352-
try {
353-
Thread.sleep(500);
354-
} catch (InterruptedException e) {
355-
Log.e(TAG, "-> " + e);
356-
}
357-
358-
saveLastReadState();
359-
}
360-
361-
/**
362-
* Sends the broadcast to {@link FolioReader#lastReadStateReceiver}
363-
*/
364-
private void saveLastReadState() {
365-
366-
if (mSpineReferenceList.size() > 0) {
346+
public ReadPosition getEntryReadPosition() {
367347

368-
Intent intent = new Intent(FolioReader.ACTION_SAVE_LAST_READ_STATE);
369-
intent.putExtra(FolioReader.EXTRA_LAST_READ_CHAPTER_INDEX,
370-
mFolioPageViewPager.getCurrentItem());
371-
intent.putExtra(FolioReader.EXTRA_LAST_READ_SPAN_INDEX, lastReadSpanIndex);
372-
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
348+
if (entryReadPosition != null) {
349+
ReadPosition tempReadPosition = entryReadPosition;
350+
entryReadPosition = null;
351+
return tempReadPosition;
373352
}
353+
return null;
374354
}
375355

376356
@Override

0 commit comments

Comments
 (0)