Skip to content

Commit

Permalink
Merge pull request #72 from amit517/pdf_url_from_protected_field
Browse files Browse the repository at this point in the history
Replace "Global Scope" with "lifeCycleScope"
  • Loading branch information
afreakyelf authored May 28, 2023
2 parents b91e6fc + bd018ba commit 318a2e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pdfViewer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ android {
}

dependencies {
def lifecycle_version = "2.4.1"

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.9.0'
Expand All @@ -49,4 +51,6 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common:$lifecycle_version"
}
16 changes: 8 additions & 8 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfDownloader.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.rajat.pdfviewer

import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.io.BufferedInputStream
import java.io.File
Expand All @@ -20,14 +19,15 @@ internal class PdfDownloader(url: String, private val listener: StatusListener)
fun onDownloadProgress(currentBytes: Long, totalBytes: Long) {}
fun onDownloadSuccess(absolutePath: String) {}
fun onError(error: Throwable) {}
fun getCoroutineScope(): CoroutineScope
}

init {
GlobalScope.async { download(url) }
listener.getCoroutineScope().launch(Dispatchers.IO) { download(url) }
}

private fun download(downloadUrl: String) {
GlobalScope.launch(Dispatchers.Main) { listener.onDownloadStart() }
listener.getCoroutineScope().launch(Dispatchers.Main) { listener.onDownloadStart() }
val outputFile = File(listener.getContext().cacheDir, "downloaded_pdf.pdf")
if (outputFile.exists())
outputFile.delete()
Expand All @@ -49,7 +49,7 @@ internal class PdfDownloader(url: String, private val listener: StatusListener)
break
if (totalLength > 0) {
downloaded += bufferSize
GlobalScope.launch(Dispatchers.Main) {
listener.getCoroutineScope().launch(Dispatchers.Main) {
listener.onDownloadProgress(
downloaded.toLong(),
totalLength.toLong()
Expand All @@ -60,9 +60,9 @@ internal class PdfDownloader(url: String, private val listener: StatusListener)
} while (true)
} catch (e: Exception) {
e.printStackTrace()
GlobalScope.launch(Dispatchers.Main) { listener.onError(e) }
listener.getCoroutineScope().launch(Dispatchers.Main) { listener.onError(e) }
return
}
GlobalScope.launch(Dispatchers.Main) { listener.onDownloadSuccess(outputFile.absolutePath) }
listener.getCoroutineScope().launch(Dispatchers.Main) { listener.onDownloadSuccess(outputFile.absolutePath) }
}
}
}
11 changes: 9 additions & 2 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfRendererView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.NO_POSITION
import kotlinx.android.synthetic.main.pdf_rendererview.view.*
import kotlinx.coroutines.CoroutineScope
import java.io.File
import java.net.URLEncoder

Expand Down Expand Up @@ -63,7 +67,8 @@ class PdfRendererView @JvmOverloads constructor(
fun initWithUrl(
url: String,
pdfQuality: PdfQuality = this.quality,
engine: PdfEngine = this.engine
engine: PdfEngine = this.engine,
lifecycleScope: LifecycleCoroutineScope = (context as AppCompatActivity).lifecycleScope
) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || engine == PdfEngine.GOOGLE) {
initUnderKitkat(url)
Expand Down Expand Up @@ -93,6 +98,8 @@ class PdfRendererView @JvmOverloads constructor(
error.printStackTrace()
statusListener?.onError(error)
}

override fun getCoroutineScope(): CoroutineScope = lifecycleScope
})
}

Expand Down Expand Up @@ -256,4 +263,4 @@ class PdfRendererView @JvmOverloads constructor(
pdfRendererCore.closePdfRender()
}

}
}

0 comments on commit 318a2e1

Please sign in to comment.