From b4130530e0638037657c6423f2a9d396ceb20fd2 Mon Sep 17 00:00:00 2001 From: Deddy Rumapea Date: Sat, 18 Jan 2025 13:08:16 +0700 Subject: [PATCH] refactor: split PdfRendererViewCompose into overloaded functions for varied sources (#132) * refactor: split into overloaded functions for varied sources * fix: update Jetpack Compose section * fix: update saveTo, it's no longer under PdfViewerActivity --- README.md | 44 ++++++++++-- .../rajat/sample/pdfviewer/ComposeActivity.kt | 5 -- .../pdfviewer/compose/PdfRendererCompose.kt | 71 +++++++++++++------ 3 files changed, 89 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 9dff033..3381086 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ PdfViewerActivity.launchPdfFromUrl( context = this, pdfUrl = "your_pdf_url_here", pdfTitle = "PDF Title", - saveTo = PdfViewerActivity.saveTo.ASK_EVERYTIME, + saveTo = saveTo.ASK_EVERYTIME, enableDownload = true ) ``` @@ -108,7 +108,7 @@ PdfViewerActivity.launchPdfFromPath( context = this, path = "your_file_path_or_uri_here", pdfTitle = "Title", - saveTo = PdfViewerActivity.saveTo.ASK_EVERYTIME, + saveTo = saveTo.ASK_EVERYTIME, fromAssets = false ) ``` @@ -126,7 +126,7 @@ PdfViewerActivity.launchPdfFromPath( context = this, path = "file_name_in_assets", pdfTitle = "Title", - saveTo = PdfViewerActivity.saveTo.ASK_EVERYTIME, + saveTo = saveTo.ASK_EVERYTIME, fromAssets = true ) ``` @@ -155,16 +155,48 @@ binding.pdfView.initWithUrl( ``` #### Using with Jetpack Compose -For Jetpack Compose, utilize PdfRendererViewCompose: + +For Jetpack Compose, utilize `PdfRendererViewCompose` to render PDF files. + +To render a PDF from a URL: + +```kotlin +PdfRendererViewCompose( + url = "your_pdf_url_here", +) +``` + +To render a PDF from a local file: + +```kotlin +PdfRendererViewCompose( + file = yourFile, +) +``` + +To render a PDF from a URI: + +```kotlin +PdfRendererViewCompose( + uri = yourUri, +) +``` + +You can also provide arguments for additional parameters such as `modifier`, `headers`, `lifecycleOwner`, and `statusCallBack`: ```kotlin PdfRendererViewCompose( url = "your_pdf_url_here", - lifecycleOwner = LocalLifecycleOwner.current + modifier = Modifier, + headers = HeaderData(mapOf("Authorization" to "123456789")), + lifecycleOwner = LocalLifecycleOwner.current, + statusCallBack = object : PdfRendererView.StatusCallBack { + // Override functions here + }, ) ``` -That's pretty much it and you're all wrapped up. +That's all you need to integrate PDF rendering in your Compose application. ### Ui Customizations You need to add the custom theme to styles.xml/themes.xml file and override the required attribute values. diff --git a/app/src/main/java/com/rajat/sample/pdfviewer/ComposeActivity.kt b/app/src/main/java/com/rajat/sample/pdfviewer/ComposeActivity.kt index 1b7d954..8c0452d 100644 --- a/app/src/main/java/com/rajat/sample/pdfviewer/ComposeActivity.kt +++ b/app/src/main/java/com/rajat/sample/pdfviewer/ComposeActivity.kt @@ -19,7 +19,6 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalLifecycleOwner @@ -100,11 +99,9 @@ fun MyPdfScreenFromUri(modifier: Modifier = Modifier) { @Composable fun MyPdfScreenFromUri(uri: Uri, modifier: Modifier = Modifier) { - val lifecycleOwner = LocalLifecycleOwner.current PdfRendererViewCompose( modifier = modifier, uri = uri, - lifecycleOwner = lifecycleOwner, statusCallBack = object : PdfRendererView.StatusCallBack { override fun onPdfLoadStart() { Log.i("statusCallBack", "onPdfLoadStart") @@ -171,11 +168,9 @@ fun MyPdfScreenFromUrl(url: String, modifier: Modifier = Modifier) { @Composable fun MyPdfScreenFromFile() { - val lifecycleOwner = LocalLifecycleOwner.current val pdfFile = File("path/to/your/file.pdf") // Replace with your file path PdfRendererViewCompose( file = pdfFile, - lifecycleOwner = lifecycleOwner ) } diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/compose/PdfRendererCompose.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/compose/PdfRendererCompose.kt index b70788d..0139362 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/compose/PdfRendererCompose.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/compose/PdfRendererCompose.kt @@ -1,5 +1,6 @@ package com.rajat.pdfviewer.compose +import android.content.Context import android.net.Uri import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier @@ -13,34 +14,64 @@ import java.io.File @Composable fun PdfRendererViewCompose( + url: String, modifier: Modifier = Modifier, - url: String? = null, - file: File? = null, - uri: Uri? = null, headers: HeaderData = HeaderData(), lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, - statusCallBack: PdfRendererView.StatusCallBack? = null + statusCallBack: PdfRendererView.StatusCallBack? = null, ) { - val lifecycleScope = lifecycleOwner.lifecycleScope - AndroidView( - factory = { context -> - PdfRendererView(context).apply { - if (statusCallBack != null) { - statusListener = statusCallBack - } - if (file != null) { - initWithFile(file) - } else if (url != null) { - initWithUrl(url, headers, lifecycleScope, lifecycleOwner.lifecycle) - } else if (uri != null) { - initWithUri(uri) - } + factory = { context: Context -> PdfRendererView(context) }, + update = { pdfRendererView: PdfRendererView -> + if (statusCallBack != null) { + pdfRendererView.statusListener = statusCallBack } + + pdfRendererView.initWithUrl( + url = url, + headers = headers, + lifecycleCoroutineScope = lifecycleOwner.lifecycleScope, + lifecycle = lifecycleOwner.lifecycle, + ) }, - update = { view -> - // Update logic if needed + modifier = modifier, + ) +} + +@Composable +fun PdfRendererViewCompose( + file: File, + modifier: Modifier = Modifier, + statusCallBack: PdfRendererView.StatusCallBack? = null, +) { + AndroidView( + factory = { context -> PdfRendererView(context) }, + update = { pdfRendererView: PdfRendererView -> + if (statusCallBack != null) { + pdfRendererView.statusListener = statusCallBack + } + + pdfRendererView.initWithFile(file = file) }, modifier = modifier ) } + +@Composable +fun PdfRendererViewCompose( + uri: Uri, + modifier: Modifier = Modifier, + statusCallBack: PdfRendererView.StatusCallBack? = null, +) { + AndroidView( + factory = { context -> PdfRendererView(context) }, + update = { pdfRendererView: PdfRendererView -> + if (statusCallBack != null) { + pdfRendererView.statusListener = statusCallBack + } + + pdfRendererView.initWithUri(uri = uri) + }, + modifier = modifier, + ) +}