Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit initWithUrl fun to use functional programming in (proInitWithUrl) function #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .idea/appInsightsSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.rajat.sample.pdfviewer
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -102,7 +104,24 @@ class MainActivity : AppCompatActivity() {
lifecycle = lifecycle
)
binding.pdfView.jumpToPage(3)

//second version of initWithUrl version, using functional programming
binding.pdfView.proWithUrl(url = download_file_url2,
lifecycleCoroutineScope = lifecycleScope,
lifecycle = lifecycle,
onDownloadProgress = {_,_,_->
//doing any thing
},
onDownloadPdfSuccess = {_->
//doing any thing
},
onDownloadPdfFailed = {e->
//doing any thing
},
onDownloadStarted = {
//doing any thing
},
onPageChanged = {_,_->}
)
}

binding.openInCompose.setOnClickListener {
Expand Down Expand Up @@ -160,5 +179,4 @@ class MainActivity : AppCompatActivity() {
)
}


}
41 changes: 41 additions & 0 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfRendererView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.os.Looper
import android.os.ParcelFileDescriptor
import android.os.Parcelable
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
Expand Down Expand Up @@ -102,6 +103,46 @@ class PdfRendererView @JvmOverloads constructor(
})
}

fun proWithUrl(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is proWithUrl? Mind adding comments and improve naming.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated version of initWithUrl fun.
Just pass to it function types as a @param.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still confused with the naming.

url: String,
lifecycleCoroutineScope: LifecycleCoroutineScope,
lifecycle: Lifecycle,
onDownloadStarted: () -> Unit,
onDownloadProgress: (progress: Int, downloadedBytes: Long, totalBytes: Long?) -> Unit,
onDownloadPdfSuccess: (absolutePath: String) -> Unit,
onDownloadPdfFailed: (e: Throwable) -> Unit,
onPageChanged: (currentPage: Int, totalPage: Int) -> Unit
){
this.initWithUrl(
url = url,
lifecycleCoroutineScope = lifecycleCoroutineScope,
lifecycle = lifecycle
)

this.statusListener = object :StatusCallBack {
override fun onPdfLoadStart() {
onDownloadStarted()
}

override fun onPdfLoadProgress(progress: Int, downloadedBytes: Long, totalBytes: Long?) {
onDownloadProgress(progress, downloadedBytes, totalBytes)
}

override fun onPdfLoadSuccess(absolutePath: String) {
onDownloadPdfSuccess(absolutePath)
}

override fun onError(error: Throwable) {
onDownloadPdfFailed(error)
}

override fun onPageChanged(currentPage: Int, totalPage: Int) {
onPageChanged(currentPage, totalPage)
}
}
}


fun initWithFile(file: File) {
init(file)
}
Expand Down