Skip to content

Commit 3eba240

Browse files
committed
Improve Cache mechanism
1 parent be41da5 commit 3eba240

File tree

14 files changed

+457
-340
lines changed

14 files changed

+457
-340
lines changed

app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,34 @@ package com.rajat.sample.pdfviewer
33
import android.content.Intent
44
import android.os.Bundle
55
import android.util.Log
6+
import android.widget.Toast
67
import androidx.activity.result.contract.ActivityResultContracts
78
import androidx.appcompat.app.AppCompatActivity
89
import androidx.core.view.WindowCompat
910
import androidx.lifecycle.lifecycleScope
1011
import com.rajat.pdfviewer.PdfRendererView
1112
import com.rajat.pdfviewer.PdfViewerActivity
13+
import com.rajat.pdfviewer.util.CacheStrategy
1214
import com.rajat.pdfviewer.util.ToolbarTitleBehavior
1315
import com.rajat.pdfviewer.util.saveTo
1416
import com.rajat.sample.pdfviewer.databinding.ActivityMainBinding
17+
import kotlinx.coroutines.delay
18+
import kotlinx.coroutines.launch
1519

1620
class MainActivity : AppCompatActivity() {
1721

1822
// View Binding
1923
private lateinit var binding: ActivityMainBinding
2024

2125
// Sample PDF URLs
22-
private val sampleOnlinePdf = "https://css4.pub/2015/usenix/example.pdf"
23-
private val largePdf = "https://research.nhm.org/pdfs/10840/10840.pdf"
26+
private val largePdf = "https://css4.pub/2015/usenix/example.pdf"
27+
private val largePdf1 = "https://research.nhm.org/pdfs/10840/10840.pdf"
2428
private val localPdf = "http://192.168.0.72:8001/pw.pdf"
2529
private val newsletterPdf = "https://css4.pub/2017/newsletter/drylab.pdf"
2630
private val textbookPdf = "https://css4.pub/2015/textbook/somatosensory.pdf"
2731

32+
private val pdfList = listOf(largePdf, largePdf1, newsletterPdf, textbookPdf)
33+
2834
override fun onCreate(savedInstanceState: Bundle?) {
2935
super.onCreate(savedInstanceState)
3036

@@ -48,7 +54,7 @@ class MainActivity : AppCompatActivity() {
4854
private fun setupListeners() {
4955
binding.onlinePdf.setOnClickListener {
5056
setupPdfStatusListener()
51-
launchPdfFromUrl(largePdf)
57+
launchPdfFromUrl(largePdf1)
5258
}
5359

5460
binding.pickPdfButton.setOnClickListener {
@@ -64,7 +70,8 @@ class MainActivity : AppCompatActivity() {
6470
binding.pdfView.initWithUrl(
6571
url = textbookPdf,
6672
lifecycleCoroutineScope = lifecycleScope,
67-
lifecycle = lifecycle
73+
lifecycle = lifecycle,
74+
cacheStrategy = CacheStrategy.MINIMIZE_CACHE
6875
)
6976
binding.pdfView.jumpToPage(3)
7077
}
@@ -108,14 +115,16 @@ class MainActivity : AppCompatActivity() {
108115
* Launches a PDF file from a URL.
109116
*/
110117
private fun launchPdfFromUrl(url: String) {
118+
Toast.makeText(this@MainActivity, "Opening PDF: $url", Toast.LENGTH_SHORT).show()
111119
startActivity(
112120
PdfViewerActivity.launchPdfFromUrl(
113121
context = this,
114122
pdfUrl = url,
115123
pdfTitle = "PDF Title",
116124
saveTo = saveTo.DOWNLOADS,
117125
enableDownload = true,
118-
toolbarTitleBehavior = ToolbarTitleBehavior.SINGLE_LINE_SCROLLABLE
126+
toolbarTitleBehavior = ToolbarTitleBehavior.SINGLE_LINE_SCROLLABLE,
127+
cacheStrategy = CacheStrategy.MINIMIZE_CACHE
119128
)
120129
)
121130
}

pdfViewer/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ mavenPublishing {
9494
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
9595
signAllPublications()
9696

97-
coordinates("io.github.afreakyelf", "Pdf-Viewer", "2.1.1")
97+
coordinates("io.github.afreakyelf", "Pdf-Viewer", "2.2.1")
9898

9999
pom {
100100
name.set("PDF Viewer")

pdfViewer/src/main/java/com/rajat/pdfviewer/PdfDownloader.kt

+30-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.rajat.pdfviewer
22

33
import android.content.Context
44
import android.util.Log
5-
import com.rajat.pdfviewer.util.FileUtils.clearPdfCache
5+
import com.rajat.pdfviewer.util.CacheHelper
6+
import com.rajat.pdfviewer.util.CacheStrategy
7+
import com.rajat.pdfviewer.util.CommonUtils.Companion.MAX_CACHED_PDFS
68
import com.rajat.pdfviewer.util.FileUtils.getCachedFileName
79
import com.rajat.pdfviewer.util.FileUtils.isValidPdf
810
import com.rajat.pdfviewer.util.FileUtils.writeFile
@@ -22,14 +24,15 @@ class PdfDownloader(
2224
private val coroutineScope: CoroutineScope,
2325
private val headers: HeaderData,
2426
private val url: String,
27+
private val cacheStrategy: CacheStrategy,
2528
private val listener: StatusListener
2629
) {
2730

2831
interface StatusListener {
2932
fun getContext(): Context
3033
fun onDownloadStart()
3134
fun onDownloadProgress(currentBytes: Long, totalBytes: Long)
32-
fun onDownloadSuccess(absolutePath: String)
35+
fun onDownloadSuccess(downloadedFile: File)
3336
fun onError(error: Throwable)
3437
}
3538

@@ -46,25 +49,28 @@ class PdfDownloader(
4649

4750
private suspend fun checkAndDownload(downloadUrl: String) {
4851
val cachedFileName = getCachedFileName(downloadUrl)
52+
val cacheDir = File(listener.getContext().cacheDir, "___pdf___cache___/$cachedFileName") // ✅ Folder for PDF + Rendered Pages
4953

50-
if (lastDownloadedFile != cachedFileName) {
51-
clearPdfCache(listener.getContext(), cachedFileName)
54+
if (!cacheDir.exists()) {
55+
cacheDir.mkdirs()
5256
}
5357

54-
val cachedFile = File(listener.getContext().cacheDir, cachedFileName)
58+
val pdfFile = File(cacheDir, cachedFileName)
5559

56-
if (cachedFile.exists() && isValidPdf(cachedFile)) {
60+
// Use centralized cache logic
61+
CacheHelper.handleCacheStrategy("Downloader", cacheDir, cacheStrategy, cachedFileName, MAX_CACHED_PDFS)
62+
63+
if (pdfFile.exists() && isValidPdf(pdfFile)) {
5764
withContext(Dispatchers.Main) {
58-
listener.onDownloadSuccess(cachedFile.absolutePath)
65+
listener.onDownloadSuccess(pdfFile)
5966
}
6067
} else {
61-
retryDownload(downloadUrl, cachedFileName)
68+
retryDownload(downloadUrl, pdfFile)
6269
}
63-
64-
lastDownloadedFile = cachedFileName
6570
}
6671

67-
private suspend fun retryDownload(downloadUrl: String, cachedFileName: String) {
72+
73+
private suspend fun retryDownload(downloadUrl: String, cachedFileName: File) {
6874
var attempt = 0
6975
while (attempt < MAX_RETRIES) {
7076
try {
@@ -98,16 +104,14 @@ class PdfDownloader(
98104
return message.contains("Invalid content type") || message.contains("Downloaded file is not a valid PDF")
99105
}
100106

101-
private suspend fun downloadFile(downloadUrl: String, cachedFileName: String) {
107+
private suspend fun downloadFile(downloadUrl: String, pdfFile: File) {
102108
withContext(Dispatchers.IO) {
103-
val cacheDir = listener.getContext().cacheDir
104-
val tempFile =
105-
File.createTempFile("download_", ".tmp", cacheDir)
106-
val outputFile = File(cacheDir, cachedFileName)
107-
108-
if (outputFile.exists() && !isValidPdf(outputFile)) {
109-
Log.d("PdfDownloader", "Deleting invalid cached PDF: ${outputFile.absolutePath}")
110-
outputFile.delete()
109+
listener.getContext().cacheDir
110+
val tempFile = File.createTempFile("download_", ".tmp", pdfFile.parentFile)
111+
112+
113+
if (pdfFile.exists() && !isValidPdf(pdfFile)) {
114+
pdfFile.delete()
111115
}
112116

113117
val response = makeNetworkRequest(downloadUrl)
@@ -123,15 +127,17 @@ class PdfDownloader(
123127
}
124128
}
125129

126-
tempFile.renameTo(outputFile)
130+
tempFile.renameTo(pdfFile)
127131

128-
if (!isValidPdf(outputFile)) {
129-
outputFile.delete()
132+
if (!isValidPdf(pdfFile)) {
133+
pdfFile.delete()
130134
throw IOException("Downloaded file is not a valid PDF")
135+
} else {
136+
Log.d("PdfDownloader", "Downloaded PDF to: ${pdfFile.absolutePath}")
131137
}
132138

133139
coroutineScope.launch(Dispatchers.Main) {
134-
listener.onDownloadSuccess(outputFile.absolutePath)
140+
listener.onDownloadSuccess(pdfFile)
135141
}
136142
}
137143
}

0 commit comments

Comments
 (0)