Skip to content

Commit

Permalink
Changing implementation of the fix for “width and height must be > 0”…
Browse files Browse the repository at this point in the history
… bug. Also adding INK annotation type to the list of annotations we check for zero dimensions. #206

Upping versionCode to 128
  • Loading branch information
Dima-Android committed Jan 6, 2025
1 parent b35e0eb commit 6717205
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 74 deletions.
10 changes: 0 additions & 10 deletions app/src/main/java/org/zotero/android/architecture/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ open class Defaults @Inject constructor(
private val webDavScheme = "webDavScheme"
private val webDavPassword = "webDavPassword"

private val needsZeroWidthOrHeightAnnotationsFix = "needsZeroWidthOrHeightAnnotationsFix2"

private val sharedPreferences: SharedPreferences by lazy {
context.getSharedPreferences(
sharedPrefsFile,
Expand Down Expand Up @@ -397,14 +395,6 @@ open class Defaults @Inject constructor(
return sharedPreferences.getInt(performFullSyncGuardKey, 1)
}

fun needsZeroWidthOrHeightAnnotationsFix(): Boolean {
return sharedPreferences.getBoolean(needsZeroWidthOrHeightAnnotationsFix, true)
}

fun setNeedsZeroWidthOrHeightAnnotationsFix(value: Boolean) {
sharedPreferences.edit { putBoolean(needsZeroWidthOrHeightAnnotationsFix, value) }
}

fun reset() {
setUsername("")
setDisplayName("")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,28 @@ class PdfReaderViewModel @Inject constructor(

update(
document = this.document,
zoteroAnnotations = dbToPdfAnnotations,
zoteroAnnotations = dbToPdfAnnotations.map { it.third },
key = key,
libraryId = library.identifier,
isDark = viewState.isDark
)
for (annotation in dbToPdfAnnotations) {

val cleanedDbToPdfAnnotations = dbToPdfAnnotations.mapNotNull { triple ->
val libraryId = triple.first
val rItemKey = triple.second
val annotation = triple.third
if (wasAnnotationsWithZeroSizeRemoved(
libraryId = libraryId,
rItemKey = rItemKey,
annotation = annotation
)
) {
return@mapNotNull null
}
annotation
}

for (annotation in cleanedDbToPdfAnnotations) {
annotationPreviewManager.store(
rawDocument = this.rawDocument,
annotation = annotation,
Expand Down Expand Up @@ -916,12 +932,8 @@ class PdfReaderViewModel @Inject constructor(
onAnnotationUpdatedListener = object :
AnnotationProvider.OnAnnotationUpdatedListener {
override fun onAnnotationCreated(annotation: Annotation) {
//Don't allow Square Annotations with zero width or height from being added.
val annotationRect = annotation.boundingBox
val width = (annotationRect.right - annotationRect.left).toInt()
val height = (annotationRect.top - annotationRect.bottom).toInt()
if (annotation.type == AnnotationType.SQUARE && (width == 0 || height == 0)) {
Timber.w("PdfReaderViewModel: Prevented an annotation of type ${annotation.type} from being created with width=$width and height=$height")
if (isAnnotationZeroSize(annotation)) {
Timber.w("PdfReaderViewModel: Prevented an annotation of type ${annotation.type} from being created due to zero dimensions")
this@PdfReaderViewModel.document.annotationProvider.removeAnnotationFromPage(
annotation
)
Expand Down Expand Up @@ -955,6 +967,42 @@ class PdfReaderViewModel @Inject constructor(
pdfFragment.addOnAnnotationUpdatedListener(onAnnotationUpdatedListener!!)
}

private fun wasAnnotationsWithZeroSizeRemoved(
libraryId: LibraryIdentifier,
rItemKey: String,
annotation: Annotation
): Boolean {
if (isAnnotationZeroSize(annotation)) {
this@PdfReaderViewModel.document.annotationProvider.removeAnnotationFromPage(
annotation
)
dbWrapperMain.realmDbStorage.perform(
MarkObjectsAsDeletedDbRequest(
clazz = RItem::class,
keys = listOf(rItemKey),
libraryId = libraryId
)
)
return true
}
return false
}

private fun isAnnotationZeroSize(annotation: Annotation): Boolean {
val annotationRect = annotation.boundingBox
val width = (annotationRect.right - annotationRect.left).toInt()
val height = (annotationRect.top - annotationRect.bottom).toInt()
if (listOf(
AnnotationType.SQUARE,
AnnotationType.INK
).contains(annotation.type) && (width == 0 || height == 0)
) {
Timber.w("PdfReaderViewModel: Found an annotation of type ${annotation.type} having zero dimensions width=$width and height=$height")
return true
}
return false
}

private fun change(annotation: Annotation, changes: List<String>) {
if (changes.isEmpty()) {
return
Expand Down
25 changes: 15 additions & 10 deletions app/src/main/java/org/zotero/android/sync/AnnotationConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@ class AnnotationConverter {
username: String,
boundingBoxConverter: AnnotationBoundingBoxConverter,
isDarkMode: Boolean,
): List<Annotation> = withContext(Dispatchers.IO) {
): List<Triple<LibraryIdentifier, String, Annotation>> = withContext(Dispatchers.IO) {
items.mapNotNull { item ->
val annotation = PDFDatabaseAnnotation.init(item) ?: return@mapNotNull null
annotation(
zoteroAnnotation = annotation,
type = type,
currentUserId = currentUserId,
library = library,
displayName = displayName,
username = username,
boundingBoxConverter = boundingBoxConverter,
isDarkMode = isDarkMode
val libraryId = item.libraryId ?: return@mapNotNull null
Triple(
libraryId,
item.key,
annotation(
zoteroAnnotation = annotation,
type = type,
currentUserId = currentUserId,
library = library,
displayName = displayName,
username = username,
boundingBoxConverter = boundingBoxConverter,
isDarkMode = isDarkMode
)
)
}
}
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/org/zotero/android/sync/UserControllers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.zotero.android.architecture.coroutines.Dispatchers
import org.zotero.android.attachmentdownloader.AttachmentDownloader
import org.zotero.android.database.DbWrapperMain
import org.zotero.android.database.requests.CleanupUnusedTags
import org.zotero.android.database.requests.FixSquareAnnotationsWithZeroWidthOrHeightDbRequest
import org.zotero.android.files.FileStore
import org.zotero.android.websocket.ChangeWsResponse
import timber.log.Timber
Expand Down Expand Up @@ -58,14 +57,6 @@ class UserControllers @Inject constructor(
dbWrapperMain.realmDbStorage.perform(coordinatorAction = { coordinator ->
isFirstLaunch = coordinator.perform(InitializeCustomLibrariesDbRequest())
coordinator.perform(CleanupUnusedTags())

if (defaults.needsZeroWidthOrHeightAnnotationsFix()) {
fileStore.annotationPreviews.deleteRecursively()
fileStore.pageThumbnails.deleteRecursively()
coordinator.perform(FixSquareAnnotationsWithZeroWidthOrHeightDbRequest())
defaults.setNeedsZeroWidthOrHeightAnnotationsFix(false)
}

coordinator.invalidate()
})
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 34

val versionCode = 127 // Must be updated on every build
val versionCode = 128 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 6717205

Please sign in to comment.