添加排序

pull/349/head
gedoor 4 years ago
parent 1847fcb5ad
commit d1626ee5d6
  1. 2
      app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt
  2. 70
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -10,7 +10,7 @@ interface ReadRecordDao {
@get:Query("select * from readRecord") @get:Query("select * from readRecord")
val all: List<ReadRecord> val all: List<ReadRecord>
@get:Query("select bookName, sum(readTime) as readTime from readRecord group by bookName") @get:Query("select bookName, sum(readTime) as readTime from readRecord group by bookName order by bookName")
val allShow: List<ReadRecordShow> val allShow: List<ReadRecordShow>
@get:Query("select sum(readTime) from readRecord") @get:Query("select sum(readTime) from readRecord")

@ -1,6 +1,5 @@
package io.legado.app.ui.widget.font package io.legado.app.ui.widget.font
import android.annotation.SuppressLint
import android.app.Activity.RESULT_OK import android.app.Activity.RESULT_OK
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
@ -38,6 +37,7 @@ class FontSelectDialog : BaseDialogFragment(),
Toolbar.OnMenuItemClickListener, Toolbar.OnMenuItemClickListener,
FontAdapter.CallBack { FontAdapter.CallBack {
private val fontFolderRequestCode = 35485 private val fontFolderRequestCode = 35485
private val fontRegex = Regex(".*\\.[ot]tf")
private val fontFolder by lazy { private val fontFolder by lazy {
FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts") FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts")
} }
@ -75,12 +75,12 @@ class FontSelectDialog : BaseDialogFragment(),
if (fontPath.isContentPath()) { if (fontPath.isContentPath()) {
val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath)) val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath))
if (doc?.canRead() == true) { if (doc?.canRead() == true) {
getFontFiles(doc) loadFontFiles(doc)
} else { } else {
openFolder() openFolder()
} }
} else { } else {
getFontFilesByPermission(fontPath) loadFontFilesByPermission(fontPath)
} }
} }
} }
@ -111,22 +111,21 @@ class FontSelectDialog : BaseDialogFragment(),
FilePicker.selectFolder(this@FontSelectDialog, fontFolderRequestCode) { FilePicker.selectFolder(this@FontSelectDialog, fontFolderRequestCode) {
val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts" val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts"
putPrefString(PreferKey.fontFolder, path) putPrefString(PreferKey.fontFolder, path)
getFontFilesByPermission(path) loadFontFilesByPermission(path)
} }
} }
} }
@SuppressLint("DefaultLocale") private fun loadFontFiles(doc: DocumentFile) {
private fun getFontFiles(doc: DocumentFile) {
execute { execute {
val fontItems = arrayListOf<DocItem>() val fontItems = arrayListOf<DocItem>()
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri) val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri)
docItems.forEach { item -> docItems.forEach { item ->
if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) { if (item.name.toLowerCase(Locale.getDefault()).matches(fontRegex)) {
fontItems.add(item) fontItems.add(item)
} }
} }
fontItems fontItems.sortedBy { it.name }
}.onSuccess { }.onSuccess {
adapter?.setItems(it) adapter?.setItems(it)
}.onError { }.onError {
@ -134,36 +133,41 @@ class FontSelectDialog : BaseDialogFragment(),
} }
} }
@SuppressLint("DefaultLocale") private fun loadFontFilesByPermission(path: String) {
private fun getFontFilesByPermission(path: String) {
PermissionsCompat.Builder(this@FontSelectDialog) PermissionsCompat.Builder(this@FontSelectDialog)
.addPermissions(*Permissions.Group.STORAGE) .addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { .onGranted {
try { loadFontFiles(path)
val fontItems = arrayListOf<DocItem>()
val file = File(path)
file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}?.forEach {
fontItems.add(
DocItem(
it.name,
it.extension,
it.length(),
Date(it.lastModified()),
Uri.parse(it.absolutePath)
)
)
}
adapter?.setItems(fontItems)
} catch (e: Exception) {
toast(e.localizedMessage ?: "")
}
} }
.request() .request()
} }
private fun loadFontFiles(path: String) {
execute {
val fontItems = arrayListOf<DocItem>()
val file = File(path)
file.listFiles { pathName ->
pathName.name.toLowerCase(Locale.getDefault()).matches(fontRegex)
}?.forEach {
fontItems.add(
DocItem(
it.name,
it.extension,
it.length(),
Date(it.lastModified()),
Uri.parse(it.absolutePath)
)
)
}
fontItems.sortedBy { it.name }
}.onSuccess {
adapter?.setItems(it)
}.onError {
toast("getFontFiles:${it.localizedMessage}")
}
}
override fun onClick(docItem: DocItem) { override fun onClick(docItem: DocItem) {
execute { execute {
fontFolder.listFiles()?.forEach { fontFolder.listFiles()?.forEach {
@ -198,7 +202,7 @@ class FontSelectDialog : BaseDialogFragment(),
when (requestCode) { when (requestCode) {
fontFolderRequestCode -> { fontFolderRequestCode -> {
putPrefString(PreferKey.fontFolder, currentPath) putPrefString(PreferKey.fontFolder, currentPath)
getFontFilesByPermission(currentPath) loadFontFilesByPermission(currentPath)
} }
} }
} }
@ -215,10 +219,10 @@ class FontSelectDialog : BaseDialogFragment(),
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION
) )
getFontFiles(doc) loadFontFiles(doc)
} else { } else {
RealPathUtil.getPath(requireContext(), uri)?.let { RealPathUtil.getPath(requireContext(), uri)?.let {
getFontFilesByPermission(it) loadFontFilesByPermission(it)
} }
} }
} }

Loading…
Cancel
Save