mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
update reading progress as pdf pages change
This commit is contained in:
parent
30ae8ef8d4
commit
ba3852e6c3
4 changed files with 33 additions and 3 deletions
|
|
@ -19,8 +19,13 @@ data class ReadingProgressParams(
|
|||
)
|
||||
}
|
||||
|
||||
suspend fun Networker.updateReadingProgress(jsonString: String): Boolean {
|
||||
val input = Gson().fromJson(jsonString, ReadingProgressParams::class.java).asSaveReadingProgressInput()
|
||||
suspend fun Networker.updateWebReadingProgress(jsonString: String): Boolean {
|
||||
val params = Gson().fromJson(jsonString, ReadingProgressParams::class.java)
|
||||
return updateReadingProgress(params)
|
||||
}
|
||||
|
||||
suspend fun Networker.updateReadingProgress(params: ReadingProgressParams): Boolean {
|
||||
val input = params.asSaveReadingProgressInput()
|
||||
|
||||
Log.d("Loggo", "created reading progress input: $input")
|
||||
|
||||
|
|
|
|||
|
|
@ -260,6 +260,11 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
|
|||
return super.onPageClick(document, pageIndex, event, pagePosition, clickedAnnotation)
|
||||
}
|
||||
|
||||
override fun onPageChanged(document: PdfDocument, pageIndex: Int) {
|
||||
viewModel.syncPageChange(pageIndex, document.pageCount)
|
||||
super.onPageChanged(document, pageIndex)
|
||||
}
|
||||
|
||||
private fun showHighlightSelectionPopover(clickedAnnotation: Annotation) {
|
||||
// TODO: anchor popover at exact position of tap (maybe add an empty view at tap loc and anchor to that?)
|
||||
val popupMenu = PopupMenu(this, fragment.view, Gravity.CENTER, androidx.appcompat.R.attr.actionOverflowMenuStyle, 0)
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|||
import kotlinx.coroutines.launch
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.lang.Double.max
|
||||
import java.lang.Double.min
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
|
|
@ -38,6 +40,7 @@ class PDFReaderViewModel @Inject constructor(
|
|||
): ViewModel() {
|
||||
var annotationUnderNoteEdit: Annotation? = null
|
||||
val pdfReaderParamsLiveData = MutableLiveData<PDFReaderParams?>(null)
|
||||
private var currentReadingProgress = 0.0
|
||||
|
||||
fun loadItem(slug: String, context: Context) {
|
||||
viewModelScope.launch {
|
||||
|
|
@ -66,6 +69,7 @@ class PDFReaderViewModel @Inject constructor(
|
|||
labelsJSONString = Gson().toJson(articleQueryResult.labels)
|
||||
)
|
||||
|
||||
currentReadingProgress = article.readingProgress
|
||||
pdfReaderParamsLiveData.postValue(PDFReaderParams(article, articleContent, Uri.fromFile(output)))
|
||||
}
|
||||
|
||||
|
|
@ -80,6 +84,22 @@ class PDFReaderViewModel @Inject constructor(
|
|||
pdfReaderParamsLiveData.postValue(null)
|
||||
}
|
||||
|
||||
fun syncPageChange(currentPageIndex: Int, totalPages: Int) {
|
||||
val rawProgress = ((currentPageIndex + 1).toDouble() / totalPages.toDouble()) * 100
|
||||
val percent = min(100.0, max(0.0, rawProgress))
|
||||
if (percent > currentReadingProgress) {
|
||||
currentReadingProgress = percent
|
||||
viewModelScope.launch {
|
||||
val params = ReadingProgressParams(
|
||||
id = pdfReaderParamsLiveData.value?.item?.id,
|
||||
readingProgressPercent = percent,
|
||||
readingProgressAnchorIndex = currentPageIndex
|
||||
)
|
||||
networker.updateReadingProgress(params)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun syncHighlightUpdates(newAnnotation: Annotation, quote: String, overlapIds: List<String>) {
|
||||
val itemID = pdfReaderParamsLiveData.value?.item?.id ?: return
|
||||
val highlightID = UUID.randomUUID().toString()
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ class WebReaderViewModel @Inject constructor(
|
|||
}
|
||||
"articleReadingProgress" -> {
|
||||
viewModelScope.launch {
|
||||
val isReadingProgressSynced = networker.updateReadingProgress(jsonString)
|
||||
val isReadingProgressSynced = networker.updateWebReadingProgress(jsonString)
|
||||
Log.d("Network", "isReadingProgressSynced = $isReadingProgressSynced")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue