Mantano / iridium

Unofficial Dart/Flutter port of some of the Readium 2 components
80 stars 27 forks source link

Controller for custom logics? #78

Open lzzy12 opened 1 year ago

lzzy12 commented 1 year ago

Hi, my use-case requires to get the page number the user currently is on and the total number of pages. Is this possible to get with the EpubScreen.fromFile constructor or is there any other way to do this? I am looking for something like a controller class for the reader.

jmgeffroy commented 1 year ago

Hi @lzzy12, In fact, the ReaderContext does provide a currentLocationStream, which seems to be what you need if I understand correctly. To see an example in action, you can have a look at this code in the ReaderToolbar class. If access to this stream is what you ned, I'll think about a clean way to provide access to it.

lzzy12 commented 1 year ago

Yes it will be helpful if this could be available through an API outside the package.

jmgeffroy commented 1 year ago

@lzzy12 I have added a paginationCallback parameter to the EpubScreen API, and adapted the reader widget example to showcase it:

        EpubScreen.fromPath(
          filePath: widget.dirPath,
          location: '{"cfi":" ","idref":"file_12.html"}',
          paginationCallback: (paginationInfo) {
            Fimber.d("--- paginationInfo: $paginationInfo");
          },
        )

The PaginationInfo class contains a lot of information about the position.

Does it match your requirements ?

lzzy12 commented 1 year ago

Hi, thanks for your quick responses. While this does give me the current page number, I could not figure out which one of the attributes stores the total number of pages in the epub file. Could you please point me to it?

jmgeffroy commented 1 year ago

Hello @lzzy12, in fact PaginationInfo contains a page method, which is defined as follows (pagination_info.dart):

  int get page =>
      linkPagination.firstPageNumber +
      percent * (linkPagination.pagesCount - 1) ~/ 100;
lzzy12 commented 1 year ago

Hi @jmgeffroy Currently this page method just gives me the current page number user is on. How do I get the total number of pages present in the epub file?

lzzy12 commented 1 year ago

Also the perccent seems to be giving wrong information. It seems inconsistent with what is currently on the UI

Screenshot_1683099559 I get this when I log the percent field [log] percent: 23 [log] percent: 23

My code:

EpubScreen.fromPath(
                        filePath: controller.bookPath!,
                        paginationCallback: (data) {
                          controller.ePubPageCount.value =
                              data.linkPagination.pagesCount;
                          controller.ePubPage.value =
                              data.percent ~/ data.linkPagination.pagesCount;
                          log("percent: ${data.percent.toString()}");
                        },
                      )
jmgeffroy commented 1 year ago

Hi @lzzy12, That's a good point; thank you for your feedback. Based on your observations, I have to look into this more. Obviously, page counts in reflowable epubs are approximations, but not as bad as what you are experiencing. Today I'm busy, but I'll look at it asap.

lzzy12 commented 1 year ago

Hi @jmgeffroy, did you get a chance to look at this issue?