Skip to content

Commit

Permalink
Sync current page in reader. Closes #124
Browse files Browse the repository at this point in the history
Upping versionCode to 68
  • Loading branch information
Dima-Android committed May 29, 2024
1 parent b3fb421 commit 3f50597
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.zotero.android.database.requests

import io.realm.Realm
import io.realm.kotlin.createObject
import io.realm.kotlin.where
import org.zotero.android.database.DbRequest
import org.zotero.android.database.objects.RObjectChange
import org.zotero.android.database.objects.RPageIndex
import org.zotero.android.database.objects.RPageIndexChanges
import org.zotero.android.database.objects.UpdatableChangeType
import org.zotero.android.sync.LibraryIdentifier

class StorePageForItemDbRequest(
private val key: String,
private val libraryId: LibraryIdentifier,
private val page: String,
): DbRequest {
override val needsWrite: Boolean
get() = true

override fun process(database: Realm) {
val pageIndex: RPageIndex
val existing = database.where<RPageIndex>().key(this.key, this.libraryId).findFirst()
if (existing != null) {
if (existing.index == this.page) {
return
}
pageIndex = existing
} else {
pageIndex = database.createObject<RPageIndex>()
pageIndex.key = this.key
pageIndex.libraryId = this.libraryId
}

pageIndex.index = this.page
pageIndex.changes.add(RObjectChange.create(listOf(RPageIndexChanges.index)))
pageIndex.changeType = UpdatableChangeType.user.name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONObject
import org.zotero.android.ZoteroApplication
import org.zotero.android.api.network.CustomResult
import org.zotero.android.api.pojo.sync.KeyBaseKeyPair
import org.zotero.android.architecture.BaseViewModel2
Expand All @@ -84,6 +85,7 @@ import org.zotero.android.database.requests.EditTagsForItemDbRequest
import org.zotero.android.database.requests.MarkObjectsAsDeletedDbRequest
import org.zotero.android.database.requests.ReadAnnotationsDbRequest
import org.zotero.android.database.requests.ReadDocumentDataDbRequest
import org.zotero.android.database.requests.StorePageForItemDbRequest
import org.zotero.android.database.requests.key
import org.zotero.android.ktx.annotation
import org.zotero.android.ktx.baseColor
Expand Down Expand Up @@ -624,6 +626,8 @@ class PdfReaderViewModel @Inject constructor(
)
}

this.fragment.pageIndex = page

if (selectedData != null) {
val (key, location) = selectedData
updateState {
Expand Down Expand Up @@ -1591,6 +1595,7 @@ class PdfReaderViewModel @Inject constructor(
.forEach {
this.document.annotationProvider.removeAnnotationFromPage(it)
}
submitPendingPage(fragment.pageIndex)
super.onCleared()
}

Expand Down Expand Up @@ -2531,6 +2536,27 @@ class PdfReaderViewModel @Inject constructor(
}
}

private fun submitPendingPage(page: Int) {
store(page = page)
}

private fun store(page: Int) {
val request = StorePageForItemDbRequest(
key = viewState.key,
libraryId = viewState.library.identifier,
page = "$page"
)
ZoteroApplication.instance.applicationScope.launch {
perform(
dbWrapper = dbWrapper,
request = request
).ifFailure {
Timber.e(it, "PDFReaderViewModel: can't store page")
return@launch
}
}
}

}

data class PdfReaderViewState(
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 = 33

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

0 comments on commit 3f50597

Please sign in to comment.