plateaukao / einkbro

A small, fast web browser based on Android WebView. It's tailored for E-Ink devices but also works great on normal android devices.
Other
1.18k stars 86 forks source link

ui: update progress dialog when saving EPUB #346

Closed uqs closed 7 months ago

uqs commented 7 months ago

hmm, now that I re-read the use of step_size and incremental updates, this will fail to indicate progress when there are more than 80 images to download, as the step_size will be 0 then.

My first version was using direct float math and setting the progress to fixed values, but that is uglier in code ... let me know what you think ...

plateaukao commented 7 months ago

I'll merge your pull request first and refactoring it.

plateaukao commented 7 months ago

@uqs You could check my refactoring commit here: https://github.com/plateaukao/einkbro/commit/813de8ee7a62d99910227fff1be171e31b78dc73

Now saveEpub() only deals the logic of saving epub, and update onProgressChanged() callback, so that in BrowserActivity, it can handle the ProgressDialog part.

Same for saveImageResources(). And for saveImageResources, it only provide the percentage of its own. It does not need to know what 80 means. That's the logic which should be handled in saveEpub or internalSaveEpub.

plateaukao commented 7 months ago

implementation to parallel downloading images is in this commit. you could also check if it's working as expected. https://github.com/plateaukao/einkbro/commit/0f8340e167cbdb33535ca876ef6f33e2b615a635

uqs commented 7 months ago

I get an exception with that:

2024-03-03 14:42:27.809 22106-22467 EpubManager             info.plateaukao.einkbro              D  Got content type: image/jpeg mediaType: image/jpeg
2024-03-03 14:42:27.822 22106-22466 EpubManager             info.plateaukao.einkbro              D  Got content type: image/jpeg mediaType: image/jpeg
2024-03-03 14:42:27.924 22106-22106 teaukao.einkbro         info.plateaukao.einkbro              W  Attempt to remove non-JNI local reference
2024-03-03 14:42:27.924 22106-22106 AndroidRuntime          info.plateaukao.einkbro              E  FATAL EXCEPTION: main
                                                                                                    Process: info.plateaukao.einkbro, PID: 22106
                                                                                                    java.util.ConcurrentModificationException
                                                                                                        at java.util.HashMap$HashIterator.nextNode(HashMap.java:1574)
                                                                                                        at java.util.HashMap$ValueIterator.next(HashMap.java:1602)
                                                                                                        at nl.siegmann.epublib.domain.Resources.containsId(Resources.java:129)
                                                                                                        at nl.siegmann.epublib.domain.Resources.createUniqueResourceId(Resources.java:112)
                                                                                                        at nl.siegmann.epublib.domain.Resources.fixResourceId(Resources.java:67)
                                                                                                        at nl.siegmann.epublib.domain.Resources.add(Resources.java:44)
                                                                                                        at nl.siegmann.epublib.domain.Book.addResource(Book.java:371)
                                                                                                        at info.plateaukao.einkbro.epub.EpubManager$saveImageResources$2$1$1.invokeSuspend(EpubManager.kt:278)
                                                                                                        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)
                                                                                                        Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@8326367, Dispatchers.Main]
2024-03-03 14:42:27.945 22106-22106 Process                 info.plateaukao.einkbro              I  Sending signal. PID: 22106 SIG: 9

Looks like epublib isn't thread-safe. It's easy to fix, will send PR shortly.