pull/66/head
kunfei 6 years ago
parent 82dce02541
commit e49a512185
  1. 53
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -18,10 +18,21 @@ import io.legado.app.constant.PreferKey
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import io.legado.app.utils.toast
import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.android.synthetic.main.dialog_font_select.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import kotlin.coroutines.CoroutineContext
class FontSelectDialog : DialogFragment(), FontAdapter.CallBack { class FontSelectDialog : DialogFragment(),
CoroutineScope,
FontAdapter.CallBack {
lateinit var job: Job
private val fontFolderRequestCode = 35485 private val fontFolderRequestCode = 35485
private lateinit var adapter: FontAdapter private lateinit var adapter: FontAdapter
var curPath: String? = null var curPath: String? = null
@ -29,6 +40,8 @@ class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator
var defaultFont: (() -> Unit)? = null var defaultFont: (() -> Unit)? = null
var selectFile: ((path: String) -> Unit)? = null var selectFile: ((path: String) -> Unit)? = null
override val coroutineContext: CoroutineContext
get() = job + Main
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -42,6 +55,7 @@ class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
job = Job()
return inflater.inflate(R.layout.dialog_font_select, container) return inflater.inflate(R.layout.dialog_font_select, container)
} }
@ -64,26 +78,33 @@ class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
} }
} }
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFiles(uri: Uri) { private fun getFontFiles(uri: Uri) {
DocumentFile.fromTreeUri(requireContext(), uri)?.listFiles()?.forEach { file -> launch(IO) {
if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) { DocumentFile.fromTreeUri(requireContext(), uri)?.listFiles()?.forEach { file ->
DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let { if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) {
FileHelp.getFile(fontFolder + file.name).writeBytes(it) DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let {
FileHelp.getFile(fontFolder + file.name).writeBytes(it)
}
} }
} }
} try {
} val file = File(fontFolder)
file.listFiles { pathName ->
@SuppressLint("DefaultLocale") pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
private fun getFontFiles(): Array<File>? { }?.let {
return try { withContext(Main) {
val file = File(fontFolder) adapter.setItems(it.toList())
file.listFiles { pathName -> }
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) }
} catch (e: Exception) {
toast(e.localizedMessage ?: "")
} }
} catch (e: Exception) {
null
} }
} }

Loading…
Cancel
Save