From e6fdcfa3e5d7e271a4e6e13a5e72312cec3dded5 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 19 Oct 2022 23:09: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 --- .../java/io/legado/app/help/book/BookHelp.kt | 8 +- .../legado/app/ui/book/cache/CacheActivity.kt | 37 ++++++-- .../legado/app/ui/book/cache/CacheAdapter.kt | 4 +- .../app/ui/book/cache/CacheViewModel.kt | 89 +++++++++---------- .../searchContent/SearchContentActivity.kt | 4 +- .../app/ui/book/toc/ChapterListFragment.kt | 4 +- 6 files changed, 83 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/book/BookHelp.kt b/app/src/main/java/io/legado/app/help/book/BookHelp.kt index 0342cada1..00a2a3b25 100644 --- a/app/src/main/java/io/legado/app/help/book/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/book/BookHelp.kt @@ -87,7 +87,7 @@ object BookHelp { ) { saveText(book, bookChapter, content) saveImages(bookSource, book, bookChapter, content) - postEvent(EventBus.SAVE_CONTENT, bookChapter) + postEvent(EventBus.SAVE_CONTENT, Pair(book, bookChapter)) } fun saveText( @@ -198,11 +198,11 @@ object BookHelp { return ZipFile(uri.path) } - fun getChapterFiles(book: Book): Set { + fun getChapterFiles(book: Book): HashSet { + val fileNames = hashSetOf() if (book.isLocalTxt) { - return emptySet() + return fileNames } - val fileNames = hashSetOf() FileUtils.createFolderIfNotExist( downloadDir, subDirs = arrayOf(cacheFolderName, book.getFolderName()) 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 64cd01ef9..def0b1ec7 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 @@ -12,6 +12,7 @@ import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.charsets import io.legado.app.constant.EventBus import io.legado.app.data.appDb +import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookGroup import io.legado.app.databinding.ActivityCacheBookBinding @@ -26,6 +27,7 @@ import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.document.HandleFileContract import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers.Default import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job import kotlinx.coroutines.flow.* @@ -191,9 +193,24 @@ class CacheActivity : VMBaseActivity() override fun observeLiveBus() { viewModel.upAdapterLiveData.observe(this) { - adapter.getItems().forEachIndexed { index, book -> - if (book.bookUrl == it) { - adapter.notifyItemChanged(index, true) + launch(Default) { + adapter.getItems().forEachIndexed { index, book -> + if (book.bookUrl == it) { + binding.recyclerView.post { + adapter.notifyItemChanged(index, true) + } + } + } + } + } + viewModel.cacheLiveData.observe(this) { + launch(Default) { + adapter.getItems().forEachIndexed { index, book -> + if (book.getFolderName() == it) { + binding.recyclerView.post { + adapter.notifyItemChanged(index, true) + } + } } } } @@ -217,11 +234,15 @@ class CacheActivity : VMBaseActivity() } } } - observeEvent(EventBus.SAVE_CONTENT) { - viewModel.cacheChapters[it.bookUrl]?.add(it.url) - adapter.getItems().forEachIndexed { index, book -> - if (book.bookUrl == it.bookUrl) { - adapter.notifyItemChanged(index, true) + observeEvent>(EventBus.SAVE_CONTENT) { (book, chapter) -> + viewModel.cacheChapters[book.bookUrl]?.add(chapter.url) + launch(Default) { + adapter.getItems().forEachIndexed { index, item -> + if (book.bookUrl == item.bookUrl) { + binding.recyclerView.post { + adapter.notifyItemChanged(index, true) + } + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheAdapter.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheAdapter.kt index 320f1af2f..90cfeca7a 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheAdapter.kt @@ -37,7 +37,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) : if (item.isLocal) { tvDownload.setText(R.string.local_book) } else { - val cs = callBack.cacheChapters[item.bookUrl] + val cs = callBack.cacheChapters[item.getFolderName()] if (cs == null) { tvDownload.setText(R.string.loading) } else { @@ -53,7 +53,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) : if (item.isLocal) { tvDownload.setText(R.string.local_book) } else { - val cacheSize = callBack.cacheChapters[item.bookUrl]?.size ?: 0 + val cacheSize = callBack.cacheChapters[item.getFolderName()]?.size ?: 0 tvDownload.text = context.getString(R.string.download_count, cacheSize, item.totalChapterNum) } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 48938a655..e41100224 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.net.Uri +import android.os.Build import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import com.bumptech.glide.Glide @@ -25,8 +26,7 @@ import io.legado.app.help.book.ContentProcessor import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.OrderCoroutine import io.legado.app.utils.* -import kotlinx.coroutines.delay -import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import me.ag2s.epublib.domain.* @@ -37,15 +37,19 @@ import splitties.init.appCtx import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream +import java.io.IOException import java.nio.charset.Charset import java.nio.file.* +import java.nio.file.attribute.BasicFileAttributes import java.util.* import java.util.concurrent.ConcurrentHashMap import kotlin.coroutines.coroutineContext +import kotlin.io.path.name class CacheViewModel(application: Application) : BaseViewModel(application) { val upAdapterLiveData = MutableLiveData() + val cacheLiveData = MutableLiveData() val exportProgress = ConcurrentHashMap() val exportMsg = ConcurrentHashMap() private val mutex = Mutex() @@ -61,52 +65,47 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { private fun loadCacheFiles() { execute { //直接获取全部缓存信息,避免切换分组重新获取 -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { -// val visitor = object : SimpleFileVisitor() { -// -// var names: HashSet? = null -// -// override fun preVisitDirectory( -// dir: Path, -// attrs: BasicFileAttributes? -// ): FileVisitResult { -// if (dir.name != "book_cache") { -// names = hashSetOf() -// } -// return FileVisitResult.CONTINUE -// } -// -// override fun visitFile( -// file: Path, -// attrs: BasicFileAttributes? -// ): FileVisitResult { -// names!!.add(file.name) -// return FileVisitResult.CONTINUE -// } -// -// override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult { -// if (dir.name != "book_cache") { -// cacheChapters[dir.name] = names!! -// } -// return FileVisitResult.CONTINUE -// } -// } -// withContext(Dispatchers.IO) { -// Files.walkFileTree(Paths.get(BookHelp.cachePath), emptySet(), 2, visitor) -// } -// return@execute -// } val books = appDb.bookDao.getByTypeOnLine(BookType.text or BookType.image) - books.forEach { book -> - val chapterCaches = hashSetOf() - val cacheNames = BookHelp.getChapterFiles(book) - appDb.bookChapterDao.getChapterList(book.bookUrl).forEach { chapter -> - if (cacheNames.contains(chapter.getFileName())) { - chapterCaches.add(chapter.url) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val visitor = object : SimpleFileVisitor() { + + var names: HashSet? = null + + override fun preVisitDirectory( + dir: Path, + attrs: BasicFileAttributes? + ): FileVisitResult { + if (dir.name != "book_cache") { + names = hashSetOf() + } + return FileVisitResult.CONTINUE + } + + override fun visitFile( + file: Path, + attrs: BasicFileAttributes? + ): FileVisitResult { + names!!.add(file.name) + return FileVisitResult.CONTINUE + } + + override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult { + if (dir.name != "book_cache") { + cacheChapters[dir.name] = names!! + cacheLiveData.postValue(dir.name) + } + return FileVisitResult.CONTINUE } } - cacheChapters[book.bookUrl] = chapterCaches - upAdapterLiveData.postValue(book.bookUrl) + withContext(Dispatchers.IO) { + Files.walkFileTree(Paths.get(BookHelp.cachePath), emptySet(), 2, visitor) + } + return@execute + } + books.forEach { book -> + val cacheNames = BookHelp.getChapterFiles(book) + cacheChapters[book.getFolderName()] = cacheNames + cacheLiveData.postValue(book.getFolderName()) } } } 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 477f273ac..f1bc5d021 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 @@ -132,9 +132,9 @@ class SearchContentActivity : } override fun observeLiveBus() { - observeEvent(EventBus.SAVE_CONTENT) { chapter -> + observeEvent>(EventBus.SAVE_CONTENT) { (book, chapter) -> viewModel.book?.bookUrl?.let { bookUrl -> - if (chapter.bookUrl == bookUrl) { + if (book.bookUrl == bookUrl) { viewModel.cacheChapterNames.add(chapter.getFileName()) adapter.notifyItemChanged(chapter.index, true) } 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 137e574d3..53ef33135 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 @@ -95,9 +95,9 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt } override fun observeLiveBus() { - observeEvent(EventBus.SAVE_CONTENT) { chapter -> + observeEvent>(EventBus.SAVE_CONTENT) { (book, chapter) -> viewModel.bookData.value?.bookUrl?.let { bookUrl -> - if (chapter.bookUrl == bookUrl) { + if (book.bookUrl == bookUrl) { adapter.cacheFileNames.add(chapter.getFileName()) if (viewModel.searchKey.isNullOrEmpty()) { adapter.notifyItemChanged(chapter.index, true)