Commit 09ad637e authored by wanglei's avatar wanglei

...

parent acc3f3c1
......@@ -18,6 +18,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.PDFViewerScannerWhite"
tools:targetApi="34">
<activity
android:name=".ui.document.pdf.PdfLoadingActivity"
android:exported="false" />
<activity
android:name=".ui.document.pdf.PdfSplitActivity"
android:exported="false" />
......
......@@ -10,6 +10,7 @@ import com.base.pdfviewerscannerwhite.utils.PermissionUtils.checkStorePermission
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.requestStorePermission
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
import com.github.barteksc.pdfviewer.scroll.ScrollHandle
import com.tom_roush.pdfbox.pdmodel.PDDocument
import java.io.File
......@@ -64,6 +65,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
private fun initAdapter() {
adapter = PdfPagerAdapter(pafPath)
adapter.clickAction = { pageIndex ->
binding.pdfview.jumpTo(pageIndex, true)
}
binding.rvPager.adapter = adapter
}
......@@ -80,18 +84,22 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
}
}
@SuppressLint("SetTextI18n")
private fun loadPdfView(file: File, password: String = "") {
binding.pdfview.fromFile(file)
.defaultPage(pageNumber)
.enableAnnotationRendering(true)
.scrollHandle(object : DefaultScrollHandle(this) {})
.scrollHandle(DefaultScrollHandle(this))
.onPageChange { page, pageCount ->
binding.tvPageCount.text = "${page + 1}/$pageCount"
adapter.changeSelectPager(page)
}
.spacing(10)
.onPageError { page, t ->
}.password(password).load()
}
override fun onAttachedToWindow() {
......
package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.content.Intent
import androidx.lifecycle.lifecycleScope
import com.base.pdfviewerscannerwhite.databinding.ActivityPdfLoadingBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.io.File
class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() {
private val TAG = "PdfLoadingActivity"
private var srcPath: String = ""
private var newName: String = ""
private var splitIndex: List<Int> = listOf()
private lateinit var pdfPresenter: PdfPresenter
override val binding: ActivityPdfLoadingBinding by lazy {
ActivityPdfLoadingBinding.inflate(layoutInflater)
}
var isFinishBoolean: Boolean = false
var newPdfFile: File? = null
override fun initView() {
pdfPresenter = PdfPresenter(this)
initSpPa()
pdfPresenter.splitPdf(File(srcPath), newName, splitIndex, finishAction = {
newPdfFile = it
isFinishBoolean = true
})
startProgress()
}
private fun startProgress() = lifecycleScope.launch(Dispatchers.Main) {
while (isActive) {
if (isFinishBoolean) {
binding.progressBar.progress = 100
delay(200)
break
} else {
binding.progressBar.progress += 2
}
delay(500)
}
if (newPdfFile == null) {
toast("split pdf failed!")
finish()
return@launch
}
startActivity(Intent(this@PdfLoadingActivity, PdfActivity::class.java).apply {
putExtra("path", newPdfFile?.absolutePath ?: "")
})
}
private fun initSpPa() {
srcPath = intent?.extras?.getString("srcPath", "") ?: ""
newName = intent?.extras?.getString("newName", "") ?: ""
splitIndex = intent.extras?.getString("splitIndex")?.split(",")?.map { it.toInt() } ?: listOf()
}
}
\ No newline at end of file
......@@ -23,6 +23,7 @@ class PdfPagerAdapter(val pdfPath: String, val itemLayout: Int = R.layout.item_p
BaseQuickAdapter<PdfPageBean, PdfPagerAdapter.PdfPagerViewHolder>() {
var selectAction: ((enable: Boolean, allSelect: Boolean) -> Unit)? = null
var clickAction: ((pageIndex:Int) -> Unit)? = null
inner class PdfPagerViewHolder(view: View) : ViewHolder(view)
......@@ -53,6 +54,9 @@ class PdfPagerAdapter(val pdfPath: String, val itemLayout: Int = R.layout.item_p
} else {
loadPagerDrawable(context, item, binding.root, binding.ivPager)
}
binding.root.setOnClickListener {
clickAction?.invoke(item.pageIndex)
}
}
R.layout.item_pdf_pager_split -> {
......@@ -105,4 +109,13 @@ class PdfPagerAdapter(val pdfPath: String, val itemLayout: Int = R.layout.item_p
items.forEach { it.isSelect = select }
notifyDataSetChanged()
}
@SuppressLint("NotifyDataSetChanged")
fun changeSelectPager(page: Int) {
runCatching {
items.forEach { it.isSelect = false }
items[page].isSelect = true
}
notifyDataSetChanged()
}
}
\ No newline at end of file
......@@ -8,7 +8,10 @@ import com.tom_roush.pdfbox.pdmodel.PDDocument
import java.io.File
class PdfPresenter(val context: Context, val pdfView: PdfView) {
class PdfPresenter(
val context: Context,
val pdfView: PdfView? = null
) {
var handler: Handler? = null
......@@ -18,11 +21,14 @@ class PdfPresenter(val context: Context, val pdfView: PdfView) {
repeat(number) {
list.add(PdfPageBean(it))
}
pdfView.initPdfPageRv(list)
pdfView?.initPdfPageRv(list)
}
fun splitPdf(file: File, newName: String, splitIndex: List<Int>) {
fun splitPdf(
file: File, newName: String, splitIndex: List<Int>,
finishAction: (newFile: File?) -> Unit
) = Thread {
val newFile = File(file.parentFile, newName)
try {
// 加载现有 PDF 文档
val sourceDocument = PDDocument.load(file)
......@@ -35,14 +41,15 @@ class PdfPresenter(val context: Context, val pdfView: PdfView) {
val page = sourceDocument.getPage(index)
newDocument.addPage(page)
}
val newFile = File(file.parentFile, newName)
// 保存新的 PDF 文档
newDocument.save(newFile)
newDocument.close()
sourceDocument.close()
}catch (e:Exception){
} catch (e: Exception) {
println("Error occurred while splitting PDF.")
finishAction.invoke(null)
return@Thread
}
}
finishAction.invoke(newFile)
}.start()
}
\ No newline at end of file
package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.content.Intent
import androidx.activity.addCallback
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.bean.PdfPageBean
......@@ -41,9 +42,13 @@ class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>(), PdfView {
}
binding.tvBtnSplit.setOnClickListener {
val splitIndex = pdfPagerAdapter.items.filter { it.isSelect }.map { it.pageIndex }
val file = File(path)
showDocumentRenameDialog { newName ->
pdfPresenter.splitPdf(file, newName, splitIndex)
startActivity(Intent(this, PdfLoadingActivity::class.java).apply {
putExtra("srcPath", path)
putExtra("newName", newName)
putExtra("splitIndex", splitIndex.joinToString(separator = ","))
})
finish()
}
}
}
......
......@@ -5,6 +5,7 @@ import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.core.widget.addTextChangedListener
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.bean.DocumentBean
import com.base.pdfviewerscannerwhite.databinding.DialogDocumentRenameBinding
......@@ -83,6 +84,10 @@ object DialogView {
dialog.show()
binding.edit.addTextChangedListener {
binding.tvOk.isEnabled = !it.isNullOrEmpty()
}
val tempName = "Split_" + System.currentTimeMillis().toFormatTime2()
binding.edit.setText(name ?: tempName)
binding.edit.requestFocus()
......
......@@ -28,7 +28,7 @@ object KotlinExt {
}
fun Long.toFormatTime2(): String {
return SimpleDateFormat("yyyyMMdd_HHmmss,", Locale.ENGLISH).format(this)
return SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(this)
}
......
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_00b8de_10" android:state_enabled="false" />
<item android:drawable="@drawable/bg_7fdcee_10" android:state_enabled="true" />
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置背景色 -->
<item android:id="@android:id/background">
<shape>
<stroke
android:width="1.5dp"
android:color="#00B8DE" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- 设置进度条颜色 -->
<item
android:id="@android:id/progress"
android:bottom="3dp"
android:end="3dp"
android:start="3dp"
android:top="3dp">
<!-- <scale android:scaleWidth="100%">-->
<clip>
<shape>
<corners android:radius="5dp" />
<solid android:color="#00B8DE" />
</shape>
</clip>
<!-- </scale>-->
</item>
</layer-list>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.document.pdf.PdfLoadingActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/tu_loading"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.35"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="Splitting PDF, please wait."
android:textColor="#333333"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@id/iv"
app:layout_constraintStart_toStartOf="@id/iv"
app:layout_constraintTop_toBottomOf="@id/iv"
tools:ignore="HardcodedText" />
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="16sp"
android:layout_marginHorizontal="56dp"
android:layout_marginTop="19dp"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/progress_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_desc" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -74,7 +74,7 @@
android:layout_width="163dp"
android:layout_height="48dp"
android:layout_marginStart="14dp"
android:background="@drawable/bg_00b8de_10"
android:background="@drawable/bg_selector_btn"
android:gravity="center"
android:text="Ok"
android:textColor="@color/white"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment