feat: 优化代码

pull/105/head
kunfei 5 years ago
parent 75c99dbe7d
commit d460a5ede3
  1. 24
      app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
  2. 17
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  3. 3
      app/src/main/res/layout/dialog_font_select.xml

@ -0,0 +1,24 @@
package io.legado.app.base
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
abstract class BaseDialogFragment : DialogFragment(), CoroutineScope {
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
private lateinit var job: Job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
job = Job()
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
}

@ -12,37 +12,30 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.PreferKey
import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.utils.*
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 kotlin.coroutines.CoroutineContext
class FontSelectDialog : DialogFragment(),
class FontSelectDialog : BaseDialogFragment(),
Toolbar.OnMenuItemClickListener,
CoroutineScope,
FontAdapter.CallBack {
private lateinit var job: Job
private val fontFolderRequestCode = 35485
private val fontFolder =
App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator
private val fontCacheFolder by lazy {
FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts")
}
override val coroutineContext: CoroutineContext
get() = job + Main
private var adapter: FontAdapter? = null
override fun onStart() {
@ -57,7 +50,6 @@ class FontSelectDialog : DialogFragment(),
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
job = Job()
return inflater.inflate(R.layout.dialog_font_select, container)
}
@ -101,11 +93,6 @@ class FontSelectDialog : DialogFragment(),
return true
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
private fun openFolder() {
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)

@ -2,8 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"

Loading…
Cancel
Save