@@ -2,7 +2,9 @@ package com.rajat.pdfviewer
2
2
3
3
import android.content.Context
4
4
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
6
8
import com.rajat.pdfviewer.util.FileUtils.getCachedFileName
7
9
import com.rajat.pdfviewer.util.FileUtils.isValidPdf
8
10
import com.rajat.pdfviewer.util.FileUtils.writeFile
@@ -22,14 +24,15 @@ class PdfDownloader(
22
24
private val coroutineScope : CoroutineScope ,
23
25
private val headers : HeaderData ,
24
26
private val url : String ,
27
+ private val cacheStrategy : CacheStrategy ,
25
28
private val listener : StatusListener
26
29
) {
27
30
28
31
interface StatusListener {
29
32
fun getContext (): Context
30
33
fun onDownloadStart ()
31
34
fun onDownloadProgress (currentBytes : Long , totalBytes : Long )
32
- fun onDownloadSuccess (absolutePath : String )
35
+ fun onDownloadSuccess (downloadedFile : File )
33
36
fun onError (error : Throwable )
34
37
}
35
38
@@ -46,25 +49,28 @@ class PdfDownloader(
46
49
47
50
private suspend fun checkAndDownload (downloadUrl : String ) {
48
51
val cachedFileName = getCachedFileName(downloadUrl)
52
+ val cacheDir = File (listener.getContext().cacheDir, " ___pdf___cache___/$cachedFileName " ) // ✅ Folder for PDF + Rendered Pages
49
53
50
- if (lastDownloadedFile != cachedFileName ) {
51
- clearPdfCache(listener.getContext(), cachedFileName )
54
+ if (! cacheDir.exists() ) {
55
+ cacheDir.mkdirs( )
52
56
}
53
57
54
- val cachedFile = File (listener.getContext(). cacheDir, cachedFileName)
58
+ val pdfFile = File (cacheDir, cachedFileName)
55
59
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)) {
57
64
withContext(Dispatchers .Main ) {
58
- listener.onDownloadSuccess(cachedFile.absolutePath )
65
+ listener.onDownloadSuccess(pdfFile )
59
66
}
60
67
} else {
61
- retryDownload(downloadUrl, cachedFileName )
68
+ retryDownload(downloadUrl, pdfFile )
62
69
}
63
-
64
- lastDownloadedFile = cachedFileName
65
70
}
66
71
67
- private suspend fun retryDownload (downloadUrl : String , cachedFileName : String ) {
72
+
73
+ private suspend fun retryDownload (downloadUrl : String , cachedFileName : File ) {
68
74
var attempt = 0
69
75
while (attempt < MAX_RETRIES ) {
70
76
try {
@@ -98,16 +104,14 @@ class PdfDownloader(
98
104
return message.contains(" Invalid content type" ) || message.contains(" Downloaded file is not a valid PDF" )
99
105
}
100
106
101
- private suspend fun downloadFile (downloadUrl : String , cachedFileName : String ) {
107
+ private suspend fun downloadFile (downloadUrl : String , pdfFile : File ) {
102
108
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()
111
115
}
112
116
113
117
val response = makeNetworkRequest(downloadUrl)
@@ -123,15 +127,17 @@ class PdfDownloader(
123
127
}
124
128
}
125
129
126
- tempFile.renameTo(outputFile )
130
+ tempFile.renameTo(pdfFile )
127
131
128
- if (! isValidPdf(outputFile )) {
129
- outputFile .delete()
132
+ if (! isValidPdf(pdfFile )) {
133
+ pdfFile .delete()
130
134
throw IOException (" Downloaded file is not a valid PDF" )
135
+ } else {
136
+ Log .d(" PdfDownloader" , " Downloaded PDF to: ${pdfFile.absolutePath} " )
131
137
}
132
138
133
139
coroutineScope.launch(Dispatchers .Main ) {
134
- listener.onDownloadSuccess(outputFile.absolutePath )
140
+ listener.onDownloadSuccess(pdfFile )
135
141
}
136
142
}
137
143
}
0 commit comments