diff --git a/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt b/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt new file mode 100644 index 000000000..8bb119625 --- /dev/null +++ b/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt @@ -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() + } +} \ No newline at end of file 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 636aa8a32..8042afbd8 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 @@ -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) diff --git a/app/src/main/res/layout/dialog_font_select.xml b/app/src/main/res/layout/dialog_font_select.xml index e4148f763..1e9cf6b42 100644 --- a/app/src/main/res/layout/dialog_font_select.xml +++ b/app/src/main/res/layout/dialog_font_select.xml @@ -2,8 +2,7 @@ + android:layout_height="wrap_content">