From df077da03f03b6c09c568c95034cd14809790616 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 21 Jul 2021 09:09:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E7=94=A8Flow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/base/BaseActivity.kt | 11 +------ .../io/legado/app/base/BaseDialogFragment.kt | 20 ++---------- .../java/io/legado/app/base/BaseFragment.kt | 31 ++++--------------- .../java/io/legado/app/base/BaseViewModel.kt | 13 ++++---- .../io/legado/app/data/dao/BookGroupDao.kt | 11 +++---- .../legado/app/ui/about/ReadRecordActivity.kt | 5 +-- .../ui/book/arrange/ArrangeBookActivity.kt | 23 +++++++------- .../legado/app/ui/book/cache/CacheActivity.kt | 25 +++++++-------- .../book/changecover/ChangeCoverViewModel.kt | 3 +- .../book/changesource/ChangeSourceDialog.kt | 3 +- .../changesource/ChangeSourceViewModel.kt | 7 +++-- .../ui/book/explore/ExploreShowViewModel.kt | 3 +- .../app/ui/book/group/GroupManageDialog.kt | 11 +++++-- .../app/ui/book/group/GroupSelectDialog.kt | 11 +++++-- .../app/ui/book/info/BookInfoActivity.kt | 3 +- .../app/ui/book/local/ImportBookActivity.kt | 11 ++++--- .../app/ui/book/read/ReadBookActivity.kt | 15 ++++----- .../app/ui/book/read/ReadBookViewModel.kt | 5 +-- .../app/ui/book/read/config/TocRegexDialog.kt | 7 +++-- .../app/ui/book/search/SearchActivity.kt | 7 +++-- .../app/ui/book/search/SearchViewModel.kt | 5 +-- .../searchContent/SearchContentActivity.kt | 5 +-- .../source/debug/BookSourceDebugActivity.kt | 3 +- .../book/source/manage/BookSourceActivity.kt | 5 +-- .../book/source/manage/GroupManageDialog.kt | 3 +- .../app/ui/book/toc/BookmarkFragment.kt | 3 +- .../app/ui/book/toc/ChapterListFragment.kt | 7 +++-- .../bookshelf/style1/BookshelfFragment1.kt | 10 +++--- .../bookshelf/style1/books/BooksFragment.kt | 3 +- .../bookshelf/style2/BookshelfFragment2.kt | 8 ++--- .../app/ui/main/explore/ExploreFragment.kt | 7 +++-- .../io/legado/app/ui/main/rss/RssFragment.kt | 5 +-- .../app/ui/rss/article/RssArticlesFragment.kt | 3 +- .../ui/rss/article/RssArticlesViewModel.kt | 5 +-- .../ui/rss/favorites/RssFavoritesActivity.kt | 3 +- .../legado/app/ui/rss/read/ReadRssActivity.kt | 3 +- .../app/ui/rss/read/ReadRssViewModel.kt | 8 ++--- .../source/debug/RssSourceDebugActivity.kt | 3 +- .../rss/source/debug/RssSourceDebugModel.kt | 3 +- .../ui/rss/source/manage/GroupManageDialog.kt | 3 +- .../ui/rss/source/manage/RssSourceActivity.kt | 5 +-- .../ui/rss/subscription/RuleSubActivity.kt | 9 +++--- .../legado/app/ui/widget/dialog/TextDialog.kt | 3 +- .../app/ui/widget/font/FontSelectDialog.kt | 3 +- 44 files changed, 168 insertions(+), 172 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 6406c18bf..bdfa48514 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -24,9 +24,6 @@ import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.widget.TitleBar import io.legado.app.utils.* -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.MainScope -import kotlinx.coroutines.cancel abstract class BaseActivity( @@ -35,8 +32,7 @@ abstract class BaseActivity( private val toolBarTheme: Theme = Theme.Auto, private val transparent: Boolean = false, private val imageBg: Boolean = true -) : AppCompatActivity(), - CoroutineScope by MainScope() { +) : AppCompatActivity() { protected abstract val binding: VB @@ -120,11 +116,6 @@ abstract class BaseActivity( setupSystemBar() } - override fun onDestroy() { - super.onDestroy() - cancel() - } - abstract fun onActivityCreated(savedInstanceState: Bundle?) final override fun onCreateOptionsMenu(menu: Menu?): Boolean { 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..ce7f3514f 100644 --- a/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt +++ b/app/src/main/java/io/legado/app/base/BaseDialogFragment.kt @@ -4,23 +4,14 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentManager +import androidx.lifecycle.lifecycleScope 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 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() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -39,13 +30,8 @@ abstract class BaseDialogFragment : DialogFragment(), CoroutineScope { } } - override fun onDestroy() { - super.onDestroy() - job.cancel() - } - fun execute( - scope: CoroutineScope = this, + scope: CoroutineScope = lifecycleScope, context: CoroutineContext = Dispatchers.IO, block: suspend CoroutineScope.() -> T ) = Coroutine.async(scope, context) { block() } 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..fba8ef6b2 100644 --- a/app/src/main/java/io/legado/app/base/BaseFragment.kt +++ b/app/src/main/java/io/legado/app/base/BaseFragment.kt @@ -3,22 +3,20 @@ 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 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 @Suppress("MemberVisibilityCanBePrivate") -abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), - CoroutineScope { - lateinit var job: Job +abstract class BaseFragment(layoutID: Int) : Fragment(layoutID) { + var supportToolbar: Toolbar? = null private set @@ -26,18 +24,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() @@ -64,11 +50,6 @@ abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), } } - override fun onDestroy() { - super.onDestroy() - job.cancel() - } - fun setSupportToolbar(toolbar: Toolbar) { supportToolbar = toolbar supportToolbar?.let { diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index 88ddc4b5e..6c7a8c782 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -4,20 +4,22 @@ import android.app.Application import android.content.Context import androidx.annotation.CallSuper import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope import io.legado.app.App import io.legado.app.help.coroutine.Coroutine import io.legado.app.utils.toastOnUi -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.Dispatchers import kotlin.coroutines.CoroutineContext @Suppress("unused") -open class BaseViewModel(application: Application) : AndroidViewModel(application), - CoroutineScope by MainScope() { +open class BaseViewModel(application: Application) : AndroidViewModel(application) { val context: Context by lazy { this.getApplication() } fun execute( - scope: CoroutineScope = this, + scope: CoroutineScope = viewModelScope, context: CoroutineContext = Dispatchers.IO, block: suspend CoroutineScope.() -> T ): Coroutine { @@ -25,7 +27,7 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio } fun submit( - scope: CoroutineScope = this, + scope: CoroutineScope = viewModelScope, context: CoroutineContext = Dispatchers.IO, block: suspend CoroutineScope.() -> Deferred ): Coroutine { @@ -35,7 +37,6 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio @CallSuper override fun onCleared() { super.onCleared() - cancel() } open fun toastOnUi(message: Int) { diff --git a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt index 34e6a3e88..51d73542e 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt @@ -1,9 +1,9 @@ package io.legado.app.data.dao -import androidx.lifecycle.LiveData import androidx.room.* import io.legado.app.constant.BookType import io.legado.app.data.entities.BookGroup +import kotlinx.coroutines.flow.Flow @Dao interface BookGroupDao { @@ -15,7 +15,7 @@ interface BookGroupDao { fun getByName(groupName: String): BookGroup? @Query("SELECT * FROM book_groups ORDER BY `order`") - fun liveDataAll(): LiveData> + fun liveDataAll(): Flow> @Query( """ @@ -24,13 +24,12 @@ interface BookGroupDao { or (groupId = -3 and show > 0 and (select count(bookUrl) from books where type = ${BookType.audio}) > 0) or (groupId = -2 and show > 0 and (select count(bookUrl) from books where origin = '${BookType.local}') > 0) or (groupId = -1 and show > 0) - ORDER BY `order` - """ + ORDER BY `order`""" ) - fun liveDataShow(): LiveData> + fun liveDataShow(): Flow> @Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`") - fun liveDataSelect(): LiveData> + fun liveDataSelect(): Flow> @get:Query("SELECT sum(groupId) FROM book_groups where groupId >= 0") val idsSum: Long diff --git a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt index 8f49e6cd7..4a68f7f43 100644 --- a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.base.adapter.ItemViewHolder @@ -72,7 +73,7 @@ class ReadRecordActivity : BaseActivity() { } private fun initData() { - launch(IO) { + lifecycleScope.launch(IO) { val allTime = appDb.readRecordDao.allTime withContext(Main) { binding.readRecord.tvReadTime.text = formatDuring(allTime) @@ -115,7 +116,7 @@ class ReadRecordActivity : BaseActivity() { binding.apply { root.setOnClickListener { val item = getItem(holder.layoutPosition) ?: return@setOnClickListener - launch { + lifecycleScope.launch { val book = withContext(IO) { appDb.bookDao.findByName(item.bookName).firstOrNull() } diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt index 2495f36f0..68c5a7467 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt @@ -5,7 +5,7 @@ import android.view.Menu import android.view.MenuItem import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu -import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R @@ -46,14 +46,13 @@ class ArrangeBookActivity : VMBaseActivity>? = null private var booksFlowJob: Job? = null private var menu: Menu? = null private var groupId: Long = -1 override fun onActivityCreated(savedInstanceState: Bundle?) { groupId = intent.getLongExtra("groupId", -1) - launch { + lifecycleScope.launch { binding.titleBar.subtitle = withContext(IO) { appDb.bookGroupDao.getByID(groupId)?.groupName ?: getString(R.string.no_group) @@ -109,19 +108,19 @@ class ArrangeBookActivity : VMBaseActivity appDb.bookDao.observeAll() AppConst.bookGroupLocalId -> appDb.bookDao.observeLocal() diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 8d24c807b..313ba092b 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -4,7 +4,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.activity.viewModels -import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar import io.legado.app.R @@ -46,7 +46,6 @@ class CacheActivity : VMBaseActivity() private val exportBookPathKey = "exportBookPath" lateinit var adapter: CacheAdapter - private var groupLiveData: LiveData>? = null private var booksFlowJob: Job? = null private var menu: Menu? = null private var exportPosition = -1 @@ -68,7 +67,7 @@ class CacheActivity : VMBaseActivity() override fun onActivityCreated(savedInstanceState: Bundle?) { groupId = intent.getLongExtra("groupId", -1) - launch { + lifecycleScope.launch { binding.titleBar.subtitle = withContext(IO) { appDb.bookGroupDao.getByID(groupId)?.groupName ?: getString(R.string.no_group) @@ -150,7 +149,7 @@ class CacheActivity : VMBaseActivity() private fun initBookData() { booksFlowJob?.cancel() - booksFlowJob = launch { + booksFlowJob = lifecycleScope.launch { when (groupId) { AppConst.bookGroupAllId -> appDb.bookDao.observeAll() AppConst.bookGroupLocalId -> appDb.bookDao.observeLocal() @@ -176,18 +175,18 @@ class CacheActivity : VMBaseActivity() } private fun initGroupData() { - groupLiveData?.removeObservers(this) - groupLiveData = appDb.bookGroupDao.liveDataAll() - groupLiveData?.observe(this, { - groupList.clear() - groupList.addAll(it) - adapter.notifyDataSetChanged() - upMenu() - }) + lifecycleScope.launch { + appDb.bookGroupDao.liveDataAll().collect { + groupList.clear() + groupList.addAll(it) + adapter.notifyDataSetChanged() + upMenu() + } + } } private fun initCacheSize(books: List) { - launch(IO) { + lifecycleScope.launch(IO) { books.forEach { book -> val chapterCaches = hashSetOf() val cacheNames = BookHelp.getChapterFiles(book) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 13488567b..cdd67fb02 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern import io.legado.app.data.appDb @@ -102,7 +103,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application return } val task = WebBook(source) - .searchBook(this, name, context = searchPool!!) + .searchBook(viewModelScope, name, context = searchPool!!) .timeout(60000L) .onSuccess(Dispatchers.IO) { if (it.isNotEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index 969217fd8..53cf693d9 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -6,6 +6,7 @@ import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R @@ -150,7 +151,7 @@ class ChangeSourceDialog : BaseDialogFragment(), viewModel.searchBooksLiveData.observe(viewLifecycleOwner, { adapter.setItems(it) }) - launch { + lifecycleScope.launch { appDb.bookSourceDao.liveGroupEnabled().collect { groups.clear() it.map { group -> diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 6197725bd..7fa050b36 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern import io.legado.app.constant.PreferKey @@ -129,7 +130,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio val source = bookSourceList[searchIndex] val webBook = WebBook(source) val task = webBook - .searchBook(this, name, context = searchPool!!) + .searchBook(viewModelScope, name, context = searchPool!!) .timeout(60000L) .onSuccess(IO) { it.forEach { searchBook -> @@ -168,7 +169,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } private fun loadBookInfo(webBook: WebBook, book: Book) { - webBook.getBookInfo(this, book) + webBook.getBookInfo(viewModelScope, book) .onSuccess { if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) { loadBookToc(webBook, book) @@ -184,7 +185,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } private fun loadBookToc(webBook: WebBook, book: Book) { - webBook.getChapterList(this, book) + webBook.getChapterList(viewModelScope, book) .onSuccess(IO) { chapters -> if (chapters.isNotEmpty()) { book.latestChapterTitle = chapters.last().title diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt index 9fedbd613..9bdd8cffa 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.book.explore import android.app.Application import android.content.Intent import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource @@ -34,7 +35,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application val source = bookSource val url = exploreUrl if (source != null && url != null) { - WebBook(source).exploreBook(this, url, page) + WebBook(source).exploreBook(viewModelScope, url, page) .timeout(30000L) .onSuccess(IO) { searchBooks -> booksData.postValue(searchBooks) diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index 33a3d55ca..e7a6a6989 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -32,6 +33,8 @@ import io.legado.app.utils.getSize import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.visible +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { @@ -77,9 +80,11 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener } private fun initData() { - appDb.bookGroupDao.liveDataAll().observe(viewLifecycleOwner, { - adapter.setItems(it) - }) + lifecycleScope.launch { + appDb.bookGroupDao.liveDataAll().collect { + adapter.setItems(it) + } + } } private fun initMenu() { diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index fdb1cfd11..a4b0e33b1 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -10,6 +10,7 @@ import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -32,6 +33,8 @@ import io.legado.app.utils.applyTint import io.legado.app.utils.getSize import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { @@ -105,9 +108,11 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener } private fun initData() { - appDb.bookGroupDao.liveDataSelect().observe(viewLifecycleOwner, { - adapter.setItems(it) - }) + lifecycleScope.launch { + appDb.bookGroupDao.liveDataSelect().collect { + adapter.setItems(it) + } + } } override fun onMenuItemClick(item: MenuItem?): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index cfdf84d02..5497fe12f 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -10,6 +10,7 @@ import android.widget.CheckBox import android.widget.LinearLayout import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels +import androidx.lifecycle.lifecycleScope import com.bumptech.glide.RequestBuilder import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions.bitmapTransform @@ -53,7 +54,7 @@ class BookInfoActivity : private val tocActivityResult = registerForActivityResult(TocActivityResult()) { it?.let { viewModel.bookData.value?.let { book -> - launch { + lifecycleScope.launch { withContext(IO) { viewModel.durChapterIndex = it.first book.durChapterIndex = it.first diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index 02388239e..4b18d3b1e 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -9,6 +9,7 @@ import android.view.MenuItem import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -124,7 +125,7 @@ class ImportBookActivity : VMBaseActivity Unit = { - launch { + lifecycleScope.launch { adapter.addItem(it) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index ce1f7e710..354d665e9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -13,6 +13,7 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.get import androidx.core.view.isVisible import androidx.core.view.size +import androidx.lifecycle.lifecycleScope import com.jaredrummler.android.colorpicker.ColorPickerDialogListener import io.legado.app.BuildConfig import io.legado.app.R @@ -108,7 +109,7 @@ class ReadBookActivity : ReadBookBaseActivity(), TextActionMenu(this, this) } - override val scope: CoroutineScope get() = this + override val scope: CoroutineScope get() = lifecycleScope override val isInitFinish: Boolean get() = viewModel.isInitFinish override val isScroll: Boolean get() = binding.readView.isScroll private val mHandler = Handler(Looper.getMainLooper()) @@ -591,7 +592,7 @@ class ReadBookActivity : ReadBookBaseActivity(), resetPageOffset: Boolean, success: (() -> Unit)? ) { - launch { + lifecycleScope.launch { autoPageProgress = 0 binding.readView.upContent(relativePosition, resetPageOffset) binding.readMenu.setSeekPage(ReadBook.durPageIndex()) @@ -604,13 +605,13 @@ class ReadBookActivity : ReadBookBaseActivity(), * 更新视图 */ override fun upView() { - launch { + lifecycleScope.launch { binding.readMenu.upBookView() } } override fun upPageAnim() { - launch { + lifecycleScope.launch { binding.readView.upPageAnim() } } @@ -620,7 +621,7 @@ class ReadBookActivity : ReadBookBaseActivity(), */ override fun pageChanged() { autoPageProgress = 0 - launch { + lifecycleScope.launch { binding.readMenu.setSeekPage(ReadBook.durPageIndex()) } mHandler.postDelayed(backupRunnable, 600000) @@ -846,7 +847,7 @@ class ReadBookActivity : ReadBookBaseActivity(), viewModel.searchContentQuery ) ReadBook.skipToPage(positions[0]) { - launch { + lifecycleScope.launch { binding.readView.curPage.selectStartMoveIndex(0, positions[1], positions[2]) delay(20L) when (positions[3]) { @@ -936,7 +937,7 @@ class ReadBookActivity : ReadBookBaseActivity(), } } observeEventSticky(EventBus.TTS_PROGRESS) { chapterStart -> - launch(IO) { + lifecycleScope.launch(IO) { if (BaseReadAloudService.isPlay()) { ReadBook.curTextChapter?.let { textChapter -> val pageStart = chapterStart - ReadBook.durChapterPos diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 6fe2feaec..92aeb6ca2 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.book.read import android.app.Application import android.content.Intent +import androidx.lifecycle.viewModelScope import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb @@ -104,7 +105,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (book.isLocalBook()) { loadChapterList(book, changeDruChapterIndex) } else { - ReadBook.webBook?.getBookInfo(this, book, canReName = false) + ReadBook.webBook?.getBookInfo(viewModelScope, book, canReName = false) ?.onSuccess { loadChapterList(book, changeDruChapterIndex) } @@ -133,7 +134,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.upMsg("LoadTocError:${it.localizedMessage}") } } else { - ReadBook.webBook?.getChapterList(this, book) + ReadBook.webBook?.getChapterList(viewModelScope, book) ?.onSuccess(IO) { cList -> if (cList.isNotEmpty()) { if (changeDruChapterIndex == null) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index 136ae587a..54755d1a5 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -11,6 +11,7 @@ import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView import com.google.android.material.snackbar.Snackbar @@ -222,7 +223,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { if (buttonView.isPressed) { getItem(holder.layoutPosition)?.let { it.enable = isChecked - launch(IO) { + lifecycleScope.launch(IO) { appDb.txtTocRuleDao.update(it) } } @@ -233,7 +234,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { } ivDelete.setOnClickListener { getItem(holder.layoutPosition)?.let { item -> - launch(IO) { + lifecycleScope.launch(IO) { appDb.txtTocRuleDao.delete(item) } } @@ -255,7 +256,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { for ((index, item) in getItems().withIndex()) { item.serialNumber = index + 1 } - launch(IO) { + lifecycleScope.launch(IO) { appDb.txtTocRuleDao.update(*getItems().toTypedArray()) } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 99ad24340..afed9c5ab 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -11,6 +11,7 @@ import android.widget.TextView import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.FlexboxLayoutManager @@ -192,7 +193,7 @@ class SearchActivity : VMBaseActivity @@ -273,7 +274,7 @@ class SearchActivity : VMBaseActivity { searchView.setQuery(key, true) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index 6a88d649d..9b2f9999e 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import android.os.Handler import android.os.Looper import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.constant.PreferKey import io.legado.app.data.appDb @@ -17,7 +18,7 @@ import kotlinx.coroutines.isActive class SearchViewModel(application: Application) : BaseViewModel(application), SearchBookModel.CallBack { val handler = Handler(Looper.getMainLooper()) - private val searchBookModel = SearchBookModel(this, this) + private val searchBookModel = SearchBookModel(viewModelScope, this) var isSearchLiveData = MutableLiveData() var searchBookLiveData = MutableLiveData>() var searchKey: String = "" @@ -64,7 +65,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application), override fun onSearchSuccess(searchBooks: ArrayList) { val precision = context.getPrefBoolean(PreferKey.precisionSearch) appDb.searchBookDao.insert(*searchBooks.toTypedArray()) - mergeItems(this, searchBooks, precision) + mergeItems(viewModelScope, searchBooks, precision) } override fun onSearchFinish() { diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt index 77382d7c5..1624fd618 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.os.Bundle import androidx.activity.viewModels import androidx.appcompat.widget.SearchView +import androidx.lifecycle.lifecycleScope import com.github.liuyueyi.quick.transfer.ChineseUtils import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -115,7 +116,7 @@ class SearchContentActivity : } private fun initCacheFileNames(book: Book) { - launch(Dispatchers.IO) { + lifecycleScope.launch(Dispatchers.IO) { adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book)) withContext(Dispatchers.Main) { adapter.notifyItemRangeChanged(0, adapter.itemCount, true) @@ -144,7 +145,7 @@ class SearchContentActivity : searchResultCounts = 0 viewModel.lastQuery = newText var searchResults = listOf() - launch(Dispatchers.Main) { + lifecycleScope.launch(Dispatchers.Main) { appDb.bookChapterDao.getChapterList(viewModel.bookUrl).map { chapter -> withContext(Dispatchers.IO) { if (isLocalBook diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt index 43c2ba1ae..a300935cf 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt @@ -5,6 +5,7 @@ import android.view.Menu import android.view.MenuItem import androidx.activity.viewModels import androidx.appcompat.widget.SearchView +import androidx.lifecycle.lifecycleScope import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivitySourceDebugBinding @@ -36,7 +37,7 @@ class BookSourceDebugActivity : VMBaseActivity - launch { + lifecycleScope.launch { adapter.addItem(msg) if (state == -1 || state == 1000) { binding.rotateLoading.hide() diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 60e91cb94..8f8f6fc41 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -10,6 +10,7 @@ import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import com.google.android.material.snackbar.Snackbar import io.legado.app.R @@ -203,7 +204,7 @@ class BookSourceActivity : VMBaseActivity { appDb.bookSourceDao.liveDataAll() @@ -275,7 +276,7 @@ class BookSourceActivity : VMBaseActivity() it.map { group -> diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index a0933af30..2f81b0a04 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt @@ -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 androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseFragment @@ -50,7 +51,7 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark override fun upBookmark(searchKey: String?) { val book = viewModel.bookData.value ?: return bookmarkFlowJob?.cancel() - bookmarkFlowJob = launch { + bookmarkFlowJob = lifecycleScope.launch { when { searchKey.isNullOrBlank() -> appDb.bookmarkDao.observeByBook(book.name, book.author) else -> appDb.bookmarkDao.liveDataSearch(book.name, book.author, searchKey) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index feef7aa82..6143cf456 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -6,6 +6,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.constant.EventBus @@ -77,7 +78,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt @SuppressLint("SetTextI18n") private fun initBook(book: Book) { - launch { + lifecycleScope.launch { upChapterList(null) durChapterIndex = book.durChapterIndex binding.tvCurrentChapterInfo.text = @@ -87,7 +88,7 @@ class ChapterListFragment : VMBaseFragment(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) @@ -108,7 +109,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt override fun upChapterList(searchKey: String?) { tocFlowJob?.cancel() - tocFlowJob = launch { + tocFlowJob = lifecycleScope.launch { when { searchKey.isNullOrBlank() -> appDb.bookChapterDao.observeByBook(viewModel.bookUrl) else -> appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, searchKey) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt index 4f6ce4978..ea76c3efc 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt @@ -10,7 +10,7 @@ import androidx.appcompat.widget.SearchView import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter -import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import com.google.android.material.tabs.TabLayout import io.legado.app.R import io.legado.app.constant.AppConst @@ -28,6 +28,8 @@ import io.legado.app.utils.getPrefInt import io.legado.app.utils.putPrefInt import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch /** * 书架界面 @@ -39,7 +41,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), private val binding by viewBinding(FragmentBookshelfBinding::bind) private lateinit var adapter: FragmentStatePagerAdapter private lateinit var tabLayout: TabLayout - private var bookGroupLiveData: LiveData>? = null private val bookGroups = mutableListOf() private val fragmentMap = hashMapOf() @@ -77,9 +78,8 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), } private fun initBookGroupData() { - bookGroupLiveData?.removeObservers(viewLifecycleOwner) - bookGroupLiveData = appDb.bookGroupDao.liveDataShow().apply { - observe(viewLifecycleOwner) { + lifecycleScope.launch { + appDb.bookGroupDao.liveDataShow().collect { viewModel.checkGroup(it) upGroup(it) } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt index 70ae87acc..8fd8b4199 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt @@ -4,6 +4,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 @@ -104,7 +105,7 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), private fun upRecyclerData() { booksFlowJob?.cancel() - booksFlowJob = launch { + booksFlowJob = lifecycleScope.launch { when (groupId) { AppConst.bookGroupAllId -> appDb.bookDao.observeAll() AppConst.bookGroupLocalId -> appDb.bookDao.observeLocal() diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt index d3627eb23..5b8953317 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt @@ -6,6 +6,7 @@ import android.view.View import androidx.appcompat.widget.SearchView import androidx.core.view.isGone import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -121,9 +122,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), } private fun initGroupData() { - bookGroupLiveData?.removeObservers(this) - bookGroupLiveData = appDb.bookGroupDao.liveDataShow().apply { - observe(viewLifecycleOwner) { + lifecycleScope.launch { + appDb.bookGroupDao.liveDataShow().collect { if (it != bookGroups) { bookGroups = it booksAdapter.notifyDataSetChanged() @@ -134,7 +134,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), private fun initBooksData() { booksFlowJob?.cancel() - booksFlowJob = launch { + booksFlowJob = lifecycleScope.launch { when (groupId) { AppConst.bookGroupAllId -> appDb.bookDao.observeAll() AppConst.bookGroupLocalId -> appDb.bookDao.observeLocal() diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index 606616dab..630627b39 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -8,6 +8,7 @@ import android.view.View import androidx.appcompat.widget.SearchView import androidx.core.view.isGone import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -87,7 +88,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo ATH.applyEdgeEffectColor(binding.rvFind) linearLayoutManager = LinearLayoutManager(context) binding.rvFind.layoutManager = linearLayoutManager - adapter = ExploreAdapter(requireContext(), this, this) + adapter = ExploreAdapter(requireContext(), lifecycleScope, this) binding.rvFind.adapter = adapter adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { @@ -101,7 +102,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo } private fun initGroupData() { - launch { + lifecycleScope.launch { appDb.bookSourceDao.liveExploreGroup() .collect { groups.clear() @@ -115,7 +116,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo private fun upExploreData(searchKey: String? = null) { exploreFlowJob?.cancel() - exploreFlowJob = launch { + exploreFlowJob = lifecycleScope.launch { val exploreFlow = when { searchKey.isNullOrBlank() -> { appDb.bookSourceDao.liveExplore() diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index 0d20d7252..5cfad3b31 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -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.AppPattern @@ -120,7 +121,7 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), } private fun initGroupData() { - launch { + lifecycleScope.launch { appDb.rssSourceDao.liveGroup().collect { groups.clear() it.map { group -> @@ -133,7 +134,7 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), private fun upRssFlowJob(searchKey: String? = null) { rssFlowJob?.cancel() - rssFlowJob = launch { + rssFlowJob = lifecycleScope.launch { when { searchKey.isNullOrEmpty() -> appDb.rssSourceDao.liveEnabled() searchKey.startsWith("group:") -> { diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt index 228ac9df9..737a93dba 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -96,7 +97,7 @@ class RssArticlesFragment : VMBaseFragment(R.layout.fragme private fun initData() { val rssUrl = activityViewModel.url ?: return articlesFlowJob?.cancel() - articlesFlowJob = launch { + articlesFlowJob = lifecycleScope.launch { appDb.rssArticleDao.liveByOriginSort(rssUrl, viewModel.sortName).collect { adapter.setItems(it) } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt index 1f81c4ba2..0c672841f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.rss.article import android.app.Application import android.os.Bundle import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.RssArticle @@ -30,7 +31,7 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application fun loadContent(rssSource: RssSource) { isLoading = true page = 1 - Rss.getArticles(this, sortName, sortUrl, rssSource, page) + Rss.getArticles(viewModelScope, sortName, sortUrl, rssSource, page) .onSuccess(Dispatchers.IO) { nextPageUrl = it.nextPageUrl it.articles.let { list -> @@ -60,7 +61,7 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application page++ val pageUrl = nextPageUrl if (!pageUrl.isNullOrEmpty()) { - Rss.getArticles(this, sortName, pageUrl, rssSource, page) + Rss.getArticles(viewModelScope, sortName, pageUrl, rssSource, page) .onSuccess(Dispatchers.IO) { nextPageUrl = it.nextPageUrl loadMoreSuccess(it.articles) diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt index 74710a7f2..4020d4e0a 100644 --- a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.rss.favorites import android.os.Bundle +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.base.BaseActivity import io.legado.app.data.appDb @@ -35,7 +36,7 @@ class RssFavoritesActivity : BaseActivity(), } private fun initData() { - launch { + lifecycleScope.launch { appDb.rssStarDao.liveAll().collect { adapter.setItems(it) } diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 2a6767be9..73bfd6e24 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -12,6 +12,7 @@ import android.view.* import android.webkit.* import androidx.activity.viewModels import androidx.core.view.size +import androidx.lifecycle.lifecycleScope import androidx.webkit.WebSettingsCompat import androidx.webkit.WebViewFeature import io.legado.app.R @@ -231,7 +232,7 @@ class ReadRssActivity : VMBaseActivity } override fun upTtsMenu(isPlaying: Boolean) { - launch { + lifecycleScope.launch { if (isPlaying) { ttsMenuItem?.setIcon(R.drawable.ic_stop_black_24dp) ttsMenuItem?.setTitle(R.string.aloud_stop) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 594a4a543..d36643242 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -9,6 +9,7 @@ import android.util.Base64 import android.webkit.URLUtil import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppConst @@ -25,7 +26,6 @@ import io.legado.app.utils.FileUtils import io.legado.app.utils.isContentScheme import io.legado.app.utils.writeBytes import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.launch import java.io.File import java.util.* @@ -95,7 +95,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), private fun loadContent(rssArticle: RssArticle, ruleContent: String) { rssSource?.let { source -> - Rss.getContent(this, rssArticle, ruleContent, source) + Rss.getContent(viewModelScope, rssArticle, ruleContent, source) .onSuccess(IO) { body -> rssArticle.description = body appDb.rssArticleDao.insert(rssArticle) @@ -189,9 +189,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), ttsInitFinish = true play() } else { - launch { - toastOnUi(R.string.tts_init_failed) - } + toastOnUi(R.string.tts_init_failed) } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt index 2d0e5f302..1a955f495 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt @@ -5,6 +5,7 @@ import android.view.Menu import android.view.MenuItem import android.widget.SearchView import androidx.activity.viewModels +import androidx.lifecycle.lifecycleScope import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivitySourceDebugBinding @@ -29,7 +30,7 @@ class RssSourceDebugActivity : VMBaseActivity - launch { + lifecycleScope.launch { adapter.addItem(msg) if (state == -1 || state == 1000) { binding.rotateLoading.hide() diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt index 9d1a5922a..23892dc6f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.rss.source.debug import android.app.Application +import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.RssSource @@ -31,7 +32,7 @@ class RssSourceDebugModel(application: Application) : BaseViewModel(application) rssSource?.let { start?.invoke() Debug.callback = this - Debug.startDebug(this, it) + Debug.startDebug(viewModelScope, it) } ?: error?.invoke() } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt index 806e8122d..6da833a55 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.activityViewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -68,7 +69,7 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener } private fun initData() { - launch { + lifecycleScope.launch { appDb.rssSourceDao.liveGroup().collect { val groups = linkedSetOf() it.map { group -> diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 23ecb9f17..96a1e9293 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -10,6 +10,7 @@ import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -178,7 +179,7 @@ class RssSourceActivity : VMBaseActivity @@ -230,7 +231,7 @@ class RssSourceActivity : VMBaseActivity { appDb.rssSourceDao.liveAll() diff --git a/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt b/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt index 652a8c94c..1209591d7 100644 --- a/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt @@ -5,6 +5,7 @@ import android.view.Menu import android.view.MenuItem import androidx.core.view.isGone import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import io.legado.app.R import io.legado.app.base.BaseActivity @@ -100,7 +101,7 @@ class RuleSubActivity : BaseActivity(), } customView { alertBinding.root } okButton { - launch { + lifecycleScope.launch { ruleSub.type = alertBinding.spType.selectedItemPosition ruleSub.name = alertBinding.etName.text?.toString() ?: "" ruleSub.url = alertBinding.etUrl.text?.toString() ?: "" @@ -121,19 +122,19 @@ class RuleSubActivity : BaseActivity(), } override fun delSubscription(ruleSub: RuleSub) { - launch(IO) { + lifecycleScope.launch(IO) { appDb.ruleSubDao.delete(ruleSub) } } override fun updateSourceSub(vararg ruleSub: RuleSub) { - launch(IO) { + lifecycleScope.launch(IO) { appDb.ruleSubDao.update(*ruleSub) } } override fun upOrder() { - launch(IO) { + lifecycleScope.launch(IO) { val sourceSubs = appDb.ruleSubDao.all for ((index: Int, ruleSub: RuleSub) in sourceSubs.withIndex()) { ruleSub.customOrder = index + 1 diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt index 8c1df6110..5773988eb 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt @@ -5,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.FragmentManager +import androidx.lifecycle.lifecycleScope import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.databinding.DialogTextViewBinding @@ -79,7 +80,7 @@ class TextDialog : BaseDialogFragment() { } if (time > 0) { binding.badgeView.setBadgeCount((time / 1000).toInt()) - launch { + lifecycleScope.launch { while (time > 0) { delay(1000) time -= 1000 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 22f205101..ad653b60d 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 @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -126,7 +127,7 @@ class FontSelectDialog : BaseDialogFragment(), } private fun openFolder() { - launch(Main) { + lifecycleScope.launch(Main) { val defaultPath = "SD${File.separator}Fonts" selectFontDir.launch( FilePickerParam(