-
-
Notifications
You must be signed in to change notification settings - Fork 34
Fix bug when the background wasn't loaded correctly on pdf change #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,6 @@ import kotlinx.coroutines.CoroutineScope | |
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.coroutineScope | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.buffer | ||
| import kotlinx.coroutines.launch | ||
|
|
@@ -666,6 +665,16 @@ class PageDataManager @Inject constructor( | |
| } | ||
|
|
||
| fun setBackground(pageId: String, background: CachedBackground) { | ||
| if (background.bitmap == null) { | ||
| val msg = "setBackground: skipping cache write, bitmap is null (id=${background.id})" | ||
| log.w(msg) | ||
| appEventBus.tryEmit( | ||
| AppEvent.GenericError( | ||
| msg | ||
| ) | ||
| ) | ||
| return | ||
| } | ||
| dataScope.launch { | ||
| // we assume that the pageId is in current notebook. | ||
| val observeBg = appRepository.isObservable(pageFromDb?.notebookId) | ||
|
|
@@ -749,26 +758,38 @@ class PageDataManager @Inject constructor( | |
| val eventString = fileObserverEventNames(event) | ||
|
|
||
| log.d("Background file changed: $filePath [event=$eventString]") | ||
|
|
||
| // HANDLE DELETION / RE-REGISTRATION | ||
| if (event == DELETE || event == DELETE_SELF) { | ||
| log.d("Background file deleted.") | ||
| log.d("Background file deleted. Waiting for recreation...") | ||
| synchronized(fileObservers) { | ||
| fileObservers.remove(filePath)?.stopWatching() | ||
| } | ||
|
|
||
| if (!waitForFileAvailable(filePath)) { | ||
| log.w("File changed, but does not exist: $filePath") | ||
| log.w("File disappeared and did not return: $filePath") | ||
| appEventBus.tryEmit( | ||
| AppEvent.ActionHint( | ||
| "Background does not exist", | ||
| 3000 | ||
| ) | ||
| ) | ||
| return@launch | ||
| } else | ||
| } else { | ||
| // RE-REGISTER: This starts a new observer on the new file handle | ||
| observeBackgroundFile(pageId, filePath) | ||
| } | ||
| } | ||
|
|
||
| // IMPORTANT: Return here. Do NOT emit invalidateFileFlow. | ||
| // We wait for the CLOSE_WRITE event from the NEW observer. | ||
| return@launch | ||
| } | ||
|
|
||
| invalidateFileFlow.emit(filePath) | ||
| // HANDLE ACTUAL DATA UPDATES | ||
| // Only emit when the file is ready to be read (CLOSE_WRITE or MOVED_TO) | ||
| if (event == CLOSE_WRITE || event == MOVED_TO) { | ||
| log.d("File is stable. Invalidating flow for: $filePath") | ||
| invalidateFileFlow.emit(filePath) | ||
| } | ||
|
Comment on lines
+788
to
+792
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,6 @@ fun loadBackgroundBitmap(filePath: String, pageNumber: Int, scale: Float): Bitma | |
| renderPdfPageMuPdf(filePath, pageNumber, targetWidth.toInt(), resolutionModifier = 1.5f) | ||
| else | ||
| renderPdfPageAndroid(file, pageNumber, targetWidth.toInt(), resolutionModifier = 1.2f) | ||
| timer.end("loaded background") | ||
| timer.end("loaded background, newBitmap: $newBitmap") | ||
|
||
| return newBitmap | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setBackground()emitsAppEvent.GenericErrorwhenbackground.bitmapis null. SinceGenericErroris user-facing (snackbar) andsetBackground()can be hit repeatedly during rendering/zoom while a file is temporarily unavailable, this risks spamming users for transient conditions. Consider downgrading this to logging (or a throttled/once-per-file ActionHint) and only emitting a user-visible error after repeated/persistent failures.