Skip to content

Commit

Permalink
Refactor PSPDFKit listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Oct 2, 2023
1 parent 72f37c8 commit b23a5fd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.readium.adapters.pspdfkit.document.PsPdfKitDocument
import org.readium.adapters.pspdfkit.document.PsPdfKitDocumentFactory
import org.readium.r2.navigator.input.InputListener
import org.readium.r2.navigator.input.TapEvent
import org.readium.r2.navigator.pdf.PdfDocumentFragment
import org.readium.r2.navigator.preferences.Axis
import org.readium.r2.navigator.preferences.Fit
Expand All @@ -61,24 +59,18 @@ import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.pdf.cachedIn

@ExperimentalReadiumApi
public class PsPdfKitDocumentFragment(
public class PsPdfKitDocumentFragment internal constructor(
private val publication: Publication,
private val href: Url,
initialPageIndex: Int,
initialSettings: PsPdfKitSettings,
private val listener: Listener?,
private val inputListener: InputListener
) : PdfDocumentFragment<PsPdfKitDocumentFragment.Listener, PsPdfKitSettings>() {

public interface Listener : PdfDocumentFragment.Listener {
/**
* Called when a PDF resource failed to be loaded, for example because of an
* [OutOfMemoryError].
*/
public fun onResourceLoadFailed(href: Url, error: Resource.Exception) {}

/** Called when configuring a new PDF fragment. */
public fun onConfigurePdfView(builder: PdfConfiguration.Builder): PdfConfiguration.Builder = builder
private val listener: Listener?
) : PdfDocumentFragment<PsPdfKitSettings>() {

internal interface Listener {
fun onResourceLoadFailed(href: Url, error: Resource.Exception)
fun onConfigurePdfView(builder: PdfConfiguration.Builder): PdfConfiguration.Builder
fun onTap(point: PointF): Boolean
}

private companion object {
Expand Down Expand Up @@ -248,7 +240,7 @@ public class PsPdfKitDocumentFragment(

override fun onDocumentClick(): Boolean {
val center = view?.run { PointF(width.toFloat() / 2, height.toFloat() / 2) }
return center?.let { inputListener.onTap(TapEvent(it)) } ?: false
return center?.let { listener?.onTap(it) } ?: false
}

override fun onPageClick(
Expand All @@ -266,7 +258,7 @@ public class PsPdfKitDocumentFragment(
}

checkNotNull(pdfFragment).viewProjection.toViewPoint(pagePosition, pageIndex)
return inputListener.onTap(TapEvent(pagePosition))
return listener?.onTap(pagePosition) ?: false
}

private val allowedTextSelectionItems = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

package org.readium.adapters.pspdfkit.navigator

import android.graphics.PointF
import com.pspdfkit.configuration.PdfConfiguration
import org.readium.r2.navigator.SimplePresentation
import org.readium.r2.navigator.VisualNavigator
import org.readium.r2.navigator.input.TapEvent
import org.readium.r2.navigator.pdf.PdfDocumentFragmentInput
import org.readium.r2.navigator.pdf.PdfEngineProvider
import org.readium.r2.navigator.preferences.Axis
Expand All @@ -16,6 +19,8 @@ import org.readium.r2.navigator.util.createFragmentFactory
import org.readium.r2.shared.ExperimentalReadiumApi
import org.readium.r2.shared.publication.Metadata
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.resource.Resource
import org.readium.r2.shared.util.Url

/**
* Main component to use the PDF navigator with PSPDFKit.
Expand All @@ -25,20 +30,36 @@ import org.readium.r2.shared.publication.Publication
*/
@ExperimentalReadiumApi
public class PsPdfKitEngineProvider(
private val defaults: PsPdfKitDefaults = PsPdfKitDefaults()
) : PdfEngineProvider<PsPdfKitDocumentFragment, PsPdfKitDocumentFragment.Listener, PsPdfKitSettings, PsPdfKitPreferences, PsPdfKitPreferencesEditor> {
private val defaults: PsPdfKitDefaults = PsPdfKitDefaults(),
private val listener: Listener? = null
) : PdfEngineProvider<PsPdfKitDocumentFragment, PsPdfKitSettings, PsPdfKitPreferences, PsPdfKitPreferencesEditor> {

public interface Listener : PdfEngineProvider.Listener {

/** Called when configuring a new PDF fragment. */
public fun onConfigurePdfView(builder: PdfConfiguration.Builder): PdfConfiguration.Builder = builder
}

override fun createDocumentFragmentFactory(
input: PdfDocumentFragmentInput<PsPdfKitDocumentFragment.Listener, PsPdfKitSettings>
input: PdfDocumentFragmentInput<PsPdfKitSettings>
): SingleFragmentFactory<PsPdfKitDocumentFragment> =
createFragmentFactory {
PsPdfKitDocumentFragment(
publication = input.publication,
href = input.href,
initialPageIndex = input.pageIndex,
initialSettings = input.settings,
listener = input.listener,
inputListener = input.inputListener
listener = object : PsPdfKitDocumentFragment.Listener {
override fun onResourceLoadFailed(href: Url, error: Resource.Exception) {
input.navigatorListener?.onResourceLoadFailed(href, error)
}

override fun onConfigurePdfView(builder: PdfConfiguration.Builder): PdfConfiguration.Builder =
listener?.onConfigurePdfView(builder) ?: builder

override fun onTap(point: PointF): Boolean =
input.inputListener?.onTap(TapEvent(point)) ?: false
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.readium.r2.navigator.pdf.PdfNavigatorFragment
import org.readium.r2.shared.ExperimentalReadiumApi

@ExperimentalReadiumApi
public typealias PsPdfKitNavigatorFragment = PdfNavigatorFragment<PsPdfKitDocumentFragment, PsPdfKitDocumentFragment.Listener, PsPdfKitSettings, PsPdfKitPreferences>
public typealias PsPdfKitNavigatorFragment = PdfNavigatorFragment<PsPdfKitDocumentFragment, PsPdfKitSettings, PsPdfKitPreferences>

@ExperimentalReadiumApi
public typealias PsPdfKitNavigatorFactory = PdfNavigatorFactory<PsPdfKitDocumentFragment, PsPdfKitDocumentFragment.Listener, PsPdfKitSettings, PsPdfKitPreferences, PsPdfKitPreferencesEditor>
public typealias PsPdfKitNavigatorFactory = PdfNavigatorFactory<PsPdfKitDocumentFragment, PsPdfKitSettings, PsPdfKitPreferences, PsPdfKitPreferencesEditor>

0 comments on commit b23a5fd

Please sign in to comment.