Skip to content

Commit

Permalink
Merge pull request #197 from codetoart/master
Browse files Browse the repository at this point in the history
Add originalHref and chapterId to ReadPosition
  • Loading branch information
Mahavir Jain committed May 3, 2018
2 parents bae98d5 + be28435 commit 62a61da
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 51 deletions.
4 changes: 2 additions & 2 deletions folioreader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ext {
siteUrl = 'https://github.com/FolioReader/FolioReader-Android'
gitUrl = 'https://github.com/FolioReader/FolioReader-Android.git'

libraryVersion = '0.3.6'
libraryVersion = '0.3.7'

developerId = 'mobisystech'
developerName = 'Folio Reader'
Expand Down Expand Up @@ -92,7 +92,7 @@ dependencies {

compile 'com.daimajia.swipelayout:library:1.2.0@aar'
// r2-streamer
final R2_STREAMER_VERSION = '0.1.3'
final R2_STREAMER_VERSION = '0.1.4'

compile "org.readium:r2-fetcher:$R2_STREAMER_VERSION"
compile "org.readium:r2-parser:$R2_STREAMER_VERSION"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ public interface ReadPosition {
String getBookId();

/**
* Returns the chapter index from spine tag
* Returns the idref of the spine item.
*/
int getChapterIndex();
String getChapterId();

/**
* Returns the chapter location from the book's root folder
* Returns the href of the manifest item.
*/
String getChapterHref();

/**
* Returns the chapter index from spine tag
*/
int getChapterIndex();

/**
* Returns true if span tag has id
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import android.os.Parcelable;
import android.util.Log;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.folioreader.util.ObjectMapperSingleton;

/**
* Created by Hrishikesh Kadam on 20/04/2018.
*/
@JsonPropertyOrder({"bookId", "chapterId", "chapterHref", "chapterIndex", "usingId", "value"})
public class ReadPositionImpl implements ReadPosition, Parcelable {

public static final Creator<ReadPositionImpl> CREATOR = new Creator<ReadPositionImpl>() {
Expand All @@ -27,40 +29,42 @@ public ReadPositionImpl[] newArray(int size) {

private static final String LOG_TAG = ReadPositionImpl.class.getSimpleName();
private String bookId;
private int chapterIndex = -1;
private String chapterId;
private String chapterHref;
private int chapterIndex = -1;
private boolean usingId;
private String value;

public ReadPositionImpl() {
}

public ReadPositionImpl(String bookId, int chapterIndex, String chapterHref, boolean usingId,
String value) {
public ReadPositionImpl(String bookId, String chapterId, String chapterHref, int chapterIndex,
boolean usingId, String value) {
this.bookId = bookId;
this.chapterIndex = chapterIndex;
this.chapterId = chapterId;
this.chapterHref = chapterHref;
this.chapterIndex = chapterIndex;
this.usingId = usingId;
this.value = value;
}

protected ReadPositionImpl(Parcel in) {
bookId = in.readString();
chapterIndex = in.readInt();
chapterId = in.readString();
chapterHref = in.readString();
chapterIndex = in.readInt();
usingId = in.readByte() != 0;
value = in.readString();
}

@Override
public String toString() {
return "ReadPositionImpl{" +
"bookId='" + bookId + '\'' +
", chapterIndex=" + chapterIndex +
", chapterHref='" + chapterHref + '\'' +
", usingId=" + usingId +
", value='" + value + '\'' +
'}';
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(bookId);
dest.writeString(chapterId);
dest.writeString(chapterHref);
dest.writeInt(chapterIndex);
dest.writeByte((byte) (usingId ? 1 : 0));
dest.writeString(value);
}

@Override
Expand All @@ -72,6 +76,15 @@ public void setBookId(String bookId) {
this.bookId = bookId;
}

@Override
public String getChapterId() {
return chapterId;
}

public void setChapterId(String chapterId) {
this.chapterId = chapterId;
}

@Override
public int getChapterIndex() {
return chapterIndex;
Expand Down Expand Up @@ -123,14 +136,4 @@ public String toJson() {
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

dest.writeString(bookId);
dest.writeInt(chapterIndex);
dest.writeString(chapterHref);
dest.writeByte((byte) (usingId ? 1 : 0));
dest.writeString(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
Expand All @@ -42,6 +43,7 @@

import com.folioreader.Config;
import com.folioreader.Constants;
import com.folioreader.FolioReader;
import com.folioreader.R;
import com.folioreader.model.HighlightImpl;
import com.folioreader.model.ReadPosition;
Expand All @@ -56,7 +58,6 @@
import com.folioreader.ui.folio.presenter.MainPresenter;
import com.folioreader.util.AppUtil;
import com.folioreader.util.FileUtil;
import com.folioreader.FolioReader;
import com.folioreader.util.UiUtil;
import com.folioreader.view.ConfigBottomSheetDialogFragment;
import com.folioreader.view.DirectionalViewpager;
Expand Down Expand Up @@ -283,24 +284,48 @@ public void onPageScrollStateChanged(int state) {
mFolioPageViewPager.setAdapter(mFolioPageFragmentAdapter);

entryReadPosition = getIntent().getParcelableExtra(FolioActivity.EXTRA_READ_POSITION);
if (entryReadPosition == null ||
(entryReadPosition.getChapterIndex() == -1 &&
entryReadPosition.getChapterHref() == null)) {
mFolioPageViewPager.setCurrentItem(0);
} else if (entryReadPosition.getChapterIndex() != -1) {
mFolioPageViewPager.setCurrentItem(entryReadPosition.getChapterIndex());
} else {
mFolioPageViewPager.setCurrentItem(
getChapterIndex(entryReadPosition.getChapterHref()));
}
mFolioPageViewPager.setCurrentItem(getChapterIndex(entryReadPosition));
}
}

/**
* Returns the index of the chapter by following priority -
* 1. id
* 2. href
* 3. index
* @param readPosition Last read position
* @return index of the chapter
*/
private int getChapterIndex(ReadPosition readPosition) {

if (readPosition == null) {
return 0;

} else if (!TextUtils.isEmpty(readPosition.getChapterId())) {
return getChapterIndex("id", readPosition.getChapterId());

} else if (!TextUtils.isEmpty(readPosition.getChapterHref())) {
return getChapterIndex("href", readPosition.getChapterHref());

} else if (readPosition.getChapterIndex() > -1
&& readPosition.getChapterIndex() < mSpineReferenceList.size()) {
return readPosition.getChapterIndex();
}

return 0;
}

private int getChapterIndex(String chapterHref) {
private int getChapterIndex(String caseString, String value) {

for (int i = 0; i < mSpineReferenceList.size(); i++) {
if (mSpineReferenceList.get(i).getHref().equals(chapterHref))
return i;
switch (caseString) {
case "id":
if (mSpineReferenceList.get(i).getId().equals(value))
return i;
case "href":
if (mSpineReferenceList.get(i).getOriginalHref().equals(value))
return i;
}
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public void onStop() {
try {
synchronized (this) {
mWebview.loadUrl("javascript:getFirstVisibleSpan(false)");
wait(6000);
wait(2000);
}
} catch (InterruptedException e) {
Log.e(TAG, "-> " + e);
Expand All @@ -651,8 +651,8 @@ public void onStop() {
public void storeFirstVisibleSpan(boolean usingId, String value) {

synchronized (this) {
ReadPositionImpl readPositionImpl = new ReadPositionImpl(mBookId, mPosition,
spineItem.getHref(), usingId, value);
ReadPositionImpl readPositionImpl = new ReadPositionImpl(mBookId, spineItem.getId(),
spineItem.getOriginalHref(), mPosition, usingId, value);
Intent intent = new Intent(FolioReader.ACTION_SAVE_READ_POSITION);
intent.putExtra(FolioReader.EXTRA_READ_POSITION, readPositionImpl);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
Expand Down
11 changes: 6 additions & 5 deletions sample/src/main/assets/read_positions/read_position.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"bookId" : "_simple_book",
"chapterIndex" : -1,
"chapterHref" : "OEBPS/ch10.xhtml",
"usingId" : false,
"value" : "28"
"bookId": "_simple_book",
"chapterId": "id-idp140724860948704",
"chapterHref": "ch03.xhtml",
"chapterIndex": 5,
"usingId": false,
"value": "0"
}
2 changes: 1 addition & 1 deletion webViewMarker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/FolioReader/FolioReader-Android'
gitUrl = 'https://github.com/FolioReader/FolioReader-Android.git'

libraryVersion = '0.3.6'
libraryVersion = '0.3.7'

developerId = 'mobisystech'
developerName = 'Folio Reader'
Expand Down

0 comments on commit 62a61da

Please sign in to comment.