This commit is contained in:
kunfei
2023-08-01 09:27:55 +08:00
parent bbea812c6d
commit 5afedc4985
7 changed files with 20 additions and 23 deletions

View File

@@ -14,13 +14,9 @@ import androidx.fragment.app.Fragment
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.MainScope
import kotlinx.coroutines.cancel
@Suppress("MemberVisibilityCanBePrivate")
abstract class BaseFragment(@LayoutRes layoutID: Int) : Fragment(layoutID),
CoroutineScope by MainScope() {
abstract class BaseFragment(@LayoutRes layoutID: Int) : Fragment(layoutID) {
var supportToolbar: Toolbar? = null
private set
@@ -55,11 +51,6 @@ abstract class BaseFragment(@LayoutRes layoutID: Int) : Fragment(layoutID),
}
}
override fun onDestroy() {
super.onDestroy()
cancel()
}
fun setSupportToolbar(toolbar: Toolbar) {
supportToolbar = toolbar
supportToolbar?.let {

View File

@@ -5,6 +5,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
import io.legado.app.data.appDb
@@ -49,7 +50,7 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
override fun upBookmark(searchKey: String?) {
val book = viewModel.bookData.value ?: return
launch {
lifecycleScope.launch {
when {
searchKey.isNullOrBlank() -> appDb.bookmarkDao.flowByBook(book.name, book.author)
else -> appDb.bookmarkDao.flowSearch(book.name, book.author, searchKey)

View File

@@ -7,6 +7,7 @@ import android.graphics.PorterDuff
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.EventBus
@@ -76,7 +77,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
@SuppressLint("SetTextI18n")
private fun initBook(book: Book) {
launch {
lifecycleScope.launch {
upChapterList(null)
durChapterIndex = book.durChapterIndex
binding.tvCurrentChapterInfo.text =
@@ -86,7 +87,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}
private fun initCacheFileNames(book: Book) {
launch(IO) {
lifecycleScope.launch(IO) {
adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book))
withContext(Main) {
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
@@ -114,7 +115,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}
override fun upChapterList(searchKey: String?) {
launch {
lifecycleScope.launch {
withContext(IO) {
when {
searchKey.isNullOrBlank() -> appDb.bookChapterDao.getChapterList(viewModel.bookUrl)
@@ -127,7 +128,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}
override fun onListChanged() {
launch {
lifecycleScope.launch {
var scrollPos = 0
withContext(Default) {
adapter.getItems().forEachIndexed { index, bookChapter ->
@@ -148,7 +149,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}
override val scope: CoroutineScope
get() = this
get() = lifecycleScope
override val book: Book?
get() = viewModel.bookData.value

View File

@@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.View
import androidx.core.view.isGone
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@@ -133,7 +134,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
*/
private fun upRecyclerData() {
booksFlowJob?.cancel()
booksFlowJob = launch {
booksFlowJob = lifecycleScope.launch {
appDb.bookDao.flowByGroup(groupId).map { list ->
//排序
when (bookSort) {
@@ -192,7 +193,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
if (!AppConfig.showLastUpdateTime) {
return
}
upLastUpdateTimeJob = launch {
upLastUpdateTimeJob = lifecycleScope.launch {
while (isActive) {
if (SystemUtils.isScreenOn()) {
booksAdapter.upLastUpdateTime()

View File

@@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.core.view.isGone
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@@ -133,7 +134,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
}
}
booksFlowJob?.cancel()
booksFlowJob = launch {
booksFlowJob = lifecycleScope.launch {
appDb.bookDao.flowByGroup(groupId).map { list ->
//排序
when (AppConfig.getBookSortByGroupId(groupId)) {

View File

@@ -129,7 +129,7 @@ class ExploreFragment() : VMBaseFragment<ExploreViewModel>(R.layout.fragment_exp
}
private fun initGroupData() {
launch {
lifecycleScope.launch {
appDb.bookSourceDao.flowExploreGroups().flowOn(IO).conflate().collect {
groups.clear()
groups.addAll(it)
@@ -141,7 +141,7 @@ class ExploreFragment() : VMBaseFragment<ExploreViewModel>(R.layout.fragment_exp
private fun upExploreData(searchKey: String? = null, once: Boolean = false) {
this.searchKey = searchKey
exploreFlowJob?.cancel()
exploreFlowJob = launch {
exploreFlowJob = lifecycleScope.launch {
when {
searchKey.isNullOrBlank() -> {
appDb.bookSourceDao.flowExplore()

View File

@@ -7,6 +7,7 @@ import android.view.SubMenu
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.AppLog
@@ -139,7 +140,7 @@ class RssFragment() : VMBaseFragment<RssViewModel>(R.layout.fragment_rss),
private fun initGroupData() {
groupsFlowJob?.cancel()
groupsFlowJob = launch {
groupsFlowJob = lifecycleScope.launch {
appDb.rssSourceDao.flowGroupEnabled().conflate().collect {
groups.clear()
it.map { group ->
@@ -152,13 +153,14 @@ class RssFragment() : VMBaseFragment<RssViewModel>(R.layout.fragment_rss),
private fun upRssFlowJob(searchKey: String? = null) {
rssFlowJob?.cancel()
rssFlowJob = launch {
rssFlowJob = lifecycleScope.launch {
when {
searchKey.isNullOrEmpty() -> appDb.rssSourceDao.flowEnabled()
searchKey.startsWith("group:") -> {
val key = searchKey.substringAfter("group:")
appDb.rssSourceDao.flowEnabledByGroup(key)
}
else -> appDb.rssSourceDao.flowEnabled(searchKey)
}.catch {
AppLog.put("订阅界面更新数据出错", it)