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 {
FragmentDocumentBinding.inflate(layoutInflater)
}
override fun setView() {
documentPresenter = DocumentPresenter(requireContext(), this, type, lifecycleScope)
initAdapter()
}
override fun onResume() {
super.onResume()
initData()
}
fun initData() { class DocumentFragment : BaseFragment<FragmentDocumentBinding>() {
if (requireContext().checkStorePermission()) {
documentPresenter.initData()
}
}
override fun refreshDocumentRv(documentList: List<DocumentBean>) { private val pdfPage: DocumentPageFragment by lazy {
binding.llEmpty.isVisible = documentList.isEmpty() DocumentPageFragment(DocumentBean.TYPE_PDF)
this.documentList.clear()
this.documentList.addAll(documentList)
adapter.submitList(documentList)
} }
private fun initAdapter() { private val wordPage: DocumentPageFragment by lazy {
adapter = DocumentAdapter() DocumentPageFragment(DocumentBean.TYPE_WORD)
adapter.bookmarkAction = { addRemove, path ->
documentPresenter.saveBookmarkChange(addRemove, path)
}
adapter.itemClick = { item ->
requireActivity().jumpDocument(item)
} }
adapter.moreAction = { item -> private val excelPage: DocumentPageFragment by lazy {
documentMoreAction.invoke(item) DocumentPageFragment()
} }
binding.rv.adapter = adapter private val pptPage: DocumentPageFragment by lazy {
DocumentPageFragment()
} }
private val fragments by lazy {
var documentMoreAction: (item: DocumentBean) -> Unit = { item -> mutableListOf(pdfPage, wordPage, excelPage, pptPage)
val mainActivity = (requireActivity() as MainActivity)
if (item.type == TYPE_PDF) {
requireContext().showPdfHomeMoreDialog(
item, adapter, this, mainActivity
)
} }
if (item.type == TYPE_WORD || item.type == TYPE_EXCEL || item.type == TYPE_PPT) { override val binding: FragmentDocumentBinding by lazy {
requireContext().showDocumentHomeMoreDialog(item, this, mainActivity) FragmentDocumentBinding.inflate(layoutInflater)
}
}
override fun splitPdf(path: String) {
startActivity(Intent(requireContext(), PdfSplitActivity::class.java).apply {
putExtra("path", path)
})
} }
override fun mergePdf(path: String) { override fun setView() {
startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply { initPageViewer()
putExtra("doWhat", ConstObject.DO_MERGE_PDF) initTabLayout()
})
} }
override fun deleteDocument(item: DocumentBean) { override fun onViewStateRestored(savedInstanceState: Bundle?) {
val flag = documentList.removeIf { it.path == item.path } super.onViewStateRestored(savedInstanceState)
LogEx.logDebug(TAG, "deleteDocument $item.path flag=$flag")
adapter.submitList(documentList)
binding.llEmpty.isVisible = adapter.items.isEmpty()
} }
fun setRecentList() {
if (isVisible) {
val recentList = documentList.filter {
(System.currentTimeMillis() - File(it.path).lastModified()) < 300L * 24 * 60 * 60 * 1000
}
binding.llEmpty.isVisible = recentList.isEmpty()
adapter.submitList(recentList)
}
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
} }
fun setAllList() { override fun createFragment(position: Int): Fragment {
if (isVisible) { return fragments[position]
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)
} }
binding.viewPager2.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
} override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels)
if (position == 3 && positionOffset > 0) {
@SuppressLint("NotifyDataSetChanged") binding.viewPager2.setCurrentItem(position, false)
fun changeSelectUi(select: Boolean) {
if (isVisible) {
adapter.items.forEach {
if (select) {
it.uiType = 1
} else {
it.isSelect = false
it.uiType = 0
}
}
adapter.notifyDataSetChanged()
} }
} }
@SuppressLint("NotifyDataSetChanged") override fun onPageSelected(position: Int) {
fun changItemSelect(select: Boolean) {
adapter.items.forEach {
it.isSelect = select
} }
adapter.notifyDataSetChanged() })
} }
fun getSelectItems(): List<DocumentBean> { private fun initTabLayout() {
return adapter.items.filter { it.isSelect } val colorPdf = ContextCompat.getColor(requireContext(), R.color.color_f02f2b)
} val colorWord = ContextCompat.getColor(requireContext(), R.color.color_0592ee)
val colorExcel = ContextCompat.getColor(requireContext(), R.color.color_149b55)
val colorPpt = ContextCompat.getColor(requireContext(), R.color.color_ffa127)
binding.tabLayoutDocument.apply {
tabIndicator.indicatorWidth =
this.resources.getDimensionPixelOffset(R.dimen.dp_50)
tabIndicator.indicatorHeight =
this.resources.getDimensionPixelOffset(R.dimen.dp_4)
tabIndicator.indicatorStyle =
DslTabIndicator.INDICATOR_STYLE_BOTTOM
fun removeList(list: List<DocumentBean>) { configTabLayoutConfig {
list.forEach { onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
adapter.remove(it) val toIndex = selectIndexList.first()
} when (toIndex) {
0 -> {
tabSelectColor = colorPdf
tabIndicator.indicatorColor = colorPdf
binding.viewPager2.currentItem = 0
} }
fun getSearchItems(search: String): List<DocumentBean> { 1 -> {
if (isVisible) { tabSelectColor = colorWord
return adapter.items.filter { File(it.path).name.lowercase(Locale.ENGLISH).contains(search.lowercase(Locale.ENGLISH)) } tabIndicator.indicatorColor = colorWord
} binding.viewPager2.currentItem = 1
return listOf()
} }
@SuppressLint("NotifyDataSetChanged") 2 -> {
fun changeHomeUI() { tabSelectColor = colorExcel
if (isVisible) { tabIndicator.indicatorColor = colorExcel
adapter.items.map { it.uiType == 0 } binding.viewPager2.currentItem = 2
adapter.notifyDataSetChanged()
} }
}
companion object {
fun Activity.jumpDocument(item: DocumentBean) { 3 -> {
if (item.type == TYPE_PDF) { tabSelectColor = colorPpt
if (item.state == 0) { tabIndicator.indicatorColor = colorPpt
startActivity(Intent(this, PdfActivity::class.java).apply { binding.viewPager2.currentItem = 3
putExtra("path", item.path)
})
}
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) { dslSelector.updateStyle()
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"
app:layout_constraintStart_toStartOf="parent"
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" android:visibility="gone"
tools:ignore="UseCompoundDrawables,UselessParent"> 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" />
<ImageView
android:id="@+id/iv_xuanze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@mipmap/h_xuanze"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_search"
app:layout_constraintTop_toTopOf="parent"
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 <TextView
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:background="@drawable/ripple_select"
android:layout_marginTop="20dp" android:clipChildren="false"
android:text="Empty" android:clipToPadding="false"
android:textColor="#B1B4B9" 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:textSize="16sp"
android:textStyle="bold"
tools:ignore="HardcodedText" /> 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