优化文字选择,不再缓存

pull/276/head^2
gedoor 5 years ago
parent 8d6d02c5d5
commit 5f84e661fa
  1. 3
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  2. 11
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  3. 23
      app/src/main/java/io/legado/app/ui/widget/font/FontAdapter.kt
  4. 87
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  5. 2
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt

@ -64,6 +64,9 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
viewModel.upChapterList() viewModel.upChapterList()
}, 1000) }, 1000)
} }
view_pager_main.postDelayed({
viewModel.postLoad()
}, 3000)
} }
override fun onNavigationItemSelected(item: MenuItem): Boolean { override fun onNavigationItemSelected(item: MenuItem): Boolean {

@ -10,6 +10,7 @@ import io.legado.app.data.entities.RssSource
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
@ -77,4 +78,14 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
} }
} }
} }
fun postLoad() {
execute {
FileUtils.getDirFile(context.cacheDir, "Fonts").let {
if (it.exists()) {
it.delete()
}
}
}
}
} }

@ -2,9 +2,12 @@ package io.legado.app.ui.widget.font
import android.content.Context import android.content.Context
import android.graphics.Typeface import android.graphics.Typeface
import android.os.Build
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.utils.DocItem
import io.legado.app.utils.RealPathUtil
import io.legado.app.utils.invisible import io.legado.app.utils.invisible
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.item_font.view.* import kotlinx.android.synthetic.main.item_font.view.*
@ -13,12 +16,24 @@ import org.jetbrains.anko.toast
import java.io.File import java.io.File
class FontAdapter(context: Context, val callBack: CallBack) : class FontAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<File>(context, R.layout.item_font) { SimpleRecyclerAdapter<DocItem>(context, R.layout.item_font) {
override fun convert(holder: ItemViewHolder, item: File, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
try { try {
val typeface = Typeface.createFromFile(item) val typeface: Typeface? = if (item.isContentPath) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.contentResolver
.openFileDescriptor(item.uri, "r")
?.fileDescriptor?.let {
Typeface.Builder(it).build()
}
} else {
Typeface.createFromFile(RealPathUtil.getPath(context, item.uri))
}
} else {
Typeface.createFromFile(item.uri.toString())
}
tv_font.typeface = typeface tv_font.typeface = typeface
} catch (e: Exception) { } catch (e: Exception) {
context.toast("读取${item.name}字体失败") context.toast("读取${item.name}字体失败")
@ -42,7 +57,7 @@ class FontAdapter(context: Context, val callBack: CallBack) :
} }
interface CallBack { interface CallBack {
fun onClick(file: File) fun onClick(docItem: DocItem)
val curFilePath: String val curFilePath: String
} }
} }

@ -26,11 +26,11 @@ import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.android.synthetic.main.dialog_font_select.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import java.util.*
class FontSelectDialog : BaseDialogFragment(), class FontSelectDialog : BaseDialogFragment(),
FileChooserDialog.CallBack, FileChooserDialog.CallBack,
@ -40,9 +40,6 @@ class FontSelectDialog : BaseDialogFragment(),
private val fontFolder by lazy { private val fontFolder by lazy {
FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts") FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts")
} }
private val fontCacheFolder by lazy {
FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts")
}
private var adapter: FontAdapter? = null private var adapter: FontAdapter? = null
override fun onStart() { override fun onStart() {
@ -90,7 +87,9 @@ class FontSelectDialog : BaseDialogFragment(),
R.id.menu_default -> { R.id.menu_default -> {
val requireContext = requireContext() val requireContext = requireContext()
requireContext.alert(titleResource = R.string.system_typeface) { requireContext.alert(titleResource = R.string.system_typeface) {
items(requireContext.resources.getStringArray(R.array.system_typefaces).toList()) { _, i -> items(
requireContext.resources.getStringArray(R.array.system_typefaces).toList()
) { _, i ->
AppConfig.systemTypefaces = i AppConfig.systemTypefaces = i
onDefaultFontChange() onDefaultFontChange()
dismiss() dismiss()
@ -117,40 +116,16 @@ class FontSelectDialog : BaseDialogFragment(),
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFiles(doc: DocumentFile) { private fun getFontFiles(doc: DocumentFile) {
execute { execute {
val fontItems = arrayListOf<DocItem>()
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri) val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri)
fontCacheFolder.listFiles()?.forEach { fontFile ->
var contain = false
for (item in docItems) {
if (fontFile.name == item.name) {
contain = true
break
}
}
if (!contain) {
fontFile.delete()
}
}
docItems.forEach { item -> docItems.forEach { item ->
if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) { if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) {
val fontFile = FileUtils.getFile(fontCacheFolder, item.name) fontItems.add(item)
if (!fontFile.exists()) {
DocumentUtils.readBytes(App.INSTANCE, item.uri)?.let { byteArray ->
fontFile.writeBytes(byteArray)
}
}
}
}
try {
fontCacheFolder.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}?.let {
withContext(Main) {
adapter?.setItems(it.toList())
}
} }
} catch (e: Exception) {
toast(e.localizedMessage ?: "")
} }
fontItems
}.onSuccess {
adapter?.setItems(it)
}.onError { }.onError {
toast("getFontFiles:${it.localizedMessage}") toast("getFontFiles:${it.localizedMessage}")
} }
@ -163,12 +138,22 @@ class FontSelectDialog : BaseDialogFragment(),
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { .onGranted {
try { try {
val fontItems = arrayListOf<DocItem>()
val file = File(path) val file = File(path)
file.listFiles { pathName -> file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}?.let { }?.forEach {
adapter?.setItems(it.toList()) fontItems.add(
DocItem(
it.name,
it.extension,
it.length(),
Date(it.lastModified()),
Uri.parse(it.absolutePath)
)
)
} }
adapter?.setItems(fontItems)
} catch (e: Exception) { } catch (e: Exception) {
toast(e.localizedMessage ?: "") toast(e.localizedMessage ?: "")
} }
@ -176,16 +161,32 @@ class FontSelectDialog : BaseDialogFragment(),
.request() .request()
} }
override fun onClick(file: File) { override fun onClick(docItem: DocItem) {
launch(IO) { execute {
file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true) fontFolder.listFiles()?.forEach {
.absolutePath.let { path -> it.delete()
if (curFilePath != path) { }
withContext(Main) { if (docItem.isContentPath) {
callBack?.selectFile(path) val file = FileUtils.createFileIfNotExist(fontFolder, docItem.name)
@Suppress("BlockingMethodInNonBlockingContext")
requireActivity().contentResolver.openInputStream(docItem.uri)?.use { input ->
file.outputStream().use { output ->
input.copyTo(output)
} }
} }
callBack?.selectFile(file.path)
} else {
val file = File(docItem.uri.toString())
file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true)
.absolutePath.let { path ->
if (curFilePath != path) {
withContext(Main) {
callBack?.selectFile(path)
}
}
}
} }
}.onSuccess {
dialog?.dismiss() dialog?.dismiss()
} }
} }

@ -138,6 +138,8 @@ data class DocItem(
val isDir: Boolean by lazy { val isDir: Boolean by lazy {
DocumentsContract.Document.MIME_TYPE_DIR == attr DocumentsContract.Document.MIME_TYPE_DIR == attr
} }
val isContentPath get() = uri.toString().isContentPath()
} }
@Throws(Exception::class) @Throws(Exception::class)

Loading…
Cancel
Save