From d460a5ede3c4a13999bfdd0d2382099c774d135e Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 19 Feb 2020 10:21:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/base/BaseDialogFragment.kt | 24 +++++++++++++++++++ .../app/ui/widget/font/FontSelectDialog.kt | 17 ++----------- .../main/res/layout/dialog_font_select.xml | 3 +-- 3 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/io/legado/app/base/BaseDialogFragment.kt 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">