Commit a132ef93 authored by wanglei's avatar wanglei

...

parent 4a35edb3
package com.base.pdfviewerscannerwhite.ui.main package com.base.pdfviewerscannerwhite.ui.main
import android.annotation.SuppressLint import android.os.Bundle
import android.app.Activity import androidx.core.content.ContextCompat
import android.app.Dialog import androidx.fragment.app.Fragment
import android.content.Intent import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.core.view.isEmpty import androidx.viewpager2.widget.ViewPager2
import androidx.core.view.isVisible import com.angcyo.tablayout.DslTabIndicator
import androidx.lifecycle.lifecycleScope import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.base.pdfviewerscannerwhite.bean.ConstObject import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.bean.DocumentBean import com.base.pdfviewerscannerwhite.bean.DocumentBean
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_EXCEL
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfviewerscannerwhite.databinding.FragmentDocumentBinding import com.base.pdfviewerscannerwhite.databinding.FragmentDocumentBinding
import com.base.pdfviewerscannerwhite.helper.BaseFragment import com.base.pdfviewerscannerwhite.helper.BaseFragment
import com.base.pdfviewerscannerwhite.ui.adapter.DocumentAdapter
import com.base.pdfviewerscannerwhite.ui.document.excel.ExcelActivity
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfActivity
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfSelectActivity
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfSplitActivity
import com.base.pdfviewerscannerwhite.ui.document.ppt.PptActivity
import com.base.pdfviewerscannerwhite.ui.document.word.WordActivity
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentHomeMoreDialog
import com.base.pdfviewerscannerwhite.ui.view.PdfDialog.showPdfHomeMoreDialog
import com.base.pdfviewerscannerwhite.ui.view.PdfDialog.showPdfPwdDialog
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.checkStorePermission
import java.io.File
import java.util.Locale
class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView {
private val TAG = "DocumentFragment"
var type = TYPE_PDF
private var documentList: ArrayList<DocumentBean> = arrayListOf()
lateinit var adapter: DocumentAdapter
lateinit var documentPresenter: DocumentPresenter
constructor(type: String) : this() {
this.type = type
}
override val binding: FragmentDocumentBinding by lazy { class DocumentFragment : BaseFragment<FragmentDocumentBinding>() {
FragmentDocumentBinding.inflate(layoutInflater)
}
override fun setView() { private val pdfPage: DocumentPageFragment by lazy {
documentPresenter = DocumentPresenter(requireContext(), this, type, lifecycleScope) DocumentPageFragment(DocumentBean.TYPE_PDF)
initAdapter()
} }
override fun onResume() { private val wordPage: DocumentPageFragment by lazy {
super.onResume() DocumentPageFragment(DocumentBean.TYPE_WORD)
initData()
} }
private val excelPage: DocumentPageFragment by lazy {
fun initData() { DocumentPageFragment()
if (requireContext().checkStorePermission()) {
documentPresenter.initData()
}
} }
private val pptPage: DocumentPageFragment by lazy {
override fun refreshDocumentRv(documentList: List<DocumentBean>) { DocumentPageFragment()
binding.llEmpty.isVisible = documentList.isEmpty()
this.documentList.clear()
this.documentList.addAll(documentList)
adapter.submitList(documentList)
} }
private val fragments by lazy {
private fun initAdapter() { mutableListOf(pdfPage, wordPage, excelPage, pptPage)
adapter = DocumentAdapter()
adapter.bookmarkAction = { addRemove, path ->
documentPresenter.saveBookmarkChange(addRemove, path)
}
adapter.itemClick = { item ->
requireActivity().jumpDocument(item)
}
adapter.moreAction = { item ->
documentMoreAction.invoke(item)
}
binding.rv.adapter = adapter
} }
var documentMoreAction: (item: DocumentBean) -> Unit = { item -> override val binding: FragmentDocumentBinding by lazy {
val mainActivity = (requireActivity() as MainActivity) FragmentDocumentBinding.inflate(layoutInflater)
if (item.type == TYPE_PDF) {
requireContext().showPdfHomeMoreDialog(
item, adapter, this, mainActivity
)
}
if (item.type == TYPE_WORD || item.type == TYPE_EXCEL || item.type == TYPE_PPT) {
requireContext().showDocumentHomeMoreDialog(item, this, mainActivity)
}
}
override fun splitPdf(path: String) {
startActivity(Intent(requireContext(), PdfSplitActivity::class.java).apply {
putExtra("path", path)
})
}
override fun mergePdf(path: String) {
startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
putExtra("doWhat", ConstObject.DO_MERGE_PDF)
})
}
override fun deleteDocument(item: DocumentBean) {
val flag = documentList.removeIf { it.path == item.path }
LogEx.logDebug(TAG, "deleteDocument $item.path flag=$flag")
adapter.submitList(documentList)
binding.llEmpty.isVisible = adapter.items.isEmpty()
} }
fun setRecentList() { override fun setView() {
if (isVisible) { initPageViewer()
val recentList = documentList.filter { initTabLayout()
(System.currentTimeMillis() - File(it.path).lastModified()) < 300L * 24 * 60 * 60 * 1000
}
binding.llEmpty.isVisible = recentList.isEmpty()
adapter.submitList(recentList)
}
} }
fun setAllList() { override fun onViewStateRestored(savedInstanceState: Bundle?) {
if (isVisible) { super.onViewStateRestored(savedInstanceState)
adapter.submitList(documentList)
binding.llEmpty.isVisible = documentList.isEmpty()
}
} }
fun setBookmarkList() {
if (isVisible) {
val bookmarkList = documentList.filter { it.isBookmarked }
binding.llEmpty.isVisible = bookmarkList.isEmpty()
adapter.submitList(bookmarkList)
}
}
private fun initPageViewer() {
binding.viewPager2.run {
isUserInputEnabled = true
//https://www.jianshu.com/p/f69bd30cf5b0
//FragmentStateAdapter 这里必须传人fragment
adapter = object : FragmentStateAdapter(this@DocumentFragment) {
override fun getItemCount(): Int {
return fragments.size
}
@SuppressLint("NotifyDataSetChanged") override fun createFragment(position: Int): Fragment {
fun changeSelectUi(select: Boolean) { return fragments[position]
if (isVisible) {
adapter.items.forEach {
if (select) {
it.uiType = 1
} else {
it.isSelect = false
it.uiType = 0
} }
} }
adapter.notifyDataSetChanged()
} }
} binding.viewPager2.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
@SuppressLint("NotifyDataSetChanged") override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
fun changItemSelect(select: Boolean) { super.onPageScrolled(position, positionOffset, positionOffsetPixels)
adapter.items.forEach { if (position == 3 && positionOffset > 0) {
it.isSelect = select binding.viewPager2.setCurrentItem(position, false)
} }
adapter.notifyDataSetChanged() }
}
fun getSelectItems(): List<DocumentBean> {
return adapter.items.filter { it.isSelect }
}
fun removeList(list: List<DocumentBean>) {
list.forEach {
adapter.remove(it)
}
}
fun getSearchItems(search: String): List<DocumentBean> {
if (isVisible) {
return adapter.items.filter { File(it.path).name.lowercase(Locale.ENGLISH).contains(search.lowercase(Locale.ENGLISH)) }
}
return listOf()
}
@SuppressLint("NotifyDataSetChanged") override fun onPageSelected(position: Int) {
fun changeHomeUI() { }
if (isVisible) { })
adapter.items.map { it.uiType == 0 }
adapter.notifyDataSetChanged()
}
} }
companion object { private fun initTabLayout() {
val colorPdf = ContextCompat.getColor(requireContext(), R.color.color_f02f2b)
fun Activity.jumpDocument(item: DocumentBean) { val colorWord = ContextCompat.getColor(requireContext(), R.color.color_0592ee)
if (item.type == TYPE_PDF) { val colorExcel = ContextCompat.getColor(requireContext(), R.color.color_149b55)
if (item.state == 0) { val colorPpt = ContextCompat.getColor(requireContext(), R.color.color_ffa127)
startActivity(Intent(this, PdfActivity::class.java).apply { binding.tabLayoutDocument.apply {
putExtra("path", item.path) tabIndicator.indicatorWidth =
}) this.resources.getDimensionPixelOffset(R.dimen.dp_50)
tabIndicator.indicatorHeight =
this.resources.getDimensionPixelOffset(R.dimen.dp_4)
tabIndicator.indicatorStyle =
DslTabIndicator.INDICATOR_STYLE_BOTTOM
configTabLayoutConfig {
onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
val toIndex = selectIndexList.first()
when (toIndex) {
0 -> {
tabSelectColor = colorPdf
tabIndicator.indicatorColor = colorPdf
binding.viewPager2.currentItem = 0
}
1 -> {
tabSelectColor = colorWord
tabIndicator.indicatorColor = colorWord
binding.viewPager2.currentItem = 1
}
2 -> {
tabSelectColor = colorExcel
tabIndicator.indicatorColor = colorExcel
binding.viewPager2.currentItem = 2
}
3 -> {
tabSelectColor = colorPpt
tabIndicator.indicatorColor = colorPpt
binding.viewPager2.currentItem = 3
}
}
dslSelector.updateStyle()
} }
if (item.state == 1) {
showPdfPwdDialog(
state = item.state,
path = item.path,
isCheckPwd = true,
verificationAction = { pwd ->
startActivity(Intent(this, PdfActivity::class.java).apply {
putExtra("path", item.path)
putExtra("pwd", pwd)
})
})
}
}
if (item.type == TYPE_WORD) {
WordActivity.wordDocumentBean = item
WordActivity.launchDocViewer(this, 3, item.path, -1, 100)
}
if (item.type == TYPE_EXCEL) {
ExcelActivity.excelDocumentBean = item
ExcelActivity.launchDocViewer(this, 3, item.path, -1, 100)
}
if (item.type == TYPE_PPT) {
PptActivity.pptDocumentBean = item
PptActivity.launchDocViewer(this, 3, item.path, -1, 100)
} }
} }
ViewPager2Delegate.install(binding.viewPager2, binding.tabLayoutDocument)
} }
} }
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.main.DocumentFragment"> tools:context=".ui.main.RecentFragment">
<androidx.constraintlayout.widget.ConstraintLayout
<androidx.recyclerview.widget.RecyclerView android:id="@+id/cl_top"
android:id="@+id/rv"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="60dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layout_constraintTop_toTopOf="parent">
tools:listitem="@layout/item_document" />
<LinearLayout <FrameLayout
android:id="@+id/ll_empty" android:id="@+id/fl_fanhui"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:padding="15dp"
android:layout_marginBottom="60dp" android:visibility="gone"
android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent"
android:visibility="gone" app:layout_constraintStart_toStartOf="parent"
tools:ignore="UseCompoundDrawables,UselessParent"> app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/fanhui_b"
tools:ignore="ContentDescription" />
</FrameLayout>
<EditText
android:id="@+id/edit_search"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="20dp"
android:background="@drawable/bg_f8f9fe_10"
android:hint="Enter the file name to search for"
android:imeOptions="actionDone"
android:paddingHorizontal="18dp"
android:singleLine="true"
android:textColor="@color/black"
android:textColorHint="#B8B9BD"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_search"
app:layout_constraintStart_toEndOf="@id/fl_fanhui"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="Autofill,HardcodedText,RtlSymmetry,TextFields" />
<TextView
android:id="@+id/tv_tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="Document"
android:textColor="@color/black"
android:textSize="19sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<ImageView <ImageView
android:id="@+id/iv_paixu"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/empty" android:layout_marginEnd="16dp"
android:src="@mipmap/h_paixu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_xuanze"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<TextView <ImageView
android:id="@+id/iv_xuanze"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_marginEnd="16dp"
android:layout_marginTop="20dp" android:src="@mipmap/h_xuanze"
android:text="Empty" app:layout_constraintBottom_toBottomOf="parent"
android:textColor="#B1B4B9" app:layout_constraintEnd_toStartOf="@id/iv_search"
android:textSize="16sp" app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" /> tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/iv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@mipmap/h_sousuo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/iv_all_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_selector_select"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/iv_search"
app:layout_constraintStart_toStartOf="@id/iv_search"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/ll_main_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/cl_top">
<!-- 外层FrameLayout设置背景用来限定水波纹大小-->
<FrameLayout
android:id="@+id/cl_tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#00000000">
<com.angcyo.tablayout.DslTabLayout
android:id="@+id/tabLayout_document"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tab_badge_gravity="center"
app:tab_badge_offset_x="20dp"
app:tab_draw_indicator="true"
app:tab_enable_text_color="true"
app:tab_indicator_drawable="@drawable/indicator_bottom_line"
app:tab_indicator_height="3dp"
app:tab_item_is_equ_width="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple_select"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:text="PDF"
android:textSize="16sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ripple_select"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:paddingVertical="5dp"
android:text="Word"
android:textSize="16sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple_select"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:text="Excel"
android:textSize="16sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple_select"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:text="PPT"
android:textSize="16sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
</com.angcyo.tablayout.DslTabLayout>
<View
android:id="@+id/v_tabLayout_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout> </LinearLayout>
</FrameLayout>
\ No newline at end of file </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
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