smartmobilefactory / EpubReaderAndroid

MIT License
24 stars 7 forks source link

Saving & retrieving the current location #11

Closed voipworld closed 6 years ago

voipworld commented 6 years ago

hello i love the epub reader view... please can you give me a basic example on how to save the current location & page on a variable, and how to move to that variable later? thanks!

timfreiheit commented 6 years ago

you can retrieve the current location by using

epubView.getCurrentLocation()

or by observing

epubView.currentLocation()

this location will either return a XPathLocation or raw ChapterLocation. (XPathLocation is also a ChapterLocation)

to save the last location you can do something like

epubView.currentLocation()
       .subscribe(location -> {
           if  (location instanceof XPathLocation) {
               XPathLocation xPathLocation = (XPathLocation) location;
               doSomethingWithChapterIndexAndXPath(xPathLocation.chapter(), xPathLocation.xPath());
           } else if (location instanceof ChapterLocation) {
                int chapter = ((ChapterLocation) location).chapter();
                doSomethingWithChapterIndex(chapter);
           }
       });

you can set the current location in the epub in two ways.

epubView.setEpub(epub, location);

when you want to set the initial location of the epub after loading. or

epubView.gotoLocation(location);

when the epub is already loaded.

You can create such a EpubLocation by using its builders

XPathLocation xPathLocation = EpubLocation.fromXPath(chapter, xPath);
ChapterLocation chapterLocation = EpubLocation.fromChapter(chapter);

If you can directly save the xPath into preferences depends highly on your use case. If the epub can be updated the xPath normally changes.

voipworld commented 6 years ago

i want to save thre current location of the ePub in shared preferences, and load it when the app start but till now unable to get it... also the book is not opening in the first page in the given example in git... so i was trying to go to the initial location on first startup.

On 12/8/17, Tim Freiheit notifications@github.com wrote:

you can retrieve the current location by using

epubView.getCurrentLocation()

or by observing

epubView.currentLocation()

this location will either return a XPathLocation or raw ChapterLocation. (XPathLocation is also a ChapterLocation)

to save the last location you can do something like

epubView.currentLocation()
       .subscribe(location -> {
           if  (location instanceof XPathLocation) {
               XPathLocation xPathLocation = (XPathLocation) location;
               doSomethingWithChapterIndexAndXPath(xPathLocation.chapter(),
xPathLocation.xPath());
           } else if (location instanceof ChapterLocation) {
                int chapter = ((ChapterLocation) location).chapter();
                doSomethingWithChapterIndex(chapter);
           }
       });

you can set the current location in the epub in two ways.

epubView.setEpub(epub, location);

when you want to set the initial location of the epub after loading. or

epubView.gotoLocation(location);

when the epub is already loaded.

You can create such a EpubLocation by using its builders

XPathLocation xPathLocation = EpubLocation.fromXPath(chapter, xPath);
ChapterLocation chapterLocation = EpubLocation.fromChapter(chapter);

If you can directly save the xPath into preferences depends highly on your use case. If the epub can be updated the xPath normally changes.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/smartmobilefactory/EpubReaderAndroid/issues/11#issuecomment-350291439

timfreiheit commented 6 years ago

How do you try to set the initial location and what DisplayStrategy are you using? If you are using the xPathLocation the EpubView should at least navigate to the correct chapter.

voipworld commented 6 years ago

i am just using your example apps that use several display strategy but when book open it dont go to the first page (cover) but to some pages from the few first pages i still try to change location on startup.

On 12/8/17, Tim Freiheit notifications@github.com wrote:

How do you try to set the initial location and what DisplayStrategy are you using? If you are using the xPathLocation the EpubView should at least navigate to the correct chapter.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/smartmobilefactory/EpubReaderAndroid/issues/11#issuecomment-350295199

voipworld commented 6 years ago

hello just to let you now, i can now save the current location by page... thanks a lot for this SDK!!!!!!!!!!!!!!!!!

voipworld commented 6 years ago

He actualy i am using binding.epubView.gotoLocation(EpubLocation.fromChapter(chapter)); to go the location (by chapter) but i dont know how to do it by xPath, because i dont know how to convert xPath location to string to be saved in shared pref saving chapter like this: editor.putInt("location", chapter); thanks!

timfreiheit commented 6 years ago

a XPathLocation can be created by using EpubLocation.fromXPath(chapter, xPath) it is up to you to save the xPath-String next to the chapter. when you want to do it in SharedPreferences you probably want to save it in an extra field.

editor.putInt("locationChapter", location.chapter());
editor.putInt("locationXPath", location.xPath());
voipworld commented 6 years ago

ok, so do location.chapter represent the actual chapter, or the actual page? at i am doing location by chapter, i found myself moving by page, not by chapter at one chapter can have several pages.

On 3/13/18, Tim Freiheit notifications@github.com wrote:

a XPathLocation can be created by using EpubLocation.fromXPath(chapter, xPath) it is up to you to save the xPath-String next to the chapter. when you want to do it in SharedPreferences you probably want to save it in an extra field.

editor.putInt("locationChapter", location.chapter());
editor.putInt("locationXPath", location.xPath());

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/smartmobilefactory/EpubReaderAndroid/issues/11#issuecomment-372617265

timfreiheit commented 6 years ago

The chapters in the library are the different html-files in the epub. they may not match the chapters in the table of contents