@@ -2,7 +2,9 @@ package com.rajat.pdfviewer
22
33import android.content.Context
44import 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
68import com.rajat.pdfviewer.util.FileUtils.getCachedFileName
79import com.rajat.pdfviewer.util.FileUtils.isValidPdf
810import 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