update
This commit is contained in:
parent
7a3efa2bee
commit
fa82f2eb96
@ -14,8 +14,14 @@ class AppStore(context: Context) {
|
||||
key = PERMISSIONS_DIALOG_PROMPT, defaultValue = false
|
||||
)
|
||||
|
||||
// 文档排序方式:name_asc, name_desc, date_asc, date_desc, size_asc, size_desc
|
||||
var documentSortType: String by store.string(
|
||||
key = DOCUMENT_SORT_TYPE, defaultValue = "date_desc"
|
||||
)
|
||||
|
||||
companion object {
|
||||
private const val FILE_NAME = "prp_sp_name"
|
||||
private const val PERMISSIONS_DIALOG_PROMPT = "permissions_dialog_prompt"
|
||||
private const val DOCUMENT_SORT_TYPE = "document_sort_type"
|
||||
}
|
||||
}
|
||||
@ -9,13 +9,12 @@ import androidx.lifecycle.lifecycleScope
|
||||
import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.ActivityMainBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.room.repository.PdfRepository
|
||||
import com.all.pdfreader.pro.app.ui.dialog.PermissionDialogFragment
|
||||
import com.all.pdfreader.pro.app.ui.fragment.FavoriteFrag
|
||||
import com.all.pdfreader.pro.app.ui.fragment.HomeFrag
|
||||
import com.all.pdfreader.pro.app.ui.fragment.RecentlyFrag
|
||||
import com.all.pdfreader.pro.app.ui.fragment.ToolsFrag
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.setDebouncedClickWithAnimation
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.setClickWithAnimation
|
||||
import com.all.pdfreader.pro.app.util.FileChangeObserver
|
||||
import com.all.pdfreader.pro.app.util.FileUtils
|
||||
import com.all.pdfreader.pro.app.util.PdfMetadataExtractor
|
||||
@ -100,16 +99,16 @@ class MainActivity : BaseActivity(), PermissionDialogFragment.PermissionCallback
|
||||
requestPermissions()
|
||||
}
|
||||
|
||||
binding.sidebarBtn.setDebouncedClickWithAnimation {
|
||||
binding.sidebarBtn.setClickWithAnimation {
|
||||
|
||||
}
|
||||
|
||||
binding.searchBtn.setDebouncedClickWithAnimation {
|
||||
binding.searchBtn.setClickWithAnimation {
|
||||
|
||||
}
|
||||
|
||||
binding.sortingBtn.setDebouncedClickWithAnimation {
|
||||
|
||||
binding.sortingBtn.setClickWithAnimation {
|
||||
showSortDialog()
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,6 +119,40 @@ class MainActivity : BaseActivity(), PermissionDialogFragment.PermissionCallback
|
||||
updateSelectedNav(target)
|
||||
}
|
||||
|
||||
private fun showSortDialog() {
|
||||
val sortOptions = arrayOf(
|
||||
"名称升序", "名称降序",
|
||||
"日期升序", "日期降序",
|
||||
"大小升序", "大小降序"
|
||||
)
|
||||
val sortValues = arrayOf(
|
||||
"name_asc", "name_desc",
|
||||
"date_asc", "date_desc",
|
||||
"size_asc", "size_desc"
|
||||
)
|
||||
|
||||
val currentSort = appStore.documentSortType
|
||||
val checkedItem = sortValues.indexOf(currentSort)
|
||||
|
||||
android.app.AlertDialog.Builder(this)
|
||||
.setTitle("选择排序方式")
|
||||
.setSingleChoiceItems(sortOptions, checkedItem) { dialog, which ->
|
||||
val newSortType = sortValues[which]
|
||||
appStore.documentSortType = newSortType
|
||||
|
||||
// 通知当前fragment更新排序
|
||||
(activeFragment as? SortableFragment)?.onSortTypeChanged(newSortType)
|
||||
|
||||
dialog.dismiss()
|
||||
}
|
||||
.setNegativeButton("取消", null)
|
||||
.show()
|
||||
}
|
||||
|
||||
interface SortableFragment {
|
||||
fun onSortTypeChanged(sortType: String)
|
||||
}
|
||||
|
||||
private fun updateSelectedNav(fragment: Fragment) {
|
||||
binding.homeIv.alpha = 0.5f
|
||||
binding.recentlyIv.alpha = 0.5f
|
||||
|
||||
@ -10,13 +10,14 @@ import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.all.pdfreader.pro.app.databinding.FragmentHomeBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.room.repository.PdfRepository
|
||||
import com.all.pdfreader.pro.app.ui.act.MainActivity
|
||||
import com.all.pdfreader.pro.app.ui.adapter.PdfAdapter
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class HomeFrag : BaseFrag() {
|
||||
class HomeFrag : BaseFrag(), MainActivity.SortableFragment {
|
||||
override val TAG: String = "HomeFrag"
|
||||
private lateinit var binding: FragmentHomeBinding
|
||||
private lateinit var adapter: PdfAdapter
|
||||
@ -53,15 +54,33 @@ class HomeFrag : BaseFrag() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSortTypeChanged(sortType: String) {
|
||||
// 排序类型改变时重新加载数据
|
||||
observeDocuments()
|
||||
}
|
||||
|
||||
private fun observeDocuments() {
|
||||
lifecycleScope.launch {
|
||||
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
PdfRepository.getInstance().getAllDocuments().collect { list ->
|
||||
adapter.updateData(list)
|
||||
logDebug("更新adapter数据")
|
||||
val sortedList = sortDocuments(list)
|
||||
adapter.updateData(sortedList)
|
||||
logDebug("更新adapter数据,排序方式: ${appStore.documentSortType}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun sortDocuments(documents: List<PdfDocumentEntity>): List<PdfDocumentEntity> {
|
||||
return when (appStore.documentSortType) {
|
||||
"name_asc" -> documents.sortedBy { it.fileName.lowercase() }
|
||||
"name_desc" -> documents.sortedByDescending { it.fileName.lowercase() }
|
||||
"date_asc" -> documents.sortedBy { it.lastModified }
|
||||
"date_desc" -> documents.sortedByDescending { it.lastModified }
|
||||
"size_asc" -> documents.sortedBy { it.fileSize }
|
||||
"size_desc" -> documents.sortedByDescending { it.fileSize }
|
||||
else -> documents
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,26 +5,19 @@ import android.view.View
|
||||
object AppUtils {
|
||||
|
||||
/**
|
||||
* 为 View 点击事件添加防抖点击 + 动画 + 执行逻辑
|
||||
* 添加点击动画(点击立即执行逻辑,动画期间禁用点击)
|
||||
*
|
||||
* @param debounceTime 防抖间隔,默认 500 毫秒
|
||||
* @param onClick 点击后执行的逻辑(动画完成后调用)
|
||||
* @param onClick 点击后立即执行的逻辑
|
||||
*/
|
||||
fun View.setDebouncedClickWithAnimation(
|
||||
debounceTime: Long = 500L,
|
||||
onClick: () -> Unit
|
||||
) {
|
||||
var lastClickTime = 0L
|
||||
|
||||
fun View.setClickWithAnimation(onClick: () -> Unit) {
|
||||
this.setOnClickListener {
|
||||
val currentTime = System.currentTimeMillis()
|
||||
if (currentTime - lastClickTime < debounceTime) {
|
||||
return@setOnClickListener
|
||||
}
|
||||
lastClickTime = currentTime
|
||||
|
||||
// 动画 + 禁用点击
|
||||
// 禁用点击,防止动画未完成重复点击
|
||||
this.isEnabled = false
|
||||
|
||||
// 立即执行逻辑
|
||||
onClick()
|
||||
|
||||
// 播放动画
|
||||
this.animate()
|
||||
.scaleX(1.2f)
|
||||
.scaleY(1.2f)
|
||||
@ -35,13 +28,12 @@ object AppUtils {
|
||||
.scaleY(1f)
|
||||
.setDuration(150)
|
||||
.withEndAction {
|
||||
// 动画结束恢复点击
|
||||
this.isEnabled = true
|
||||
onClick() // 执行传入的逻辑
|
||||
}
|
||||
.start()
|
||||
}
|
||||
.start()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user