优化
This commit is contained in:
+16
-25
@@ -27,7 +27,6 @@ import io.legado.app.ui.about.AppLogDialog
|
|||||||
import io.legado.app.ui.document.HandleFileContract
|
import io.legado.app.ui.document.HandleFileContract
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
import kotlinx.coroutines.Dispatchers.Default
|
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
@@ -42,6 +41,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
|
|
||||||
private val exportBookPathKey = "exportBookPath"
|
private val exportBookPathKey = "exportBookPath"
|
||||||
private val exportTypes = arrayListOf("txt", "epub")
|
private val exportTypes = arrayListOf("txt", "epub")
|
||||||
|
private val layoutManager by lazy { LinearLayoutManager(this) }
|
||||||
private val adapter by lazy { CacheAdapter(this, this) }
|
private val adapter by lazy { CacheAdapter(this, this) }
|
||||||
private var booksFlowJob: Job? = null
|
private var booksFlowJob: Job? = null
|
||||||
private var menu: Menu? = null
|
private var menu: Menu? = null
|
||||||
@@ -147,7 +147,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initRecyclerView() {
|
private fun initRecyclerView() {
|
||||||
binding.recyclerView.layoutManager = LinearLayoutManager(this)
|
binding.recyclerView.layoutManager = layoutManager
|
||||||
binding.recyclerView.adapter = adapter
|
binding.recyclerView.adapter = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,18 +192,21 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun observeLiveBus() {
|
private fun notifyItemChanged(bookUrl: String) {
|
||||||
viewModel.upAdapterLiveData.observe(this) {
|
kotlin.runCatching {
|
||||||
launch(Default) {
|
adapter.getItems().forEachIndexed { index, book ->
|
||||||
adapter.getItems().forEachIndexed { index, book ->
|
if (bookUrl == book.bookUrl) {
|
||||||
if (book.bookUrl == it) {
|
adapter.notifyItemChanged(index, true)
|
||||||
binding.recyclerView.post {
|
return
|
||||||
adapter.notifyItemChanged(index, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun observeLiveBus() {
|
||||||
|
viewModel.upAdapterLiveData.observe(this) {
|
||||||
|
notifyItemChanged(it)
|
||||||
|
}
|
||||||
observeEvent<String>(EventBus.UP_DOWNLOAD) {
|
observeEvent<String>(EventBus.UP_DOWNLOAD) {
|
||||||
if (!CacheBook.isRun) {
|
if (!CacheBook.isRun) {
|
||||||
menu?.findItem(R.id.menu_download)?.let { item ->
|
menu?.findItem(R.id.menu_download)?.let { item ->
|
||||||
@@ -218,23 +221,11 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
menu?.applyTint(this)
|
menu?.applyTint(this)
|
||||||
}
|
}
|
||||||
adapter.getItems().forEachIndexed { index, book ->
|
notifyItemChanged(it)
|
||||||
if (book.bookUrl == it) {
|
|
||||||
adapter.notifyItemChanged(index, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
observeEvent<Pair<Book, BookChapter>>(EventBus.SAVE_CONTENT) { (book, chapter) ->
|
observeEvent<Pair<Book, BookChapter>>(EventBus.SAVE_CONTENT) { (book, chapter) ->
|
||||||
viewModel.cacheChapters[book.bookUrl]?.add(chapter.url)
|
viewModel.cacheChapters[book.bookUrl]?.add(chapter.url)
|
||||||
launch(Default) {
|
notifyItemChanged(book.bookUrl)
|
||||||
adapter.getItems().forEachIndexed { index, item ->
|
|
||||||
if (book.bookUrl == item.bookUrl) {
|
|
||||||
binding.recyclerView.post {
|
|
||||||
adapter.notifyItemChanged(index, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import io.legado.app.exception.NoStackTraceException
|
|||||||
import io.legado.app.help.AppWebDav
|
import io.legado.app.help.AppWebDav
|
||||||
import io.legado.app.help.book.BookHelp
|
import io.legado.app.help.book.BookHelp
|
||||||
import io.legado.app.help.book.ContentProcessor
|
import io.legado.app.help.book.ContentProcessor
|
||||||
|
import io.legado.app.help.book.isLocal
|
||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
import io.legado.app.help.coroutine.Coroutine
|
import io.legado.app.help.coroutine.Coroutine
|
||||||
import io.legado.app.help.coroutine.OrderCoroutine
|
import io.legado.app.help.coroutine.OrderCoroutine
|
||||||
@@ -58,7 +59,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
loadChapterCoroutine?.cancel()
|
loadChapterCoroutine?.cancel()
|
||||||
loadChapterCoroutine = execute {
|
loadChapterCoroutine = execute {
|
||||||
books.forEach { book ->
|
books.forEach { book ->
|
||||||
if (!cacheChapters.contains(book.bookUrl)) {
|
if (!book.isLocal && !cacheChapters.contains(book.bookUrl)) {
|
||||||
val chapterCaches = hashSetOf<String>()
|
val chapterCaches = hashSetOf<String>()
|
||||||
val cacheNames = BookHelp.getChapterFiles(book)
|
val cacheNames = BookHelp.getChapterFiles(book)
|
||||||
if (cacheNames.isNotEmpty()) {
|
if (cacheNames.isNotEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user