feat: 优化代码

pull/121/head
kunfei 5 years ago
parent 98b75d547b
commit ae3581d29c
  1. 9
      app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
  2. 4
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -2,6 +2,7 @@ package io.legado.app.base
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import io.legado.app.help.coroutine.Coroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@ -21,4 +22,12 @@ abstract class BaseDialogFragment : DialogFragment(), CoroutineScope {
super.onDestroy()
job.cancel()
}
fun <T> execute(
scope: CoroutineScope = this,
context: CoroutineContext = Dispatchers.IO,
block: suspend CoroutineScope.() -> T
): Coroutine<T> {
return Coroutine.async(scope, context) { block() }
}
}

@ -147,7 +147,7 @@ class FontSelectDialog : BaseDialogFragment(),
@SuppressLint("DefaultLocale")
private fun getFontFiles(doc: DocumentFile) {
launch(IO) {
execute {
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri)
fontCacheFolder.listFiles()?.forEach { fontFile ->
var contain = false
@ -182,6 +182,8 @@ class FontSelectDialog : BaseDialogFragment(),
} catch (e: Exception) {
toast(e.localizedMessage ?: "")
}
}.onError {
toast("getFontFiles:${it.localizedMessage}")
}
}

Loading…
Cancel
Save