NightWhistler / PageTurner

Android e-book reader with cloud synchronization
pageturner-reader.org
GNU General Public License v3.0
466 stars 222 forks source link

Go to Page (jump directly to user entered page number) #592

Open Pinkal7600 opened 7 years ago

Pinkal7600 commented 7 years ago

Description

I have implement GO TO page feature in this App. It help user to jump directly on entered page number. I have got most of success, and in most of cases i can jump successfully at given page number.

I have implement this feature in search which is already present and comments old code as given below in Edited Files>ReadingFragment.java>performSearch(String query)

Issues

(1). In some cases page number jump to diffrent pages, like if i enterd 110 then it will redirect to 109. (2). when i want to jump at chapter starting page number then page number is changed but chapter containt does not changes. like if chapter start with page no. 49, I am on Page no. 99, when i enter 49 then page number is been change to 49 but containt is of page no. 99.

Edited Files

PageChangeStrategy.java

public void gotoPage(int pageNumber);

BookView.java

`public void gotoPageNumber(int pageNum) {

strategy.gotoPage(pageNum);
progressUpdate();

}`

FixedPagesStrategy.java

`@Override public void gotoPage(int pageNumber) {

PageTurnerSpine spinePos = bookView.getSpine();

this.storedPosition = -1;

int currentPage = getCurrentPage() + spinePos.getUptoPage(spinePos.getPosition());
Log.e(TAG, "Adding >> Upto Page : " + spinePos.getUptoPage(spinePos.getPosition())
        + ", currentPage :  " + getCurrentPage());
Log.e(TAG, "pagenum : " + pageNum);

if (pageNumber > currentPage) {     //pageNumber is greater then current page

    int jumpSpine = spinePos.getIndexFromPage(pageNumber);
    int currentSpine = spinePos.getPosition();
    Log.e(TAG, "jumpSpine : " + jumpSpine + ", currentSpine : " + currentSpine);

    if (jumpSpine == currentSpine) {

        int diffrence = pageNumber - currentPage;
        Log.e(TAG, "diffrence < : " + diffrence);

        Log.e(TAG, "Minimum >> PageOffSets - 1 : " + (spinePos.getPageOffSets(currentSpine) - 1)
                + ", pageNum + diffrence : " + (pageNum + diffrence));

        this.pageNum = Math.min(pageNum + diffrence, spinePos.getPageOffSets(currentSpine) - 1);
        updatePosition();

    } else {

        PageTurnerSpine spine = bookView.getSpine();

        if (spine == null || !spine.navigateFrontSpine(spine.getIndexFromPage(pageNumber))) {
            return;
        }
        this.pageNum = 0;

        gotoPage(pageNumber);

    }

} else {                            //pageNumber is less then current page

    int jumpSpine = spinePos.getIndexFromPage(pageNumber);
    int currentSpine = spinePos.getPosition();
    Log.e(TAG, "jumpSpine : " + jumpSpine + ", currentSpine : " + currentSpine);

    if (jumpSpine == currentSpine) {

        int diffrence = currentPage - pageNumber;
        Log.e(TAG, "diffrence > : " + diffrence);

        Log.e(TAG, "pagenum - diffrence : " + (pageNum - diffrence));

        this.pageNum = Math.max(pageNum - diffrence, 0);
        updatePosition();

    } else {

        PageTurnerSpine spine = bookView.getSpine();

        if (spine == null || !spine.navigateBackSpine(spine.getIndexFromPage(pageNumber))) {
            return;
        }
          this.storedPosition = Integer.MAX_VALUE;
        this.pageNum = 0;

        gotoPage(pageNumber);
    }
}

Log.e(TAG, "In last pageNum : " + pageNum);

}`

PageTurnerSpine.java

`// get index from page number public int getIndexFromPage(int pageNumber) { int total = 0; int totalIndex = 0;

for (List<Integer> pagesPerSection : pageOffsets) {
    total += pagesPerSection.size();
    totalIndex = totalIndex + 1;
    if (total - 1 >= pageNumber) {
        return totalIndex - 1;
    }
}
return 0;

}

// get total pages before given index public int getUptoPage(int position) { int total = 0, max = 0; for (List pagesPerSection : pageOffsets) { max = max + 1;

    if (position == max - 1) {
        return total;
    }
    total += pagesPerSection.size();
}
return 0;

}

// get current index pageOffSets public int getPageOffSets(int position) { int max = 0; for (List pagesPerSection : pageOffsets) { max = max + 1;

    if (position == max - 1) {
        return pagesPerSection.size();
    }
}
return 0;

}

// jump to particular index forward public boolean navigateFrontSpine(int indexSpine) {

if (this.position == size() - 1) {
    return false;
}

this.position = indexSpine;
return true;

}

// jump to particular index backward public boolean navigateBackSpine(int indexSpine) {

if (this.position == 0) {
    return false;
}

this.position = indexSpine;
return true;

}`

ReadingFragment.java

`public void performSearch(String query) {

int index = Integer.parseInt(query);

if (index > bookView.getTotalNumberOfPages()) {
        Toast.makeText(context, "Please enter number between 0 to " + bookView.getTotalNumberOfPages(), Toast.LENGTH_SHORT).show();
} else {
        bookView.gotoPageNumber(index);
}

// final ProgressDialog searchProgress = new ProgressDialog(context); // searchProgress.setOwnerActivity(getActivity()); // searchProgress.setCancelable(true); // searchProgress.setMax(100); // searchProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // // // final int[] counter = {0}; //Yes, this is essentially a pointer to an int :P // // final SearchTextTask task = new SearchTextTask(bookView.getBook()); // // task.setOnPreExecute(() -> { // // searchProgress.setMessage(getString(R.string.search_wait)); // searchProgress.show(); // // // Hide on-screen keyboard if it is showing // InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); // imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // // }); // // task.setOnProgressUpdate((values) -> { // // if (isAdded()) { // // LOG.debug("Found match at index=" + values[0].getIndex() // + ", offset=" + values[0].getStart() + " with context " // + values[0].getDisplay()); // SearchResult res = values[0]; // // if (res.getDisplay() != null) { // counter[0] = counter[0] + 1; // String update = String.format( // getString(R.string.search_hits), counter[0]); // searchProgress.setMessage(update); // } // // searchProgress.setProgress(bookView.getPercentageFor(res.getIndex(), res.getStart())); // } // }); // // task.setOnCancelled((result) -> { // if (isAdded()) { // Toast.makeText(context, R.string.search_cancelled, // Toast.LENGTH_LONG).show(); // } // }); // // task.setOnPostExecute((result) -> { // searchProgress.dismiss(); // // if (!task.isCancelled() && isAdded()) { // // List resultList = result.getOrElse(new ArrayList<>()); // // if (resultList.size() > 0) { // searchResults = resultList; // showSearchResultDialog(resultList); // } else { // Toast.makeText(context, // R.string.search_no_matches, Toast.LENGTH_LONG) // .show(); // } // } // }); // // searchProgress.setOnCancelListener(dialog -> task.cancel(true)); // executeTask(task, query);

}`

Thanks.