优化
This commit is contained in:
@@ -80,6 +80,9 @@ interface BookDao {
|
|||||||
@get:Query("SELECT * FROM books")
|
@get:Query("SELECT * FROM books")
|
||||||
val all: List<Book>
|
val all: List<Book>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM books where type & :type > 0 and type & ${BookType.local} = 0")
|
||||||
|
fun getByTypeOnLine(type: Int): List<Book>
|
||||||
|
|
||||||
@get:Query("SELECT * FROM books where type & ${BookType.text} > 0 ORDER BY durChapterTime DESC limit 1")
|
@get:Query("SELECT * FROM books where type & ${BookType.text} > 0 ORDER BY durChapterTime DESC limit 1")
|
||||||
val lastReadBook: Book?
|
val lastReadBook: Book?
|
||||||
|
|
||||||
|
|||||||
+10
-18
@@ -11,14 +11,11 @@ import io.legado.app.base.VMBaseActivity
|
|||||||
import io.legado.app.constant.AppConst
|
import io.legado.app.constant.AppConst
|
||||||
import io.legado.app.constant.AppConst.charsets
|
import io.legado.app.constant.AppConst.charsets
|
||||||
import io.legado.app.constant.EventBus
|
import io.legado.app.constant.EventBus
|
||||||
import io.legado.app.constant.PreferKey
|
|
||||||
import io.legado.app.data.appDb
|
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.BookChapter
|
||||||
import io.legado.app.data.entities.BookGroup
|
import io.legado.app.data.entities.BookGroup
|
||||||
import io.legado.app.databinding.ActivityCacheBookBinding
|
import io.legado.app.databinding.ActivityCacheBookBinding
|
||||||
import io.legado.app.databinding.DialogEditTextBinding
|
import io.legado.app.databinding.DialogEditTextBinding
|
||||||
import io.legado.app.help.book.BookHelp
|
|
||||||
import io.legado.app.help.book.isAudio
|
import io.legado.app.help.book.isAudio
|
||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
import io.legado.app.lib.dialogs.SelectItem
|
import io.legado.app.lib.dialogs.SelectItem
|
||||||
@@ -32,8 +29,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
@@ -76,6 +72,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
initRecyclerView()
|
initRecyclerView()
|
||||||
initGroupData()
|
initGroupData()
|
||||||
initBookData()
|
initBookData()
|
||||||
|
initCacheData()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
@@ -178,7 +175,6 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
}.conflate().collect { books ->
|
}.conflate().collect { books ->
|
||||||
adapter.setItems(books)
|
adapter.setItems(books)
|
||||||
initCacheSize(books)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,17 +191,10 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initCacheSize(books: List<Book>) {
|
private fun initCacheData() {
|
||||||
launch(IO) {
|
launch {
|
||||||
books.forEach { book ->
|
viewModel.bookCacheFlow.conflate().collect {
|
||||||
val chapterCaches = hashSetOf<String>()
|
viewModel.cacheChapters[it.first] = it.second
|
||||||
val cacheNames = BookHelp.getChapterFiles(book)
|
|
||||||
appDb.bookChapterDao.getChapterList(book.bookUrl).forEach { chapter ->
|
|
||||||
if (cacheNames.contains(chapter.getFileName())) {
|
|
||||||
chapterCaches.add(chapter.url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
adapter.cacheChapters[book.bookUrl] = chapterCaches
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
|
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
|
||||||
}
|
}
|
||||||
@@ -242,7 +231,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) {
|
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) {
|
||||||
adapter.cacheChapters[it.bookUrl]?.add(it.url)
|
viewModel.cacheChapters[it.bookUrl]?.add(it.url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,6 +330,9 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override val cacheChapters: HashMap<String, HashSet<String>>
|
||||||
|
get() = viewModel.cacheChapters
|
||||||
|
|
||||||
override fun exportProgress(bookUrl: String): Int? {
|
override fun exportProgress(bookUrl: String): Int? {
|
||||||
return viewModel.exportProgress[bookUrl]
|
return viewModel.exportProgress[bookUrl]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import io.legado.app.utils.visible
|
|||||||
class CacheAdapter(context: Context, private val callBack: CallBack) :
|
class CacheAdapter(context: Context, private val callBack: CallBack) :
|
||||||
RecyclerAdapter<Book, ItemDownloadBinding>(context) {
|
RecyclerAdapter<Book, ItemDownloadBinding>(context) {
|
||||||
|
|
||||||
val cacheChapters = hashMapOf<String, HashSet<String>>()
|
|
||||||
|
|
||||||
override fun getViewBinding(parent: ViewGroup): ItemDownloadBinding {
|
override fun getViewBinding(parent: ViewGroup): ItemDownloadBinding {
|
||||||
return ItemDownloadBinding.inflate(inflater, parent, false)
|
return ItemDownloadBinding.inflate(inflater, parent, false)
|
||||||
@@ -37,7 +37,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) :
|
|||||||
if (item.isLocal) {
|
if (item.isLocal) {
|
||||||
tvDownload.setText(R.string.local_book)
|
tvDownload.setText(R.string.local_book)
|
||||||
} else {
|
} else {
|
||||||
val cs = cacheChapters[item.bookUrl]
|
val cs = callBack.cacheChapters[item.bookUrl]
|
||||||
if (cs == null) {
|
if (cs == null) {
|
||||||
tvDownload.setText(R.string.loading)
|
tvDownload.setText(R.string.loading)
|
||||||
} else {
|
} else {
|
||||||
@@ -53,7 +53,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) :
|
|||||||
if (item.isLocal) {
|
if (item.isLocal) {
|
||||||
tvDownload.setText(R.string.local_book)
|
tvDownload.setText(R.string.local_book)
|
||||||
} else {
|
} else {
|
||||||
val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0
|
val cacheSize = callBack.cacheChapters[item.bookUrl]?.size ?: 0
|
||||||
tvDownload.text =
|
tvDownload.text =
|
||||||
context.getString(R.string.download_count, cacheSize, item.totalChapterNum)
|
context.getString(R.string.download_count, cacheSize, item.totalChapterNum)
|
||||||
}
|
}
|
||||||
@@ -121,6 +121,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface CallBack {
|
interface CallBack {
|
||||||
|
val cacheChapters: HashMap<String, HashSet<String>>
|
||||||
fun export(position: Int)
|
fun export(position: Int)
|
||||||
fun exportProgress(bookUrl: String): Int?
|
fun exportProgress(bookUrl: String): Int?
|
||||||
fun exportMsg(bookUrl: String): String?
|
fun exportMsg(bookUrl: String): String?
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import io.legado.app.R
|
|||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.constant.AppConst
|
import io.legado.app.constant.AppConst
|
||||||
import io.legado.app.constant.AppPattern
|
import io.legado.app.constant.AppPattern
|
||||||
|
import io.legado.app.constant.BookType
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.Book
|
import io.legado.app.data.entities.Book
|
||||||
import io.legado.app.data.entities.BookChapter
|
import io.legado.app.data.entities.BookChapter
|
||||||
@@ -24,8 +25,11 @@ import io.legado.app.help.book.ContentProcessor
|
|||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
import io.legado.app.help.coroutine.OrderCoroutine
|
import io.legado.app.help.coroutine.OrderCoroutine
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import me.ag2s.epublib.domain.*
|
import me.ag2s.epublib.domain.*
|
||||||
@@ -46,6 +50,23 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
val exportMsg = ConcurrentHashMap<String, String>()
|
val exportMsg = ConcurrentHashMap<String, String>()
|
||||||
private val mutex = Mutex()
|
private val mutex = Mutex()
|
||||||
|
|
||||||
|
val cacheChapters = hashMapOf<String, HashSet<String>>()
|
||||||
|
|
||||||
|
val bookCacheFlow = flow {
|
||||||
|
//直接获取全部缓存信息,避免切换分组重新获取
|
||||||
|
val books = appDb.bookDao.getByTypeOnLine(BookType.text or BookType.image)
|
||||||
|
books.forEach { book ->
|
||||||
|
val chapterCaches = hashSetOf<String>()
|
||||||
|
val cacheNames = BookHelp.getChapterFiles(book)
|
||||||
|
appDb.bookChapterDao.getChapterList(book.bookUrl).forEach { chapter ->
|
||||||
|
if (cacheNames.contains(chapter.getFileName())) {
|
||||||
|
chapterCaches.add(chapter.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
emit(Pair(book.bookUrl, chapterCaches))
|
||||||
|
}
|
||||||
|
}.flowOn(Dispatchers.IO)
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
private var exportNumber = 0
|
private var exportNumber = 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user