Skip to content

Commit

Permalink
Potential fix for Incorrect toggling of full-screen mode when using a…
Browse files Browse the repository at this point in the history
…nnotation tools #178.

Upping versionCode to 113
  • Loading branch information
Dima-Android committed Oct 30, 2024
1 parent 22257f1 commit 6ceca1f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ import com.pspdfkit.annotations.configuration.MarkupAnnotationConfiguration
import com.pspdfkit.annotations.configuration.NoteAnnotationConfiguration
import com.pspdfkit.annotations.configuration.ShapeAnnotationConfiguration
import com.pspdfkit.configuration.activity.PdfActivityConfiguration
import com.pspdfkit.configuration.activity.UserInterfaceViewMode
import com.pspdfkit.configuration.page.PageFitMode
import com.pspdfkit.configuration.page.PageScrollMode
import com.pspdfkit.configuration.theming.ThemeMode
import com.pspdfkit.document.OutlineElement
import com.pspdfkit.document.PdfDocument
import com.pspdfkit.document.PdfDocumentLoader
import com.pspdfkit.listeners.DocumentListener
import com.pspdfkit.listeners.scrolling.DocumentScrollListener
import com.pspdfkit.listeners.scrolling.ScrollState
import com.pspdfkit.preferences.PSPDFKitPreferences
import com.pspdfkit.ui.PdfFragment
import com.pspdfkit.ui.PdfUiFragment
Expand Down Expand Up @@ -237,12 +240,7 @@ class PdfReaderViewModel @Inject constructor(
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onUserInterfaceVisibilityChanged(event: OnUserInterfaceVisibilityChangedEvent) {
toggleTopAndBottomBarVisibility(event.isVisible)
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onPdfPageScrolled(event: OnPdfPageScrolled) {
fun onPageChangedEvent(event: OnPageChangedEvent) {
triggerEffect(PdfReaderViewEffect.ScrollThumbnailListToIndex(event.pageIndex))
val row = viewState.thumbnailRows.firstOrNull { it.pageIndex == event.pageIndex }
updateState {
Expand Down Expand Up @@ -379,19 +377,13 @@ class PdfReaderViewModel @Inject constructor(
}

class CustomPdfUiFragment: PdfUiFragment() {
override fun onUserInterfaceVisibilityChanged(visible: Boolean) {
super.onUserInterfaceVisibilityChanged(visible)
EventBus.getDefault().post(OnUserInterfaceVisibilityChangedEvent(visible))
}

override fun onPageChanged(document: PdfDocument, pageIndex: Int) {
super.onPageChanged(document, pageIndex)
EventBus.getDefault().post(OnPdfPageScrolled(pageIndex))
EventBus.getDefault().post(OnPageChangedEvent(pageIndex))
}
}

data class OnUserInterfaceVisibilityChangedEvent(val isVisible: Boolean)
data class OnPdfPageScrolled(val pageIndex: Int)
data class OnPageChangedEvent(val pageIndex: Int)

override fun init(
uri: Uri,
Expand Down Expand Up @@ -440,6 +432,7 @@ class PdfReaderViewModel @Inject constructor(
addDocumentListenerOnInit()
addOnAnnotationCreationModeChangeListener()
setOnPreparePopupToolbarListener()
addDocumentScrollListener()
}

override fun onDestroy(owner: LifecycleOwner) {
Expand All @@ -452,6 +445,25 @@ class PdfReaderViewModel @Inject constructor(
}
}

private fun addDocumentScrollListener() {
pdfFragment.addDocumentScrollListener(object : DocumentScrollListener {
override fun onScrollStateChanged(state: ScrollState) {
if (state == ScrollState.DRAGGED) {
setBottomBarVisibility(false)
}
}

override fun onDocumentScrolled(p0: Int, p1: Int, p2: Int, p3: Int, p4: Int, p5: Int) {
//no-op
}
})

}

private fun setBottomBarVisibility(isVisible: Boolean) {
pdfUiFragment.setUserInterfaceVisible(isVisible, true)
}

private fun addDocumentListenerOnInit() {
this@PdfReaderViewModel.pdfFragment.addDocumentListener(object :
DocumentListener {
Expand All @@ -466,11 +478,50 @@ class PdfReaderViewModel @Inject constructor(
pagePosition: PointF?,
clickedAnnotation: Annotation?
): Boolean {
decideTopBarAndBottomBarVisibility(clickedAnnotation)
return false
}
})
}

private var lastSelectedAnnotation: Annotation? = null

private fun decideTopBarAndBottomBarVisibility(currentlySelectedAnnotation: Annotation?) {
val wasAnnotationClicked = currentlySelectedAnnotation != null
if (currentlySelectedAnnotation == null &&
(lastSelectedAnnotation?.type == AnnotationType.FREETEXT || lastSelectedAnnotation?.type == AnnotationType.NOTE)) {
lastSelectedAnnotation = null
return
}
lastSelectedAnnotation = currentlySelectedAnnotation
if (wasAnnotationClicked) {
return
}
val isBottomBarCurrentlyVisible = pdfUiFragment.isUserInterfaceVisible
val isTopBarCurrentlyVisible = viewState.isTopBarVisible

if (isTopBarCurrentlyVisible && isBottomBarCurrentlyVisible) {
setTopBarVisibility(false)
setBottomBarVisibility(false)
return
}
if (!isTopBarCurrentlyVisible && !isBottomBarCurrentlyVisible) {
setTopBarVisibility(true)
setBottomBarVisibility(true)
return
}

if (isBottomBarCurrentlyVisible && !isTopBarCurrentlyVisible) {
setTopBarVisibility(true)
return
}
if (!isBottomBarCurrentlyVisible && isTopBarCurrentlyVisible) {
setBottomBarVisibility(true)
return
}

}

private fun setOnPreparePopupToolbarListener() {
this.pdfFragment.setOnPreparePopupToolbarListener { toolbar ->
val sourceItems = toolbar.menuItems
Expand Down Expand Up @@ -841,7 +892,12 @@ class PdfReaderViewModel @Inject constructor(
}

override fun onAnnotationUpdated(annotation: Annotation) {
processAnnotationObserving(annotation, emptyList(), PdfReaderNotification.PSPDFAnnotationChanged)
processAnnotationObserving(
annotation = annotation,
changes = emptyList(),
pdfReaderNotification = PdfReaderNotification.PSPDFAnnotationChanged
)
lastSelectedAnnotation = annotation
}

override fun onAnnotationRemoved(annotation: Annotation) {
Expand Down Expand Up @@ -1959,7 +2015,8 @@ class PdfReaderViewModel @Inject constructor(
.disableDefaultToolbar()
.hideDocumentTitleOverlay()
.enableStylusOnDetection(true)
.hideUserInterfaceWhenCreatingAnnotations(true)
.hideUserInterfaceWhenCreatingAnnotations(false)
.setUserInterfaceViewMode(UserInterfaceViewMode.USER_INTERFACE_VIEW_MODE_MANUAL)
.build()
}

Expand Down Expand Up @@ -2237,6 +2294,7 @@ class PdfReaderViewModel @Inject constructor(
addDocumentListener2()
addOnAnnotationCreationModeChangeListener()
setOnPreparePopupToolbarListener()
addDocumentScrollListener()
// updateVisibilityOfAnnotations()

}
Expand Down Expand Up @@ -2275,6 +2333,7 @@ class PdfReaderViewModel @Inject constructor(
pagePosition: PointF?,
clickedAnnotation: Annotation?
): Boolean {
decideTopBarAndBottomBarVisibility(clickedAnnotation)
return false
}
})
Expand All @@ -2299,7 +2358,7 @@ class PdfReaderViewModel @Inject constructor(
})
}

private fun toggleTopAndBottomBarVisibility(isVisible: Boolean) {
private fun setTopBarVisibility(isVisible: Boolean) {
updateState {
copy(isTopBarVisible = isVisible)
}
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 = 112 // Must be updated on every build
val versionCode = 113 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 6ceca1f

Please sign in to comment.