Skip to content

Commit

Permalink
Fix for "Text annotations aren't reliably changing color" Closes #187 :
Browse files Browse the repository at this point in the history
Fix thumbnail and annotation previews not being updated sometimes after annotation gets changed.
Fix intermittent bug that due to a race condition led to annotation not getting assigned new color after user goes back from color editing screen back to PdfScreen on phones.
  • Loading branch information
Dima-Android committed Nov 12, 2024
1 parent 53ccf71 commit c32236a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.inject.Singleton

@Singleton
class AnnotationPreviewFileCache @Inject constructor(
dispatchers: Dispatchers,
private val dispatchers: Dispatchers,
private val fileStore: FileStore,
private val annotationPreviewMemoryCache: AnnotationPreviewMemoryCache,
) {
Expand Down Expand Up @@ -44,6 +44,7 @@ class AnnotationPreviewFileCache @Inject constructor(
fun cancelProcessing() {
currentlyProcessingAnnotations.clear()
coroutineScope.cancel()
coroutineScope = CoroutineScope(dispatchers.default)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import javax.inject.Singleton

@Singleton
class AnnotationPreviewManager @Inject constructor(
dispatchers: Dispatchers,
private val dispatchers: Dispatchers,
private val fileStore: FileStore,
private val memoryCache: AnnotationPreviewMemoryCache,
private val context: Context,
Expand Down Expand Up @@ -224,5 +224,6 @@ class AnnotationPreviewManager @Inject constructor(
fun cancelProcessing() {
currentlyProcessingAnnotations.clear()
coroutineScope.cancel()
coroutineScope = CoroutineScope(dispatchers.default)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1069,17 +1069,10 @@ class PdfReaderViewModel @Inject constructor(
requests.add(request)
}

if(requests.isEmpty()) { return }

viewModelScope.launch {
perform(
dbWrapper = dbWrapperMain,
writeRequests = requests
).ifFailure {
Timber.e(it, "PDFReaderViewModel: can't update changed annotations")
return@launch
}
if (requests.isEmpty()) {
return
}
dbWrapperMain.realmDbStorage.perform(requests)

pdfFragment.notifyAnnotationHasChanged(annotation)
//TODO
Expand All @@ -1088,7 +1081,8 @@ class PdfReaderViewModel @Inject constructor(
private fun processAnnotationObserving(
annotation: Annotation,
changes: List<String>,
pdfReaderNotification: PdfReaderNotification
pdfReaderNotification: PdfReaderNotification,
ignoreDebouncer: Boolean = false,
) {

when (pdfReaderNotification) {
Expand All @@ -1110,13 +1104,18 @@ class PdfReaderViewModel @Inject constructor(
val key = annotation.key
if (key != null) {
// if (changes.contains("rotation") || freeTextAnnotation.rotation != 0) {
onAnnotationChangedDebouncerFlow.tryEmit(
Triple(
Random.nextInt(),
adjustedAnnotations,
annotation

if (ignoreDebouncer) {
change(annotation = annotation, adjustedAnnotations)
} else {
onAnnotationChangedDebouncerFlow.tryEmit(
Triple(
Random.nextInt(),
adjustedAnnotations,
annotation
)
)
)
}
// } else {
// val k = onAnnotationChangedDebouncerFlow.value
// if (k != null) {
Expand Down Expand Up @@ -1742,7 +1741,9 @@ class PdfReaderViewModel @Inject constructor(
if (color != null && color.first != annotation.color) {
changes.add(PdfAnnotationChanges.color)
}
var ignoreDebouncer = true
if (contents != null && contents != annotation.comment) {
ignoreDebouncer = false
changes.add(PdfAnnotationChanges.contents)
}

Expand Down Expand Up @@ -1779,9 +1780,10 @@ class PdfReaderViewModel @Inject constructor(
}

processAnnotationObserving(
pdfAnnotation,
PdfAnnotationChanges.stringValues(changes),
PdfReaderNotification.PSPDFAnnotationChanged
annotation = pdfAnnotation,
changes = PdfAnnotationChanges.stringValues(changes),
pdfReaderNotification = PdfReaderNotification.PSPDFAnnotationChanged,
ignoreDebouncer = ignoreDebouncer,
)
}

Expand Down Expand Up @@ -3059,15 +3061,7 @@ class PdfReaderViewModel @Inject constructor(
dateParser = this.dateParser
)

viewModelScope.launch {
perform(
dbWrapper = dbWrapperMain,
request = request
).ifFailure {
Timber.e(it, "PDFReaderViewModel: can't update annotation $key")
return@launch
}
}
dbWrapperMain.realmDbStorage.perform(request)
}

private fun submitPendingPage(page: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import javax.inject.Singleton

@Singleton
class ThumbnailPreviewManager @Inject constructor(
dispatchers: Dispatchers,
private val dispatchers: Dispatchers,
private val fileStore: FileStore,
private val memoryCache: ThumbnailPreviewMemoryCache,
private val context: Context,
Expand Down Expand Up @@ -171,5 +171,6 @@ class ThumbnailPreviewManager @Inject constructor(
fun cancelProcessing() {
currentlyProcessingThumbnails.clear()
coroutineScope.cancel()
coroutineScope = CoroutineScope(dispatchers.default)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.inject.Singleton

@Singleton
class ThumbnailsPreviewFileCache @Inject constructor(
dispatchers: Dispatchers,
private val dispatchers: Dispatchers,
private val fileStore: FileStore,
private val thumbnailPreviewMemoryCache: ThumbnailPreviewMemoryCache,
) {
Expand Down Expand Up @@ -44,6 +44,7 @@ class ThumbnailsPreviewFileCache @Inject constructor(
fun cancelProcessing() {
currentlyProcessingThumbnails.clear()
coroutineScope.cancel()
coroutineScope = CoroutineScope(dispatchers.default)
}

}

0 comments on commit c32236a

Please sign in to comment.