From d1626ee5d65433459ac5c8c74224584e8e1e42ec Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 30 Aug 2020 19:27:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/ReadRecordDao.kt | 2 +- .../app/ui/widget/font/FontSelectDialog.kt | 70 ++++++++++--------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt b/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt index 991eaf61c..311194cf0 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt @@ -10,7 +10,7 @@ interface ReadRecordDao { @get:Query("select * from readRecord") val all: List - @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 @get:Query("select sum(readTime) from readRecord") diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index b10371864..697c3f68c 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.widget.font -import android.annotation.SuppressLint import android.app.Activity.RESULT_OK import android.content.Intent import android.net.Uri @@ -38,6 +37,7 @@ class FontSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener, FontAdapter.CallBack { private val fontFolderRequestCode = 35485 + private val fontRegex = Regex(".*\\.[ot]tf") private val fontFolder by lazy { FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts") } @@ -75,12 +75,12 @@ class FontSelectDialog : BaseDialogFragment(), if (fontPath.isContentPath()) { val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath)) if (doc?.canRead() == true) { - getFontFiles(doc) + loadFontFiles(doc) } else { openFolder() } } else { - getFontFilesByPermission(fontPath) + loadFontFilesByPermission(fontPath) } } } @@ -111,22 +111,21 @@ class FontSelectDialog : BaseDialogFragment(), FilePicker.selectFolder(this@FontSelectDialog, fontFolderRequestCode) { val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts" putPrefString(PreferKey.fontFolder, path) - getFontFilesByPermission(path) + loadFontFilesByPermission(path) } } } - @SuppressLint("DefaultLocale") - private fun getFontFiles(doc: DocumentFile) { + private fun loadFontFiles(doc: DocumentFile) { execute { val fontItems = arrayListOf() val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri) docItems.forEach { item -> - if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) { + if (item.name.toLowerCase(Locale.getDefault()).matches(fontRegex)) { fontItems.add(item) } } - fontItems + fontItems.sortedBy { it.name } }.onSuccess { adapter?.setItems(it) }.onError { @@ -134,36 +133,41 @@ class FontSelectDialog : BaseDialogFragment(), } } - @SuppressLint("DefaultLocale") - private fun getFontFilesByPermission(path: String) { + private fun loadFontFilesByPermission(path: String) { PermissionsCompat.Builder(this@FontSelectDialog) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) .onGranted { - try { - val fontItems = arrayListOf() - 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 ?: "") - } + loadFontFiles(path) } .request() } + private fun loadFontFiles(path: String) { + execute { + val fontItems = arrayListOf() + 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) { execute { fontFolder.listFiles()?.forEach { @@ -198,7 +202,7 @@ class FontSelectDialog : BaseDialogFragment(), when (requestCode) { fontFolderRequestCode -> { putPrefString(PreferKey.fontFolder, currentPath) - getFontFilesByPermission(currentPath) + loadFontFilesByPermission(currentPath) } } } @@ -215,10 +219,10 @@ class FontSelectDialog : BaseDialogFragment(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION ) - getFontFiles(doc) + loadFontFiles(doc) } else { RealPathUtil.getPath(requireContext(), uri)?.let { - getFontFilesByPermission(it) + loadFontFilesByPermission(it) } } }