Skip to content

Commit

Permalink
Merge pull request #186 from codetoart/master
Browse files Browse the repository at this point in the history
Last Read position improved
  • Loading branch information
Mahavir Jain committed Apr 24, 2018
2 parents 848a722 + 5181cf3 commit 1f212b2
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 204 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ android:
components:
- build-tools-26.0.2
- android-26
- android-19
- extra-android-m2repository

licenses:
Expand Down
51 changes: 38 additions & 13 deletions folioreader/src/main/assets/js/Bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,32 +574,57 @@ function isElementVisible(element, isHorizontal) {
return rect.top > 0;
}

function getFirstVisibleSpanIndex(isHorizontal) {
/**
Gets the first visible span from the displayed chapter and if it has id then usingId is true with
value as span id else usingId is false with value as span index. usingId and value is forwarded to
FolioPageFragment#storeFirstVisibleSpan(boolean, String) JavascriptInterface.
@param {boolean} isHorizontal - scrolling type of DirectionalViewpager#mDirection
*/
function getFirstVisibleSpan(isHorizontal) {

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

for (var i = 0 ; i < spanCollection.length ; i++) {
if (spanCollection.length == 0) {
FolioPageFragment.storeFirstVisibleSpan(false, 0);
return;
}

if (isElementVisible(spanCollection[i], isHorizontal)) {
var spanIndex = 0;
var spanElement;

json.value = i;
for (var i = 0 ; i < spanCollection.length ; i++) {
if (isElementVisible(spanCollection[i], isHorizontal)) {
spanIndex = i;
spanElement = spanCollection[i];
break;
}
}

FolioPageFragment.storeFirstVisibleSpanIndex(JSON.stringify(json));
var usingId = spanElement.id ? true : false;
var value = usingId ? spanElement.id : spanIndex;
FolioPageFragment.storeFirstVisibleSpan(usingId, value);
}

function scrollToSpanIndex(index) {
/**
Scrolls the web page to particular span using id or index
var spanCollection = document.getElementsByTagName("span");
@param {boolean} usingId - if span tag has id then true or else false
@param {number} value - if usingId true then span id else span index
*/
function scrollToSpan(usingId, value) {

if (spanCollection.length == 0 || index < 0 || index >= spanCollection.length)
return;

goToEl(spanCollection[index]);
if (usingId) {
var spanElement = document.getElementById(value);
if (spanElement)
goToEl(spanElement);
} else {
var spanCollection = document.getElementsByTagName("span");
if (spanCollection.length == 0 || value < 0 || value >= spanCollection.length
|| value == null)
return;
goToEl(spanCollection[value]);
}
}

// Class based onClick listener
Expand Down
4 changes: 0 additions & 4 deletions folioreader/src/main/java/com/folioreader/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public class Constants {
public static final String CHAPTER_SELECTED = "chapter_selected";
public static final String HIGHLIGHT_SELECTED = "highlight_selected";
public static final String BOOK_TITLE = "book_title";
public static final String BOOK_ID = "book_id";
public static final String LAST_READ_SPAN_INDEX = "last_read_span_index";
public static final String LAST_READ_CHAPTER_INDEX = "last_read_chapter_index";
public static final String LAST_READ_STATE = "_last_read_state";
public static final int PORT_NUMBER = 8080;
public static final String LOCALHOST = "http://127.0.0.1:" + PORT_NUMBER + "/";
public static final String SELECTED_WORD = "selected_word";
Expand Down
19 changes: 19 additions & 0 deletions folioreader/src/main/java/com/folioreader/model/ReadPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.folioreader.model;

/**
* Created by Hrishikesh Kadam on 20/04/2018.
*/
public interface ReadPosition {

String getBookId();

int getChapterIndex();

String getChapterHref();

boolean isUsingId();

String getValue();

String toJson();
}
135 changes: 135 additions & 0 deletions folioreader/src/main/java/com/folioreader/model/ReadPositionImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.folioreader.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

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.
*/
public class ReadPositionImpl implements ReadPosition, Parcelable {

public static final Creator<ReadPositionImpl> CREATOR = new Creator<ReadPositionImpl>() {
@Override
public ReadPositionImpl createFromParcel(Parcel in) {
return new ReadPositionImpl(in);
}

@Override
public ReadPositionImpl[] newArray(int size) {
return new ReadPositionImpl[size];
}
};

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

public ReadPositionImpl() {
}

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

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

@Override
public String toString() {
return "ReadPositionImpl{" +
"bookId='" + bookId + '\'' +
", chapterIndex=" + chapterIndex +
", chapterHref='" + chapterHref + '\'' +
", usingId=" + usingId +
", value='" + value + '\'' +
'}';
}

@Override
public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

@Override
public int getChapterIndex() {
return chapterIndex;
}

public void setChapterIndex(int chapterIndex) {
this.chapterIndex = chapterIndex;
}

@Override
public String getChapterHref() {
return chapterHref;
}

public void setChapterHref(String chapterHref) {
this.chapterHref = chapterHref;
}

@Override
public boolean isUsingId() {
return usingId;
}

public void setUsingId(boolean usingId) {
this.usingId = usingId;
}

@Override
public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public String toJson() {
try {
ObjectWriter objectWriter = ObjectMapperSingleton.getObjectMapper().writer();
return objectWriter.writeValueAsString(this);
} catch (JsonProcessingException e) {
Log.e(LOG_TAG, "-> " + e);
return null;
}
}

@Override
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 @@ -25,11 +25,9 @@
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
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 @@ -46,6 +44,7 @@
import com.folioreader.Constants;
import com.folioreader.R;
import com.folioreader.model.HighlightImpl;
import com.folioreader.model.ReadPosition;
import com.folioreader.model.event.AnchorIdEvent;
import com.folioreader.model.event.MediaOverlayHighlightStyleEvent;
import com.folioreader.model.event.MediaOverlayPlayPauseEvent;
Expand Down Expand Up @@ -93,9 +92,7 @@ public class FolioActivity
public static final String INTENT_EPUB_SOURCE_PATH = "com.folioreader.epub_asset_path";
public static final String INTENT_EPUB_SOURCE_TYPE = "epub_source_type";
public static final String INTENT_HIGHLIGHTS_LIST = "highlight_list";
public static final String EXTRA_LAST_READ_CHAPTER_INDEX = "com.folioreader.extra_last_read_chapter_position";
public static final String EXTRA_LAST_READ_SPAN_INDEX = "com.folioreader.extra_last_read_span_index";
public static final String ACTION_SAVE_LAST_READ_STATE = "save_last_read_state";
public static final String EXTRA_READ_POSITION = "com.folioreader.extra.READ_POSITION";

public enum EpubSourceType {
RAW,
Expand All @@ -115,7 +112,7 @@ public enum EpubSourceType {

private int mChapterPosition;
private FolioPageFragmentAdapter mFolioPageFragmentAdapter;
private String lastReadSpanIndex;
private ReadPosition entryReadPosition;
private ConfigBottomSheetDialogFragment mConfigBottomSheetDialogFragment;
private TextView title;

Expand Down Expand Up @@ -284,17 +281,28 @@ public void onPageScrollStateChanged(int state) {
if (mSpineReferenceList != null) {
mFolioPageFragmentAdapter = new FolioPageFragmentAdapter(getSupportFragmentManager(), mSpineReferenceList, bookFileName, mBookId);
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()));
}
}
}

int lastReadChapterIndex =
getIntent().getIntExtra(FolioActivity.EXTRA_LAST_READ_CHAPTER_INDEX, 0);
String lastReadSpanIndex =
getIntent().getStringExtra(FolioActivity.EXTRA_LAST_READ_SPAN_INDEX);
if (TextUtils.isEmpty(lastReadSpanIndex))
lastReadSpanIndex = "{\"usingId\":false,\"value\":0}";
mFolioPageViewPager.setCurrentItem(lastReadChapterIndex);
AppUtil.saveLastReadState(
getApplicationContext(), mBookId, lastReadChapterIndex, lastReadSpanIndex);
private int getChapterIndex(String chapterHref) {

for (int i = 0; i < mSpineReferenceList.size(); i++) {
if (mSpineReferenceList.get(i).getHref().equals(chapterHref))
return i;
}
return 0;
}

private void configDrawerLayoutButtons() {
Expand Down Expand Up @@ -335,36 +343,14 @@ public void setPagerToPosition(String href) {
}

@Override
public void setLastReadSpanIndex(String json) {
lastReadSpanIndex = json;
}

@Override
protected void onStop() {
super.onStop();

// Activity is waiting to get lastReadSpanIndex stored from
// Bridge -> FolioPageFragment -> FolioActivity to avoid any race condition.
// This delay goes unnoticed as it is onStop() and not in onPause()
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.e(TAG, "-> " + e);
}

saveLastReadState();
}

private void saveLastReadState() {

if (mSpineReferenceList.size() > 0) {
public ReadPosition getEntryReadPosition() {

Intent intent = new Intent(FolioActivity.ACTION_SAVE_LAST_READ_STATE);
intent.putExtra(FolioActivity.EXTRA_LAST_READ_CHAPTER_INDEX,
mFolioPageViewPager.getCurrentItem());
intent.putExtra(FolioActivity.EXTRA_LAST_READ_SPAN_INDEX, lastReadSpanIndex);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
if (entryReadPosition != null) {
ReadPosition tempReadPosition = entryReadPosition;
entryReadPosition = null;
return tempReadPosition;
}
return null;
}

@Override
Expand Down

0 comments on commit 1f212b2

Please sign in to comment.