Skip to content

Commit

Permalink
Update example to PSPDFKit for Android 2024.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
PSPDFKit authored and PSPDFKit committed Sep 4, 2024
0 parents commit 1be5177
Show file tree
Hide file tree
Showing 69 changed files with 2,902 additions and 0 deletions.
42 changes: 42 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
The PSPDFKit Sample applications are licensed with a modified BSD
license. In plain language: you're allowed to do whatever you wish
with the code, modify, redistribute, embed in your products (free or
commercial), but you must include copyright, terms of usage and
disclaimer as stated in the license.

You will require a commercial PSPDFKit License to run these examples
in non-demo mode. Please refer to sales@pspdfkit.com for details.

Copyright © 2016-present PSPDFKit GmbH.
All rights reserved.

Redistribution and use in source or binary forms,
with or without modification, are permitted provided
that the following conditions are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

- Redistributions of PSPDFKit Samples must include attribution to
PSPDFKit, either in documentation or other appropriate media.

- Neither the name of the PSPDFKit, PSPDFKit GmbH, nor its developers
may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# PSPDFKit for Android Example - Jetpack Compose PDF Viewer

An example app demonstrating [PSPDFKit for Android](https://pspdfkit.com/pdf-sdk/android/)'s composable API in the `com.pspdfkit.jetpack.compose` package.

## Prerequisites

- The latest stable Android Studio version available [here](https://developer.android.com/studio).

## Getting Started

Clone and check out this example app repository on your local machine:

```sh
git clone https://github.com/PSPDFKit/pspdfkit-jetpack-compose-pdf-viewer.git
cd pspdfkit-jetpack-compose-pdf-viewer
```

You can now open the project inside Android Studio, or build and install the app directly from the command line:

```sh
./gradlew :app:installDebug
```

## License

This software is licensed under a [modified BSD license](LICENSE).

## Additional Resources

* PSPDFKit for Android online guides: https://pspdfkit.com/guides/android/current/
* PSPDFKit API reference:
* KDoc: https://pspdfkit.com/api/android/kdoc/
* Javadoc: https://pspdfkit.com/api/android/javadoc/
* PSPDFKit technical customer support: https://pspdfkit.com/support/request/
75 changes: 75 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright © 2019-2024 PSPDFKit GmbH. All rights reserved.
*
* The PSPDFKit Sample applications are licensed with a modified BSD license.
* Please see License for details. This notice may not be removed from this file.
*/

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp")
}

android {
namespace = "com.pspdfkit.example.compose"
compileSdk = 35

defaultConfig {
applicationId = namespace
minSdk = 21
targetSdk = compileSdk
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}
lint {
warningsAsErrors = true
disable.add("ObsoleteLintCustomCheck")
// "GradleDependency" needs to be on a separate line because of gradle_lint.sh CI script
disable.add("GradleDependency")
}
}

dependencies {

// PSPDFKit is integrated from the PSPDFKit Maven repository. See the `repositories` block at the beginning
// of this file, which shows how to set up the repository in your app.
implementation("com.pspdfkit:pspdfkit:2024.5.1")


implementation("androidx.datastore:datastore-preferences:1.1.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.3")
implementation("androidx.navigation:navigation-compose:2.7.7")
implementation("io.insert-koin:koin-androidx-compose:3.5.0")
val roomVersion = "2.6.1"
implementation("androidx.room:room-runtime:$roomVersion")
ksp("androidx.room:room-compiler:$roomVersion")
// Optional - Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:$roomVersion")
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.activity:activity-compose:1.9.0")
implementation(platform("androidx.compose:compose-bom:2024.06.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material:material-icons-extended")
}
28 changes: 28 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Compose"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.Compose">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Binary file added app/src/main/assets/demo.pdf
Binary file not shown.
26 changes: 26 additions & 0 deletions app/src/main/java/com/pspdfkit/example/compose/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2023-2024 PSPDFKit GmbH. All rights reserved.
*
* The PSPDFKit Sample applications are licensed with a modified BSD license.
* Please see License for details. This notice may not be removed from this file.
*/

package com.pspdfkit.example.compose

import android.app.Application
import com.pspdfkit.example.compose.di.database
import com.pspdfkit.example.compose.di.repository
import com.pspdfkit.example.compose.di.viewModel
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

class App() : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
// inject Android context
androidContext(applicationContext)
koin.loadModules(listOf(viewModel, database, repository))
}
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/pspdfkit/example/compose/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.pspdfkit.example.compose

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import com.pspdfkit.example.compose.ui.components.Root

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { Root() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright © 2023-2024 PSPDFKit GmbH. All rights reserved.
*
* The PSPDFKit Sample applications are licensed with a modified BSD license.
* Please see License for details. This notice may not be removed from this file.
*/

package com.pspdfkit.example.compose.data.local

import androidx.room.Dao
import androidx.room.Database
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.RoomDatabase
import com.pspdfkit.example.compose.models.HistoryTable
import kotlinx.coroutines.flow.Flow

/** Database class required to hold pdf access history */
@Database(version = 1, entities = [HistoryTable::class])
abstract class AppDatabase : RoomDatabase() {
abstract fun historyDao(): HistoryDao
}

/** Dao class for [HistoryTable]*/
@Dao
interface HistoryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(historyTable: HistoryTable)

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(historyList: List<HistoryTable>)

@Query("SELECT * FROM history_table")
fun getAllHistory(): Flow<List<HistoryTable>>

@Query("SELECT * FROM history_table where id = :id")
fun getHistoryById(id: String): Flow<List<HistoryTable>>

@Query("DELETE FROM history_table")
suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © 2023-2024 PSPDFKit GmbH. All rights reserved.
*
* The PSPDFKit Sample applications are licensed with a modified BSD license.
* Please see License for details. This notice may not be removed from this file.
*/

package com.pspdfkit.example.compose.data.repository

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import androidx.collection.LruCache
import androidx.compose.ui.unit.dp
import com.pspdfkit.document.PdfDocumentLoader
import com.pspdfkit.example.compose.utils.calculateBitmapSize
import com.pspdfkit.utils.Size
import java.io.File
/** Repository to hold all the tasks related to bitmap processing */
class BitmapRepository(private val context: Context) {

/** [previewImageCache] acts as a local cache for bitmap */
private val previewImageCache: LruCache<String, Bitmap> =
object : LruCache<String, Bitmap>((Runtime.getRuntime().maxMemory() / 1024 / 8).toInt()) {
override fun sizeOf(key: String, value: Bitmap): Int {
// The cache size will be measured in kilobytes rather than number of items.
return value.byteCount / 1024
}
}

/** Used to retrieve bitmap from the help of [PdfDocumentLoader] */
fun getBitmap(file: File): Bitmap {
val id = file.path
return previewImageCache[id] ?: try {
val doc = PdfDocumentLoader.openDocument(context, Uri.fromFile(file))
val size = doc.calculateBitmapSize(Size(200.dp.value, 350.dp.value))
previewImageCache.put(id, doc.renderPageToBitmap(context, 0, size.width.toInt(), size.height.toInt()))
previewImageCache[id] ?: throw java.lang.Exception("unable to generate bitmap with PdfDocumentLoader")
} catch (e: Exception) {
e.printStackTrace()
Bitmap.createBitmap(1.dp.value.toInt(), 150.dp.value.toInt(), Bitmap.Config.ARGB_8888)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright © 2023-2024 PSPDFKit GmbH. All rights reserved.
*
* The PSPDFKit Sample applications are licensed with a modified BSD license.
* Please see License for details. This notice may not be removed from this file.
*/

package com.pspdfkit.example.compose.data.repository

import android.content.Context
import com.pspdfkit.example.compose.data.local.HistoryDao
import com.pspdfkit.example.compose.models.HistoryTable
import com.pspdfkit.example.compose.models.HistoryType.LOCAL
import com.pspdfkit.example.compose.models.HistoryType.RECENT
import com.pspdfkit.example.compose.models.local
import com.pspdfkit.example.compose.utils.DEMO_DOCUMENT_ASSET_NAME
import com.pspdfkit.example.compose.utils.copyFile
import com.pspdfkit.example.compose.utils.doesLocalFileExists
import com.pspdfkit.example.compose.utils.getFile
import com.pspdfkit.example.compose.utils.getWorkingDir
import kotlinx.coroutines.flow.MutableStateFlow

/** [MainRepository] mostly manages all the functions related to Main screen */
class MainRepository(private val historyDao: HistoryDao, private val context: Context) {
init {
/** initialises the specific working cache directory */
context.getWorkingDir().let { if (!it.exists()) it.mkdir() }
}

/** pre provided pdf for testing */
val file = context.getFile(DEMO_DOCUMENT_ASSET_NAME)
val assetFileTransferFlow = MutableStateFlow(file.exists())

/** adds [RECENT] type pdf to database */
private suspend fun insertLocalPdf(path: String) {
historyDao.insert(local(path))
}

/** retrieves all entries from database */
fun getAllPdf() = historyDao.getAllHistory()

/** functionality to copy local pdf file from assets to internal working directory. */
suspend fun init() {
if (context.doesLocalFileExists()) {
assetFileTransferFlow.emit(true)
} else {
file.copyFile(context) {
insertLocalPdf(file.path)
assetFileTransferFlow.emit(true)
}
}
}

/** adds [LOCAL] type pdf to database */
suspend fun addRecentPdf(historyTable: HistoryTable) {
historyDao.insert(historyTable)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright © 2023-2024 PSPDFKit GmbH. All rights reserved.
*
* The PSPDFKit Sample applications are licensed with a modified BSD license.
* Please see License for details. This notice may not be removed from this file.
*/

package com.pspdfkit.example.compose.data.repository

import com.pspdfkit.example.compose.data.local.HistoryDao

/** [PdfRepository] holds functionality related to pdf. */
class PdfRepository(private val historyDao: HistoryDao) {
/** retrieves pdf entry from db by primary key */
fun getPdf(id: String) = historyDao.getHistoryById(id)
}
Loading

0 comments on commit 1be5177

Please sign in to comment.