From 80529cd2f8827715c03f5692c7b285dce35d1149 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 26 Jul 2021 11:28:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/base/BaseDialogFragment.kt | 15 +++------- .../java/io/legado/app/base/BaseFragment.kt | 28 ++++++------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt b/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt index 468dd4c95..555ef1306 100644 --- a/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt +++ b/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt @@ -8,19 +8,12 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.theme.ThemeStore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel 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() - } +abstract class BaseDialogFragment : DialogFragment(), CoroutineScope by MainScope() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -41,7 +34,7 @@ abstract class BaseDialogFragment : DialogFragment(), CoroutineScope { override fun onDestroy() { super.onDestroy() - job.cancel() + cancel() } fun execute( diff --git a/app/src/main/java/io/legado/app/base/BaseFragment.kt b/app/src/main/java/io/legado/app/base/BaseFragment.kt index 3b3f3dd28..c95a5ab67 100644 --- a/app/src/main/java/io/legado/app/base/BaseFragment.kt +++ b/app/src/main/java/io/legado/app/base/BaseFragment.kt @@ -3,7 +3,10 @@ package io.legado.app.base import android.annotation.SuppressLint import android.content.res.Configuration import android.os.Bundle -import android.view.* +import android.view.Menu +import android.view.MenuInflater +import android.view.MenuItem +import android.view.View import androidx.appcompat.view.SupportMenuInflater import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment @@ -11,14 +14,13 @@ import io.legado.app.R import io.legado.app.ui.widget.TitleBar import io.legado.app.utils.applyTint import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlin.coroutines.CoroutineContext +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel @Suppress("MemberVisibilityCanBePrivate") abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), - CoroutineScope { - lateinit var job: Job + CoroutineScope by MainScope() { + var supportToolbar: Toolbar? = null private set @@ -26,18 +28,6 @@ abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), @SuppressLint("RestrictedApi") get() = SupportMenuInflater(requireContext()) - override val coroutineContext: CoroutineContext - get() = job + Dispatchers.Main - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - job = Job() - return super.onCreateView(inflater, container, savedInstanceState) - } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) onMultiWindowModeChanged() @@ -66,7 +56,7 @@ abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), override fun onDestroy() { super.onDestroy() - job.cancel() + cancel() } fun setSupportToolbar(toolbar: Toolbar) {