pull/1238/head
gedoor 3 years ago
parent 35f51e8f51
commit 80529cd2f8
  1. 15
      app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
  2. 28
      app/src/main/java/io/legado/app/base/BaseFragment.kt

@ -8,19 +8,12 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.ThemeStore
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
abstract class BaseDialogFragment : DialogFragment(), CoroutineScope { abstract class BaseDialogFragment : DialogFragment(), CoroutineScope by MainScope() {
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 onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -41,7 +34,7 @@ abstract class BaseDialogFragment : DialogFragment(), CoroutineScope {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
job.cancel() cancel()
} }
fun <T> execute( fun <T> execute(

@ -3,7 +3,10 @@ package io.legado.app.base
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Bundle 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.view.SupportMenuInflater
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment 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.ui.widget.TitleBar
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope
import kotlinx.coroutines.Job import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext
@Suppress("MemberVisibilityCanBePrivate") @Suppress("MemberVisibilityCanBePrivate")
abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), abstract class BaseFragment(layoutID: Int) : Fragment(layoutID),
CoroutineScope { CoroutineScope by MainScope() {
lateinit var job: Job
var supportToolbar: Toolbar? = null var supportToolbar: Toolbar? = null
private set private set
@ -26,18 +28,6 @@ abstract class BaseFragment(layoutID: Int) : Fragment(layoutID),
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
get() = SupportMenuInflater(requireContext()) 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?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
onMultiWindowModeChanged() onMultiWindowModeChanged()
@ -66,7 +56,7 @@ abstract class BaseFragment(layoutID: Int) : Fragment(layoutID),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
job.cancel() cancel()
} }
fun setSupportToolbar(toolbar: Toolbar) { fun setSupportToolbar(toolbar: Toolbar) {

Loading…
Cancel
Save