Skip to content

Commit 0341887

Browse files
kristofnemereclaude
andcommitted
Extract stamp generator registration to separate function, set flag on success
- Move stamp appearance generator registration into its own registerStampAppearanceGenerators() suspend function to keep loadAnnotations() clean, matching the original code structure. - Set stampAppearanceGeneratorsRegistered = true only after successful registration so a failed attempt can be retried on the next call. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b8c170d commit 0341887

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

libs/annotations/src/main/java/com/instructure/annotations/PdfSubmissionView.kt

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -482,31 +482,11 @@ abstract class PdfSubmissionView(context: Context, private val studentAnnotation
482482
// This will result in all of the comments being iterated over first as the COMMENT_REPLY type is last in the AnnotationType enum.
483483
val sortedAnnotationList = annotations.data.sortedByDescending { it.annotationType }
484484

485-
// Register stamp appearance generators only when stamps are present, and only after the network
486-
// roundtrip above — by this point PSPDFKit has finished its initial render, avoiding blank pages.
487485
// AnnotationType.TEXT is the CanvaDoc API type for stamp (point) annotations, which are converted
488-
// to PSPDFKit StampAnnotations via convertTextType().
489-
if (!stampAppearanceGeneratorsRegistered && sortedAnnotationList.any { it.annotationType == CanvaDocAnnotation.AnnotationType.TEXT }) {
490-
stampAppearanceGeneratorsRegistered = true
491-
val generators = withContext(Dispatchers.IO) {
492-
mapOf(
493-
blackStampSubject to AssetAppearanceStreamGenerator(blackStampFile),
494-
blueStampSubject to AssetAppearanceStreamGenerator(blueStampFile),
495-
brownStampSubject to AssetAppearanceStreamGenerator(brownStampFile),
496-
greenStampSubject to AssetAppearanceStreamGenerator(greenStampFile),
497-
navyStampSubject to AssetAppearanceStreamGenerator(navyStampFile),
498-
orangeStampSubject to AssetAppearanceStreamGenerator(orangeStampFile),
499-
pinkStampSubject to AssetAppearanceStreamGenerator(pinkStampFile),
500-
purpleStampSubject to AssetAppearanceStreamGenerator(purpleStampFile),
501-
redStampSubject to AssetAppearanceStreamGenerator(redStampFile),
502-
yellowStampSubject to AssetAppearanceStreamGenerator(yellowStampFile)
503-
)
504-
}
505-
val customStampAppearanceStreamGenerator = CustomStampAppearanceStreamGenerator()
506-
pdfFragment?.document?.annotationProvider?.addAppearanceStreamGenerator(customStampAppearanceStreamGenerator)
507-
generators.forEach { (subject, generator) ->
508-
customStampAppearanceStreamGenerator.addAppearanceStreamGenerator(subject, generator)
509-
}
486+
// to PSPDFKit StampAnnotations via convertTextType(). Register generators only after this network
487+
// roundtrip — by this point PSPDFKit has finished its initial render, avoiding blank pages.
488+
if (sortedAnnotationList.any { it.annotationType == CanvaDocAnnotation.AnnotationType.TEXT }) {
489+
registerStampAppearanceGenerators()
510490
}
511491

512492
for (item in sortedAnnotationList) {
@@ -1094,6 +1074,30 @@ abstract class PdfSubmissionView(context: Context, private val studentAnnotation
10941074
return stamps
10951075
}
10961076

1077+
private suspend fun registerStampAppearanceGenerators() {
1078+
if (stampAppearanceGeneratorsRegistered) return
1079+
val generators = withContext(Dispatchers.IO) {
1080+
mapOf(
1081+
blackStampSubject to AssetAppearanceStreamGenerator(blackStampFile),
1082+
blueStampSubject to AssetAppearanceStreamGenerator(blueStampFile),
1083+
brownStampSubject to AssetAppearanceStreamGenerator(brownStampFile),
1084+
greenStampSubject to AssetAppearanceStreamGenerator(greenStampFile),
1085+
navyStampSubject to AssetAppearanceStreamGenerator(navyStampFile),
1086+
orangeStampSubject to AssetAppearanceStreamGenerator(orangeStampFile),
1087+
pinkStampSubject to AssetAppearanceStreamGenerator(pinkStampFile),
1088+
purpleStampSubject to AssetAppearanceStreamGenerator(purpleStampFile),
1089+
redStampSubject to AssetAppearanceStreamGenerator(redStampFile),
1090+
yellowStampSubject to AssetAppearanceStreamGenerator(yellowStampFile)
1091+
)
1092+
}
1093+
val customStampAppearanceStreamGenerator = CustomStampAppearanceStreamGenerator()
1094+
pdfFragment?.document?.annotationProvider?.addAppearanceStreamGenerator(customStampAppearanceStreamGenerator)
1095+
generators.forEach { (subject, generator) ->
1096+
customStampAppearanceStreamGenerator.addAppearanceStreamGenerator(subject, generator)
1097+
}
1098+
stampAppearanceGeneratorsRegistered = true
1099+
}
1100+
10971101
//endregion
10981102

10991103
open fun configureEditMenuItemGrouping(toolbarMenuItems: MutableList<ContextualToolbarMenuItem>): MutableList<ContextualToolbarMenuItem> {

0 commit comments

Comments
 (0)