Giter Club home page Giter Club logo

Comments (5)

mertakdut avatar mertakdut commented on August 14, 2024

Hi,

There is no way to calculate number of sections without actually finish reading the book. The whole book is not calculated and held in memory, which is the purpose of the library: reducing memory consumption.

Though you can still use viewpager; by setting the page count of adapter to max at the beginning, when you get OutOfPagesException from reader class' readSection method, you can just update the page count and call notifyDataSetChanged on adapter.

Check EpubParser-Sample-Android-Application out for more info.

from epubparser.

mkaflowski avatar mkaflowski commented on August 14, 2024

This is my soulution for Android. I have added this code to Reader class.

    public int loadPageCount(Context context) {
        System.out.println("loadPageCount");
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        if (!sharedPreferences.contains("PAGE_NUM-" + content.getZipFilePath()))
            savePageCount(context, designatePageCount(context));
        return sharedPreferences.getInt("PAGE_NUM-" + content.getZipFilePath(), Integer.MAX_VALUE);
    }

    private int designatePageCount(Context context) {
        System.out.println("designatePageCount");
        try {
            readSection(Integer.MAX_VALUE);
        } catch (ReadingException e) {
            e.printStackTrace();
        } catch (OutOfPagesException e) {
            e.printStackTrace();
            return e.getPageCount();
        }
        return Integer.MAX_VALUE;
    }

    private void savePageCount(Context context, int num) {
        System.out.println("savePageCount");

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        sharedPreferences.edit().putInt("PAGE_NUM-" + content.getZipFilePath(), num).apply();
    }

from epubparser.

mertakdut avatar mertakdut commented on August 14, 2024

Well, this is a solution for acquiring the page count, no doubt. But designatePageCount method's required time and cpu will increase in parallel to the size of books.

Avoid calculating the page count if you can, but if you couldn't, better call saveProgress to avoid parsing books again.

Thank you for your participation.

from epubparser.

mkaflowski avatar mkaflowski commented on August 14, 2024

Thank you for response. I find knowing page count very useful because when I set page number to bookPager.setCurrentItem in the adapter when you have Integer.MAX_VALUE element in the adapter it can freeze Android for a while. Especially when you go to a previous pages from a big number is a small number (e.g. 550 -> 1).

I my solution I check the page count only once per book.

from epubparser.

mertakdut avatar mertakdut commented on August 14, 2024

It freezes because of calling readSection for (destinationPage - currentPage) times (if the pages between are not parsed before). And if the page is not parsed before readSection will spend extra parsing time.

Spreading the parsing process over time is the best course of action. However, to get the page count, currently you are forced to parse the whole book before. I'll try to think about a workaround for this.

from epubparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.