Skip to content

Commit

Permalink
api 21 support
Browse files Browse the repository at this point in the history
  • Loading branch information
rishujam committed Oct 6, 2023
1 parent 87fc858 commit 676f4c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId "com.docs.pdfviewer"
minSdk 26
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
Expand Down
2 changes: 1 addition & 1 deletion docwatcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
compileSdk 33

defaultConfig {
minSdk 26
minSdk 21
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
33 changes: 24 additions & 9 deletions docwatcher/src/main/java/com/docs/docwatcher/DocView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.docs.docwatcher
import android.content.Context
import android.graphics.Bitmap
import android.graphics.pdf.PdfRenderer
import android.os.Build
import android.os.ParcelFileDescriptor
import android.util.AttributeSet
import android.view.LayoutInflater
Expand Down Expand Up @@ -90,16 +91,30 @@ class DocView @JvmOverloads constructor(
val cardText = "1 / ${list.size}"
pageText.text = cardText
pageCard.showPagesViewForSomeSeconds(coroutineScope, context)
rv.setOnScrollChangeListener { _, _, _, _, _ ->
val visiblePosition = (rv.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
if(visiblePosition != -1) {
val currPage = visiblePosition + 1
if(!noOfPages.isNullOrEmpty()) {
pageCard.show()
val text = "$currPage / $noOfPages"
pageText.text = text
pageCard.showPagesViewForSomeSeconds(coroutineScope, context)
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
handleOnScroll()
}
})
} else {
rv.setOnScrollChangeListener { _, _, _, _, _ ->
handleOnScroll()
}
}
}

private fun handleOnScroll() {
val visiblePosition = (rv.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
if(visiblePosition != -1) {
val currPage = visiblePosition + 1
if(!noOfPages.isNullOrEmpty()) {
pageCard.show()
val text = "$currPage / $noOfPages"
pageText.text = text
pageCard.showPagesViewForSomeSeconds(coroutineScope, context)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.app.DownloadManager
import android.content.Context
import android.database.ContentObserver
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.core.net.toUri
import com.docs.docwatcher.DocView

Expand All @@ -19,7 +19,11 @@ internal class PdfDownloader(
private val listener: DocView.DownloadListener
) {

private val downloadManager = context.getSystemService(DownloadManager::class.java)
private val downloadManager = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
} else {
context.getSystemService(DownloadManager::class.java)
}

fun download(url: String) {
val request = DownloadManager.Request(url.toUri())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,4 @@ internal fun View.show() {

internal fun View.gone() {
this.visibility = View.GONE
}

internal fun View.invisible() {
this.visibility = View.INVISIBLE
}

0 comments on commit 676f4c0

Please sign in to comment.