1

I am using Kotlin Jetpack and getting below error while opening the document.

com.pspdfkit.jetpack.compose.NonFragmentActivityException: You need to use a fragment activity in order to use the DocumentView composable.
        at com.pspdfkit.jetpack.compose.DocumentViewKt.DocumentView(SourceFile:228)
        at com.harry.pennysaver.views.pdf.ReaderKt$PdfList$1$5.invoke(Reader.kt:142)
        at com.harry.pennysaver.views.pdf.ReaderKt$PdfList$1$5.invoke(Reader.kt:124)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.animation.AnimatedVisibilityKt.AnimatedEnterExitImpl(AnimatedVisibility.kt:935)
        at androidx.compose.animation.AnimatedVisibilityKt.AnimatedVisibility(AnimatedVisibility.kt:128)
        at com.harry.pennysaver.views.pdf.ReaderKt.PdfList(Reader.kt:120)
        at com.harry.pennysaver.views.home.HomeScreenKt.HomeScreen(HomeScreen.kt:47)
        at com.harry.pennysaver.views.home.HomeScreenKt$HomeScreen$2.invoke(Unknown Source:10)
        at com.harry.pennysaver.views.home.HomeScreenKt$HomeScreen$2.invoke(Unknown Source:10)
        at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:145)
        at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2375)
        at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2643)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3260)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3238)
        at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
        at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3238)
        at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3203)
        at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:771)
        at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1031)
        at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:125)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:534)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:503)
        at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
        at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:970)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:727)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.runtime.PausableMonotonicFrameClock@4ce413b, StandaloneCoroutine{Cancelling}@253f058, AndroidUiDispatcher@d0855b1]

My Pdf composable is as below

private const val thumbnailPageIndex = 0

    @SuppressLint("pspdfkit-experimental")
    @OptIn(ExperimentalFoundationApi::class, ExperimentalPSPDFKitApi::class)
    @Composable
    fun PdfList(
        pdfState: PdfState,
        loadPdfs: () -> Unit,
        openDocument: (Uri) -> Unit
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.fillMaxSize()
        ) {
            AnimatedVisibility(
                visible = pdfState.loading,
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(24.dp)
                ) {
                    Text(
                        text = "Loading....",
                        textAlign = TextAlign.Center
                    )
                    VerticalSpacer(height = 8.dp)
                    CircularProgressIndicator()
                }
    
            }
    
            AnimatedVisibility(
                visible = !pdfState.loading && pdfState.documents.isEmpty(),
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(24.dp)
                ) {
                    Text(
                        text = "No documents yet",
                        textAlign = TextAlign.Center
                    )
                    VerticalSpacer(height = 8.dp)
    
                    PrimaryButton(
                        label = "Load Documents",
                        modifier = Modifier
                            .height(44.dp)
                            .fillMaxSize()
                            .padding(horizontal = 55.dp)
                    ) {
                        loadPdfs()
                    }
                }
            }
    
            AnimatedVisibility(
                visible = !pdfState.loading && pdfState.documents.isNotEmpty(),
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                LazyVerticalGrid(
                    cells = GridCells.Adaptive(100.dp),
                    modifier = Modifier.fillMaxSize()
                ) {
                    items(pdfState.documents.toList()) {(uri, document) ->
                        PdfThumbnail(
                            document = document,
                            onClick = { openDocument(uri) }
                        )
                    }
                }
            }
    
            AnimatedVisibility(
                visible = pdfState.selectedDocumentUri != null,
                enter = slideInHorizontally{ it },
                exit = slideOutHorizontally{ it }
            ) {
    
                if (pdfState.selectedDocumentUri == null) {
                    Box(modifier = Modifier.fillMaxSize())
                } else {
                    val context = LocalContext.current
                    val pdfActivityConfiguration = remember {
                        PdfActivityConfiguration
                            .Builder(context)
                            .setUserInterfaceViewMode(UserInterfaceViewMode.USER_INTERFACE_VIEW_MODE_HIDDEN)
                            .build()
                    }
    
                    val documentState = rememberDocumentState(
                        pdfState.selectedDocumentUri,
                        pdfActivityConfiguration
                    )
    
                    DocumentView(
                        documentState = documentState,
                        modifier = Modifier.fillMaxSize()
                    )
                }
            }
        }
    }
    
    @Composable
    fun PdfThumbnail(
        document: PdfDocument,
        onClick: () -> Unit
    ) {
        val context = LocalContext.current
    
        val thumbnailImage = remember(document) {
            val pageImageSize = document.getPageSize(thumbnailPageIndex).toRect()
    
            document.renderPageToBitmap(
                context,
                thumbnailPageIndex,
                pageImageSize.width().toInt(),
                pageImageSize.height().toInt()
            ).asImageBitmap()
        }
    
        Card(
            elevation = 4.dp,
            modifier = Modifier
                .padding(8.dp)
                .clickable(
                    interactionSource = remember {
                        MutableInteractionSource()
                    },
                    indication = rememberRipple()
                ) {
                    onClick()
                }
        ) {
            Column {
                Image(
                    bitmap = thumbnailImage ,
                    contentDescription =  "Preview for document ${document.title}",
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .height(120.dp)
                        .fillMaxWidth()
                )
    
                VerticalSpacer(height = 8.dp)
    
                Text(
                    text = document.title ?: "Untitled Document",
                    modifier = Modifier.padding(12.dp),
                    style = TextStyle(
                        fontWeight = Typography.body1.fontWeight,
                        fontSize = Typography.body1.fontSize
                    )
                )
            }
        }
    }

Now this PdfList composable is used as below in my HomeScreen.kt composable.

@Composable
fun HomeScreen(
    viewModel: PdfViewModel? = hiltViewModel(),
    navController: NavController
) {
    val spacing = MaterialTheme.spacing
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .padding(spacing.medium)
            .padding(top = spacing.extraLarge),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        viewModel?.let {
            val state by it.state.collectAsState(PdfState())
            PdfList(pdfState = state, loadPdfs = viewModel::loadPdfs , openDocument = viewModel::openDocument)
        }

    }
}

What I am doing wrong? Why this error is there when I click on the card to open the document.

Harry
  • 154
  • 4
  • 15

1 Answers1

0

I wrote to support on this issue, they wrote Document View() relies on FragmentActivity, so this function cannot be run from the JC ComponentActivity, although it is marked as Composable.

The API is in test mode and will not work as it should soon.

PSPDFKIT doesn't work on JC right now

Here is the actual response from support: Response from support

helvete
  • 2,455
  • 13
  • 33
  • 37
LazyHat
  • 1
  • 1