功能项新增搜索选择功能
This commit is contained in:
parent
b04a8c9142
commit
85a66a9911
@ -2,7 +2,6 @@ package com.all.pdfreader.pro.app.ui.act
|
|||||||
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Environment
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
@ -13,6 +12,7 @@ import com.all.pdfreader.pro.app.R
|
|||||||
import com.all.pdfreader.pro.app.databinding.ActivityMainBinding
|
import com.all.pdfreader.pro.app.databinding.ActivityMainBinding
|
||||||
import com.all.pdfreader.pro.app.model.FileActionEvent
|
import com.all.pdfreader.pro.app.model.FileActionEvent
|
||||||
import com.all.pdfreader.pro.app.model.FragmentType
|
import com.all.pdfreader.pro.app.model.FragmentType
|
||||||
|
import com.all.pdfreader.pro.app.model.PdfPickerSource
|
||||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||||
import com.all.pdfreader.pro.app.ui.dialog.PermissionDialogFragment
|
import com.all.pdfreader.pro.app.ui.dialog.PermissionDialogFragment
|
||||||
import com.all.pdfreader.pro.app.ui.dialog.ProgressDialogFragment
|
import com.all.pdfreader.pro.app.ui.dialog.ProgressDialogFragment
|
||||||
@ -25,7 +25,6 @@ import com.all.pdfreader.pro.app.ui.fragment.ToolsFrag
|
|||||||
import com.all.pdfreader.pro.app.util.AppUtils.setClickWithAnimation
|
import com.all.pdfreader.pro.app.util.AppUtils.setClickWithAnimation
|
||||||
import com.all.pdfreader.pro.app.util.AppUtils.setOnSingleClickListener
|
import com.all.pdfreader.pro.app.util.AppUtils.setOnSingleClickListener
|
||||||
import com.all.pdfreader.pro.app.util.PdfScanner
|
import com.all.pdfreader.pro.app.util.PdfScanner
|
||||||
import com.all.pdfreader.pro.app.util.PdfUtils
|
|
||||||
import com.all.pdfreader.pro.app.util.StoragePermissionHelper
|
import com.all.pdfreader.pro.app.util.StoragePermissionHelper
|
||||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||||
import com.all.pdfreader.pro.app.viewmodel.observeEvent
|
import com.all.pdfreader.pro.app.viewmodel.observeEvent
|
||||||
@ -187,7 +186,7 @@ class MainActivity : BaseActivity(), PermissionDialogFragment.PermissionCallback
|
|||||||
}
|
}
|
||||||
|
|
||||||
binding.searchBtn.setClickWithAnimation {
|
binding.searchBtn.setClickWithAnimation {
|
||||||
startActivity(SearchActivity.createIntent(this))
|
startActivity(SearchActivity.createIntent(this, PdfPickerSource.NONE))
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.sortingBtn.setClickWithAnimation {
|
binding.sortingBtn.setClickWithAnimation {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.all.pdfreader.pro.app.R
|
import com.all.pdfreader.pro.app.R
|
||||||
@ -112,10 +113,6 @@ class PdfPickerActivity : BaseActivity() {
|
|||||||
onSelectModelItemClick = {
|
onSelectModelItemClick = {
|
||||||
if (source == PdfPickerSource.MERGE) {
|
if (source == PdfPickerSource.MERGE) {
|
||||||
val selectedItems = adapter.getSelectedItems()
|
val selectedItems = adapter.getSelectedItems()
|
||||||
if (selectedItems.isNotEmpty()) {
|
|
||||||
val filePaths = selectedItems.map { it.filePath }
|
|
||||||
logDebug("${filePaths.size}")
|
|
||||||
}
|
|
||||||
updateViewAndState(selectedItems.size)
|
updateViewAndState(selectedItems.size)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -164,10 +161,43 @@ class PdfPickerActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val searchLauncher =
|
||||||
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
|
if (result.resultCode == RESULT_OK) {
|
||||||
|
if (source == PdfPickerSource.MERGE) {
|
||||||
|
val selectedPath =
|
||||||
|
result.data?.getStringExtra(SearchActivity.EXTRA_SELECTED_PATH)
|
||||||
|
val currentList = adapter.getCurrentList().toMutableList()
|
||||||
|
val selectedItemIndex = currentList.indexOfFirst { it.filePath == selectedPath }
|
||||||
|
if (selectedItemIndex != -1) {
|
||||||
|
// 找到选中项
|
||||||
|
val selectedItem = currentList[selectedItemIndex]
|
||||||
|
selectedItem.isSelected = true
|
||||||
|
// 移动到第一位
|
||||||
|
currentList.removeAt(selectedItemIndex)
|
||||||
|
currentList.add(0, selectedItem)
|
||||||
|
|
||||||
|
adapter.updateData(currentList)
|
||||||
|
}
|
||||||
|
val selectedItems = adapter.getSelectedItems()
|
||||||
|
updateViewAndState(selectedItems.size)
|
||||||
|
} else {
|
||||||
|
// 只有非多选的情况才关闭
|
||||||
|
// 这里主要是为了只有通过这边进入搜索界面后,在搜索界面点击了item后finish前面这个act
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openSearch() {
|
||||||
|
val intent = SearchActivity.createIntent(this, source)
|
||||||
|
searchLauncher.launch(intent)
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupClick() {
|
private fun setupClick() {
|
||||||
binding.backBtn.setOnSingleClickListener { finish() }
|
binding.backBtn.setOnSingleClickListener { finish() }
|
||||||
binding.searchBtn.setClickWithAnimation {
|
binding.searchBtn.setClickWithAnimation {
|
||||||
|
openSearch()
|
||||||
}
|
}
|
||||||
binding.continueNowBtn.setOnSingleClickListener {
|
binding.continueNowBtn.setOnSingleClickListener {
|
||||||
val selectedItems = adapter.getSelectedItems()
|
val selectedItems = adapter.getSelectedItems()
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.all.pdfreader.pro.app.ui.act
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.text.Editable
|
import android.text.Editable
|
||||||
import android.text.TextWatcher
|
import android.text.TextWatcher
|
||||||
@ -14,9 +15,11 @@ import androidx.lifecycle.lifecycleScope
|
|||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.all.pdfreader.pro.app.R
|
import com.all.pdfreader.pro.app.R
|
||||||
import com.all.pdfreader.pro.app.databinding.ActivitySearchPdfBinding
|
import com.all.pdfreader.pro.app.databinding.ActivitySearchPdfBinding
|
||||||
|
import com.all.pdfreader.pro.app.model.PdfPickerSource
|
||||||
import com.all.pdfreader.pro.app.ui.adapter.SearchPdfAdapter
|
import com.all.pdfreader.pro.app.ui.adapter.SearchPdfAdapter
|
||||||
import com.all.pdfreader.pro.app.ui.dialog.ListMoreDialogFragment
|
import com.all.pdfreader.pro.app.ui.dialog.ListMoreDialogFragment
|
||||||
import com.all.pdfreader.pro.app.ui.dialog.PermissionDialogFragment
|
import com.all.pdfreader.pro.app.ui.dialog.PdfRemovePasswordDialog
|
||||||
|
import com.all.pdfreader.pro.app.ui.dialog.PdfSetPasswordDialog
|
||||||
import com.all.pdfreader.pro.app.ui.dialog.PromptDialogFragment
|
import com.all.pdfreader.pro.app.ui.dialog.PromptDialogFragment
|
||||||
import com.all.pdfreader.pro.app.util.AppUtils.showKeyboard
|
import com.all.pdfreader.pro.app.util.AppUtils.showKeyboard
|
||||||
import com.gyf.immersionbar.ImmersionBar
|
import com.gyf.immersionbar.ImmersionBar
|
||||||
@ -25,7 +28,7 @@ import kotlinx.coroutines.delay
|
|||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
import kotlin.math.log
|
import java.io.Serializable
|
||||||
|
|
||||||
class SearchActivity : BaseActivity() {
|
class SearchActivity : BaseActivity() {
|
||||||
override val TAG: String = "SearchActivity"
|
override val TAG: String = "SearchActivity"
|
||||||
@ -34,9 +37,13 @@ class SearchActivity : BaseActivity() {
|
|||||||
const val FRAG_TAG = "SearchActivity"
|
const val FRAG_TAG = "SearchActivity"
|
||||||
private const val PREF_SEARCH_HISTORY = "pref_search_history"
|
private const val PREF_SEARCH_HISTORY = "pref_search_history"
|
||||||
private const val KEY_HISTORY_LIST = "key_history_list_json"
|
private const val KEY_HISTORY_LIST = "key_history_list_json"
|
||||||
|
private const val EXTRA_SOURCE = "extra_source"
|
||||||
|
const val EXTRA_SELECTED_PATH = "extra_selected_path"
|
||||||
|
|
||||||
fun createIntent(context: Context): Intent {
|
fun createIntent(context: Context, source: PdfPickerSource): Intent {
|
||||||
return Intent(context, SearchActivity::class.java)
|
return Intent(context, SearchActivity::class.java).apply {
|
||||||
|
putExtra(EXTRA_SOURCE, source)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +54,7 @@ class SearchActivity : BaseActivity() {
|
|||||||
|
|
||||||
private val searchHistory: MutableList<String> = mutableListOf()
|
private val searchHistory: MutableList<String> = mutableListOf()
|
||||||
private lateinit var sp: SharedPreferences
|
private lateinit var sp: SharedPreferences
|
||||||
|
private lateinit var source: PdfPickerSource // 来源
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -54,7 +62,7 @@ class SearchActivity : BaseActivity() {
|
|||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
ImmersionBar.with(this).statusBarView(binding.view).statusBarDarkFont(true)
|
ImmersionBar.with(this).statusBarView(binding.view).statusBarDarkFont(true)
|
||||||
.navigationBarColor(R.color.bg_color).init()
|
.navigationBarColor(R.color.bg_color).init()
|
||||||
|
source = getSerializableOrDefault(EXTRA_SOURCE, PdfPickerSource.NONE)
|
||||||
sp = getSharedPreferences(PREF_SEARCH_HISTORY, MODE_PRIVATE)
|
sp = getSharedPreferences(PREF_SEARCH_HISTORY, MODE_PRIVATE)
|
||||||
loadHistory()
|
loadHistory()
|
||||||
|
|
||||||
@ -88,7 +96,13 @@ class SearchActivity : BaseActivity() {
|
|||||||
delay(150)
|
delay(150)
|
||||||
pdfRepository.searchDocuments(query).collectLatest { list ->
|
pdfRepository.searchDocuments(query).collectLatest { list ->
|
||||||
if (list.isNotEmpty()) {
|
if (list.isNotEmpty()) {
|
||||||
adapter.updateData(list) {
|
// 再根据来源过滤
|
||||||
|
val displayList = when (source) {
|
||||||
|
PdfPickerSource.LOCK -> list.filter { !it.isPassword }//只展示没有被加密的
|
||||||
|
PdfPickerSource.UNLOCK -> list.filter { it.isPassword }//只展示已经被加密的
|
||||||
|
else -> list
|
||||||
|
}
|
||||||
|
adapter.updateData(displayList) {
|
||||||
adapter.highlightItems(query.trim())
|
adapter.highlightItems(query.trim())
|
||||||
}
|
}
|
||||||
binding.noFilesLayout.visibility = View.GONE
|
binding.noFilesLayout.visibility = View.GONE
|
||||||
@ -125,10 +139,53 @@ class SearchActivity : BaseActivity() {
|
|||||||
private fun initView() {
|
private fun initView() {
|
||||||
binding.searchEdit.showKeyboard()
|
binding.searchEdit.showKeyboard()
|
||||||
adapter = SearchPdfAdapter(onItemClick = { pdf ->
|
adapter = SearchPdfAdapter(onItemClick = { pdf ->
|
||||||
|
when (source) {
|
||||||
|
PdfPickerSource.NONE -> {
|
||||||
val intent = PdfViewActivity.createIntent(this, pdf.filePath)
|
val intent = PdfViewActivity.createIntent(this, pdf.filePath)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
|
finish()
|
||||||
val query = binding.searchEdit.text.toString()
|
val query = binding.searchEdit.text.toString()
|
||||||
if (query.isNotEmpty()) saveHistory(query)
|
if (query.isNotEmpty()) saveHistory(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfPickerSource.MERGE -> {
|
||||||
|
val data = Intent().apply { putExtra(EXTRA_SELECTED_PATH, pdf.filePath) }
|
||||||
|
setResult(RESULT_OK, data)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfPickerSource.SPLIT -> {
|
||||||
|
val intent = SplitPdfActivity.createIntent(this, pdf.filePath)
|
||||||
|
startActivity(intent)
|
||||||
|
setResult(RESULT_OK)//通知选择界面关闭
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfPickerSource.LOCK -> {
|
||||||
|
PdfSetPasswordDialog(onOkClick = { password ->
|
||||||
|
val intent = PdfResultActivity.createIntentLock(
|
||||||
|
this, pdf.filePath, password, PdfPickerSource.LOCK
|
||||||
|
)
|
||||||
|
startActivity(intent)
|
||||||
|
setResult(RESULT_OK)//通知选择界面关闭
|
||||||
|
finish()
|
||||||
|
}).show(supportFragmentManager, "PdfSetPasswordDialog")
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfPickerSource.UNLOCK -> {
|
||||||
|
PdfRemovePasswordDialog(pdf.filePath, onOkClick = { password ->
|
||||||
|
val intent = PdfResultActivity.createIntentLock(
|
||||||
|
this, pdf.filePath, password, PdfPickerSource.UNLOCK
|
||||||
|
)
|
||||||
|
startActivity(intent)
|
||||||
|
setResult(RESULT_OK)
|
||||||
|
finish()
|
||||||
|
}).show(supportFragmentManager, "PdfRemovePasswordDialog")
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfPickerSource.TO_IMAGES -> {}
|
||||||
|
PdfPickerSource.TO_LONG_IMAGE -> {}
|
||||||
|
}
|
||||||
}, onMoreClick = { pdf ->
|
}, onMoreClick = { pdf ->
|
||||||
ListMoreDialogFragment(pdf.filePath).show(supportFragmentManager, FRAG_TAG)
|
ListMoreDialogFragment(pdf.filePath).show(supportFragmentManager, FRAG_TAG)
|
||||||
})
|
})
|
||||||
@ -170,9 +227,7 @@ class SearchActivity : BaseActivity() {
|
|||||||
|
|
||||||
searchHistory.forEach { keyword ->
|
searchHistory.forEach { keyword ->
|
||||||
val tagView = inflater.inflate(
|
val tagView = inflater.inflate(
|
||||||
R.layout.item_history_tag,
|
R.layout.item_history_tag, binding.historyFlexLayout, false
|
||||||
binding.historyFlexLayout,
|
|
||||||
false
|
|
||||||
) as TextView
|
) as TextView
|
||||||
tagView.text = keyword
|
tagView.text = keyword
|
||||||
tagView.setOnClickListener {
|
tagView.setOnClickListener {
|
||||||
@ -183,4 +238,15 @@ class SearchActivity : BaseActivity() {
|
|||||||
binding.historyFlexLayout.addView(tagView)
|
binding.historyFlexLayout.addView(tagView)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private inline fun <reified T : Serializable> getSerializableOrDefault(
|
||||||
|
key: String, default: T
|
||||||
|
): T {
|
||||||
|
val result: T? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
intent.getSerializableExtra(key, T::class.java)
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION") intent.getSerializableExtra(key) as? T
|
||||||
|
}
|
||||||
|
return result ?: default
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -114,6 +114,10 @@ class PdfAdapter(
|
|||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getCurrentList(): List<PdfDocumentEntity> {
|
||||||
|
return pdfList
|
||||||
|
}
|
||||||
|
|
||||||
private fun toggleSelection(item: PdfDocumentEntity, position: Int) {
|
private fun toggleSelection(item: PdfDocumentEntity, position: Int) {
|
||||||
item.isSelected = !item.isSelected
|
item.isSelected = !item.isSelected
|
||||||
notifyItemChanged(position)
|
notifyItemChanged(position)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user