From 30eef01a1f53835a4ab929bf5741b9b17280b826 Mon Sep 17 00:00:00 2001 From: Dima Date: Thu, 8 Feb 2024 19:53:13 +0900 Subject: [PATCH] =?UTF-8?q?Missing=20page=20scrubber=20in=20PDF=20reader.?= =?UTF-8?q?=20Closes=20#113=20Fixes=20=E2=80=9CDarker=20gray=20background?= =?UTF-8?q?=20around=20PDF=20in=20dark=20mode=E2=80=9D.=20Closes=20#59?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two Important nuances regarding PSPDFKIT: Nuance #1. Adjusting design of PSPDFKIT: There are 2 ways to change colors of components in the PSPDFKIT - either dynamically at runtime via calls like “pdfThumbnailBar.thumbnailBorderColor” or statically via a “style” property in our app’s style library. As I’ve discovered for some components color cannot be changed at all programmatically, even though the corresponding API is present (like in case of thumbnailBorderColor). In order not to waste time in the future I would highly advise first trying to change the UI of PSPDFKIT via styles, which seem consistent. Nuance #2. Scrollbars. As it turns out the “scrollbarsEnabled(true)” doesn’t really do much when passed via Configuration object to the PdfFragment. It probably works fine when dealing with PdfActivity, but that’s not our case. Changing properties of parent view to account for the possibility of having a scrollable view child inside of it didn’t help. So the problem is not that the scrollbars are present, but slightly out of frame, nor is it because they are of transparent color. As it turns out, we have to specifically declare PSPDFKIT style in our styles library and either completely setup all properties for the scrollbars ourselves, or point out to the PSPDFKIT’s own parent style. You would expect the PSPDFKIT kit to use some sensible defaults for the case where no style is specified, but for scrollbars this wasn’t the case. Upping versionCode 49 --- .../pdf/reader/PdfReaderPspdfKitView.kt | 20 ++++++++++- .../android/pdf/reader/PdfReaderViewModel.kt | 34 ++++++++++++++++++- app/src/main/res/values-night-v23/styles.xml | 6 ++++ app/src/main/res/values-night/colors.xml | 2 ++ app/src/main/res/values/colors.xml | 2 ++ app/src/main/res/values/styles.xml | 11 ++++++ buildSrc/src/main/kotlin/BuildConfig.kt | 2 +- 7 files changed, 74 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderPspdfKitView.kt b/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderPspdfKitView.kt index bd479302..e34ea966 100644 --- a/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderPspdfKitView.kt +++ b/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderPspdfKitView.kt @@ -3,6 +3,9 @@ package org.zotero.android.pdf.reader import android.content.res.Resources import android.net.Uri import android.util.TypedValue +import android.view.Gravity +import android.view.ViewGroup +import android.widget.FrameLayout import androidx.appcompat.app.AppCompatActivity import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable @@ -10,6 +13,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.viewinterop.AndroidView import androidx.fragment.app.FragmentContainerView +import com.pspdfkit.ui.PdfThumbnailBar import org.zotero.android.R import org.zotero.android.architecture.ui.CustomLayoutSize @@ -22,18 +26,32 @@ fun PdfReaderPspdfKitView(uri: Uri, viewModel: PdfReaderViewModel) { AndroidView( modifier = Modifier.fillMaxSize(), factory = { context -> + val frameLayout = FrameLayout(context) + val containerId = R.id.container val fragmentContainerView = FragmentContainerView(context).apply { id = containerId } + frameLayout.addView(fragmentContainerView) + + val pdfThumbnailBar = PdfThumbnailBar(context) + val thumbnailBarLayoutParams = FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + thumbnailBarLayoutParams.gravity = Gravity.BOTTOM + pdfThumbnailBar.layoutParams = thumbnailBarLayoutParams + frameLayout.addView(pdfThumbnailBar) + viewModel.init( isTablet = layoutType.isTablet(), uri = uri, containerId = fragmentContainerView.id, fragmentManager = fragmentManager, + pdfThumbnailBar = pdfThumbnailBar, annotationMaxSideSize = annotationMaxSideSize ) - fragmentContainerView + frameLayout }, update = { _ -> } diff --git a/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderViewModel.kt b/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderViewModel.kt index bc906fb3..6c6e9a25 100644 --- a/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderViewModel.kt +++ b/app/src/main/java/org/zotero/android/pdf/reader/PdfReaderViewModel.kt @@ -5,6 +5,8 @@ import android.graphics.PointF import android.graphics.RectF import android.net.Uri import android.os.Handler +import android.view.MotionEvent +import androidx.core.view.isVisible import androidx.fragment.app.FragmentManager import androidx.fragment.app.commit import androidx.lifecycle.viewModelScope @@ -31,6 +33,7 @@ import com.pspdfkit.document.PdfDocumentLoader import com.pspdfkit.listeners.DocumentListener import com.pspdfkit.preferences.PSPDFKitPreferences import com.pspdfkit.ui.PdfFragment +import com.pspdfkit.ui.PdfThumbnailBar import com.pspdfkit.ui.special_mode.controller.AnnotationCreationController import com.pspdfkit.ui.special_mode.controller.AnnotationSelectionController import com.pspdfkit.ui.special_mode.controller.AnnotationTool @@ -155,6 +158,7 @@ class PdfReaderViewModel @Inject constructor( private val onSearchStateFlow = MutableStateFlow("") private val onCommentChangeFlow = MutableStateFlow?>(null) private lateinit var fragmentManager: FragmentManager + private lateinit var pdfThumbnailBar: PdfThumbnailBar private var isTablet: Boolean = false private val handler = Handler(context.mainLooper) @@ -284,9 +288,11 @@ class PdfReaderViewModel @Inject constructor( containerId: Int, fragmentManager: FragmentManager, isTablet: Boolean, + pdfThumbnailBar: PdfThumbnailBar, ) { this.isTablet = isTablet this.fragmentManager = fragmentManager + this.pdfThumbnailBar = pdfThumbnailBar this.containerId = containerId this.annotationMaxSideSize = annotationMaxSideSize @@ -311,7 +317,20 @@ class PdfReaderViewModel @Inject constructor( override fun onDocumentLoaded(document: PdfDocument) { this@PdfReaderViewModel.onDocumentLoaded(document) } + + override fun onPageClick( + document: PdfDocument, + pageIndex: Int, + event: MotionEvent?, + pagePosition: PointF?, + clickedAnnotation: Annotation? + ): Boolean { + pdfThumbnailBar.isVisible = !pdfThumbnailBar.isVisible + return false + } }) + + this@PdfReaderViewModel.fragment.addDocumentListener(pdfThumbnailBar.documentListener) this.fragment.addOnAnnotationCreationModeChangeListener(object: AnnotationManager.OnAnnotationCreationModeChangeListener { override fun onEnterAnnotationCreationMode(p0: AnnotationCreationController) { @@ -420,6 +439,8 @@ class PdfReaderViewModel @Inject constructor( fragment.addOnAnnotationDeselectedListener { annotation, _ -> deselectSelectedAnnotation(annotation) } + pdfThumbnailBar.setOnPageChangedListener { _, pageIndex: Int -> fragment.pageIndex = pageIndex } + pdfThumbnailBar.setDocument(document, fragment.configuration) } private fun initState() { @@ -1559,7 +1580,6 @@ class PdfReaderViewModel @Inject constructor( } private fun generatePdfConfiguration(pdfSettings: PDFSettings): PdfConfiguration { - if (!PSPDFKitPreferences.get(context).isAnnotationCreatorSet) { PSPDFKitPreferences.get(context).setAnnotationCreator(viewState.displayName) } @@ -1598,6 +1618,7 @@ class PdfReaderViewModel @Inject constructor( // .disableAnnotationRotation() // .setSelectedAnnotationResizeEnabled(false) .autosaveEnabled(false) + .scrollbarsEnabled(true) .build() } @@ -1873,7 +1894,18 @@ class PdfReaderViewModel @Inject constructor( } } + override fun onPageClick( + document: PdfDocument, + pageIndex: Int, + event: MotionEvent?, + pagePosition: PointF?, + clickedAnnotation: Annotation? + ): Boolean { + pdfThumbnailBar.isVisible = !pdfThumbnailBar.isVisible + return false + } }) + this@PdfReaderViewModel.fragment.addDocumentListener(pdfThumbnailBar.documentListener) this.fragment.addOnAnnotationCreationModeChangeListener(object: AnnotationManager.OnAnnotationCreationModeChangeListener { override fun onEnterAnnotationCreationMode(p0: AnnotationCreationController) { diff --git a/app/src/main/res/values-night-v23/styles.xml b/app/src/main/res/values-night-v23/styles.xml index 63fb1e34..ff4a2c9a 100644 --- a/app/src/main/res/values-night-v23/styles.xml +++ b/app/src/main/res/values-night-v23/styles.xml @@ -11,5 +11,11 @@ @color/checkbox_dark @color/white false + + @style/CustomPspdfDocumentViewStyleDark + @style/CustomPspdfThumbnailBarStyle + @color/pdf_view_background_color + + diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index 5fbd6281..44e97596 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -29,4 +29,6 @@ #282828 #FF4072E5 #FF335BB8 + #FF2D2D2D + #FF1C1C1E diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 19e12644..3e21f0d2 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -62,4 +62,6 @@ #E4E4E7 #77767B #FAFAFD + #FFE8E8ED + #FFF2F2F7 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 50a85ccf..43b0208a 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -33,6 +33,17 @@ + + + +