|
|
|
@ -3,6 +3,7 @@ package io.legado.app.model |
|
|
|
|
import android.content.Context |
|
|
|
|
import io.legado.app.R |
|
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
|
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.BookSource |
|
|
|
@ -12,67 +13,91 @@ import io.legado.app.utils.msg |
|
|
|
|
import io.legado.app.utils.startService |
|
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
|
import splitties.init.appCtx |
|
|
|
|
import java.util.concurrent.ConcurrentHashMap |
|
|
|
|
import java.util.concurrent.CopyOnWriteArraySet |
|
|
|
|
|
|
|
|
|
object CacheBook { |
|
|
|
|
val logs = arrayListOf<String>() |
|
|
|
|
private val downloadMap = ConcurrentHashMap<String, CopyOnWriteArraySet<Int>>() |
|
|
|
|
class CacheBook(val bookSource: BookSource, val book: Book) { |
|
|
|
|
|
|
|
|
|
fun addLog(log: String?) { |
|
|
|
|
log ?: return |
|
|
|
|
synchronized(this) { |
|
|
|
|
if (logs.size > 1000) { |
|
|
|
|
logs.removeAt(0) |
|
|
|
|
companion object { |
|
|
|
|
|
|
|
|
|
val logs = arrayListOf<String>() |
|
|
|
|
private val cacheBookMap = hashMapOf<String, CacheBook>() |
|
|
|
|
|
|
|
|
|
fun get(bookUrl: String): CacheBook? { |
|
|
|
|
var cacheBook = cacheBookMap[bookUrl] |
|
|
|
|
if (cacheBook != null) { |
|
|
|
|
return cacheBook |
|
|
|
|
} |
|
|
|
|
logs.add(log) |
|
|
|
|
val book = appDb.bookDao.getBook(bookUrl) ?: return null |
|
|
|
|
val bookSource = appDb.bookSourceDao.getBookSource(book.origin) ?: return null |
|
|
|
|
cacheBook = CacheBook(bookSource, book) |
|
|
|
|
cacheBookMap[bookUrl] = cacheBook |
|
|
|
|
return cacheBook |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun start(context: Context, bookUrl: String, start: Int, end: Int) { |
|
|
|
|
context.startService<CacheBookService> { |
|
|
|
|
action = IntentAction.start |
|
|
|
|
putExtra("bookUrl", bookUrl) |
|
|
|
|
putExtra("start", start) |
|
|
|
|
putExtra("end", end) |
|
|
|
|
fun get(bookSource: BookSource, book: Book): CacheBook { |
|
|
|
|
var cacheBook = cacheBookMap[book.bookUrl] |
|
|
|
|
if (cacheBook != null) { |
|
|
|
|
return cacheBook |
|
|
|
|
} |
|
|
|
|
cacheBook = CacheBook(bookSource, book) |
|
|
|
|
cacheBookMap[book.bookUrl] = cacheBook |
|
|
|
|
return cacheBook |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun remove(context: Context, bookUrl: String) { |
|
|
|
|
context.startService<CacheBookService> { |
|
|
|
|
action = IntentAction.remove |
|
|
|
|
putExtra("bookUrl", bookUrl) |
|
|
|
|
fun addLog(log: String?) { |
|
|
|
|
log ?: return |
|
|
|
|
synchronized(this) { |
|
|
|
|
if (logs.size > 1000) { |
|
|
|
|
logs.removeAt(0) |
|
|
|
|
} |
|
|
|
|
logs.add(log) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun stop(context: Context) { |
|
|
|
|
context.startService<CacheBookService> { |
|
|
|
|
action = IntentAction.stop |
|
|
|
|
fun start(context: Context, bookUrl: String, start: Int, end: Int) { |
|
|
|
|
context.startService<CacheBookService> { |
|
|
|
|
action = IntentAction.start |
|
|
|
|
putExtra("bookUrl", bookUrl) |
|
|
|
|
putExtra("start", start) |
|
|
|
|
putExtra("end", end) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun downloadCount(): Int { |
|
|
|
|
var count = 0 |
|
|
|
|
downloadMap.forEach { |
|
|
|
|
count += it.value.size |
|
|
|
|
fun remove(context: Context, bookUrl: String) { |
|
|
|
|
context.startService<CacheBookService> { |
|
|
|
|
action = IntentAction.remove |
|
|
|
|
putExtra("bookUrl", bookUrl) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return count |
|
|
|
|
|
|
|
|
|
fun stop(context: Context) { |
|
|
|
|
context.startService<CacheBookService> { |
|
|
|
|
action = IntentAction.stop |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val downloadCount: Int |
|
|
|
|
get() { |
|
|
|
|
var count = 0 |
|
|
|
|
cacheBookMap.forEach { |
|
|
|
|
count += it.value.downloadSet.size |
|
|
|
|
} |
|
|
|
|
return count |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val downloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
|
|
|
|
|
|
fun download( |
|
|
|
|
scope: CoroutineScope, |
|
|
|
|
bookSource: BookSource, |
|
|
|
|
book: Book, |
|
|
|
|
chapter: BookChapter, |
|
|
|
|
resetPageOffset: Boolean = false |
|
|
|
|
) { |
|
|
|
|
if (downloadMap[book.bookUrl]?.contains(chapter.index) == true) { |
|
|
|
|
if (downloadSet.contains(chapter.index)) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (downloadMap[book.bookUrl] == null) { |
|
|
|
|
downloadMap[book.bookUrl] = CopyOnWriteArraySet() |
|
|
|
|
} |
|
|
|
|
downloadMap[book.bookUrl]?.add(chapter.index) |
|
|
|
|
downloadSet.add(chapter.index) |
|
|
|
|
WebBook.getContent(scope, bookSource, book, chapter) |
|
|
|
|
.onSuccess { content -> |
|
|
|
|
if (ReadBook.book?.bookUrl == book.bookUrl) { |
|
|
|
@ -93,10 +118,7 @@ object CacheBook { |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}.onFinally { |
|
|
|
|
downloadMap[book.bookUrl]?.remove(chapter.index) |
|
|
|
|
if (downloadMap[book.bookUrl].isNullOrEmpty()) { |
|
|
|
|
downloadMap.remove(book.bookUrl) |
|
|
|
|
} |
|
|
|
|
downloadSet.remove(chapter.index) |
|
|
|
|
ReadBook.removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|