Skip to content

Commit

Permalink
Fix for PdfReaderViewModel.loadAnnotations ANR triggered by main thre…
Browse files Browse the repository at this point in the history
…ad waiting for too long

Upping versionCode to 125
  • Loading branch information
Dima-Android committed Dec 27, 2024
1 parent 00950b1 commit b45d6e7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class ReadAnnotationsDbRequest(
.deleted(false)
.`in`("annotationType", supportedTypes.toTypedArray())
.sort("annotationSortIndex", Sort.ASCENDING)
.findAll()
.findAllAsync()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import io.realm.RealmResults
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
Expand All @@ -70,6 +71,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.internal.toHexString
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
Expand Down Expand Up @@ -197,6 +199,7 @@ class PdfReaderViewModel @Inject constructor(
private val schemaController: SchemaController,
private val dateParser: DateParser,
private val navigationParamsMarshaller: NavigationParamsMarshaller,
private val dispatcher: CoroutineDispatcher,
stateHandle: SavedStateHandle,
) : BaseViewModel2<PdfReaderViewState, PdfReaderViewEffect>(PdfReaderViewState()), PdfReaderVMInterface {

Expand Down Expand Up @@ -473,7 +476,9 @@ class PdfReaderViewModel @Inject constructor(
this@PdfReaderViewModel.pdfFragment.addDocumentListener(object :
DocumentListener {
override fun onDocumentLoaded(document: PdfDocument) {
this@PdfReaderViewModel.onDocumentLoaded(document)
viewModelScope.launch {
this@PdfReaderViewModel.onDocumentLoaded(document)
}
}

override fun onDocumentClick(): Boolean {
Expand Down Expand Up @@ -584,7 +589,7 @@ class PdfReaderViewModel @Inject constructor(
}


private fun onDocumentLoaded(document: PdfDocument) {
private suspend fun onDocumentLoaded(document: PdfDocument) {
this.document = document
annotationBoundingBoxConverter = AnnotationBoundingBoxConverter(document)
loadRawDocument()
Expand Down Expand Up @@ -778,11 +783,11 @@ class PdfReaderViewModel @Inject constructor(
}
}

private fun loadAnnotations(
private suspend fun loadAnnotations(
document: PdfDocument,
username: String,
displayName: String
): Map<String, PDFDocumentAnnotation> {
): Map<String, PDFDocumentAnnotation> = withContext(dispatcher){
val annotations = mutableMapOf<String, PDFDocumentAnnotation>()
val pdfAnnotations = document.annotationProvider
.getAllAnnotationsOfTypeAsync(AnnotationsConfig.supported)
Expand All @@ -799,17 +804,15 @@ class PdfReaderViewModel @Inject constructor(
color = pdfAnnotation.color.toHexString(),
username = username,
displayName = displayName,
boundingBoxConverter = this.annotationBoundingBoxConverter
boundingBoxConverter = this@PdfReaderViewModel.annotationBoundingBoxConverter
) ?: continue

annotations[annotation.key] = annotation
}

return annotations

annotations
}

private fun loadDocumentData() {
private suspend fun loadDocumentData() {
val key = viewState.key
val library = viewState.library
val dbResult = loadAnnotationsAndPage(key = key, library = library)
Expand Down Expand Up @@ -2396,16 +2399,17 @@ class PdfReaderViewModel @Inject constructor(
private fun addDocumentListener2() {
this.pdfFragment.addDocumentListener(object : DocumentListener {
override fun onDocumentLoaded(document: PdfDocument) {
this@PdfReaderViewModel.onDocumentLoaded(document)

if (queuedUpPdfReaderColorPickerResult != null) {
setToolOptions(
hex = queuedUpPdfReaderColorPickerResult!!.colorHex,
size = queuedUpPdfReaderColorPickerResult!!.size,
tool = queuedUpPdfReaderColorPickerResult!!.annotationTool
)
queuedUpPdfReaderColorPickerResult = null

viewModelScope.launch {
this@PdfReaderViewModel.onDocumentLoaded(document)

if (queuedUpPdfReaderColorPickerResult != null) {
setToolOptions(
hex = queuedUpPdfReaderColorPickerResult!!.colorHex,
size = queuedUpPdfReaderColorPickerResult!!.size,
tool = queuedUpPdfReaderColorPickerResult!!.annotationTool
)
queuedUpPdfReaderColorPickerResult = null
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.pspdfkit.annotations.TextMarkupAnnotation
import com.pspdfkit.annotations.UnderlineAnnotation
import com.pspdfkit.document.PdfDocument
import io.realm.RealmResults
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
import org.zotero.android.database.objects.AnnotationType
import org.zotero.android.database.objects.AnnotationsConfig
Expand All @@ -39,7 +41,7 @@ class AnnotationConverter {

companion object {

fun annotations(
suspend fun annotations(
items: RealmResults<RItem>,
type: Kind = zotero,
currentUserId: Long,
Expand All @@ -48,8 +50,8 @@ class AnnotationConverter {
username: String,
boundingBoxConverter: AnnotationBoundingBoxConverter,
isDarkMode: Boolean,
): List<Annotation> {
return items.mapNotNull { item ->
): List<Annotation> = withContext(Dispatchers.IO) {
items.mapNotNull { item ->
val annotation = PDFDatabaseAnnotation.init(item) ?: return@mapNotNull null
annotation(
zoteroAnnotation = annotation,
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 = 124 // Must be updated on every build
val versionCode = 125 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit b45d6e7

Please sign in to comment.