Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split PdfRendererViewCompose into overloaded functions for varied sources #132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
```
Expand All @@ -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
)
```
Expand All @@ -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
)
```
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
)
}