|
|
@ -19,14 +19,13 @@ import io.legado.app.utils.msg |
|
|
|
import io.legado.app.utils.toastOnUi |
|
|
|
import io.legado.app.utils.toastOnUi |
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
|
|
|
import kotlinx.coroutines.MainScope |
|
|
|
import kotlinx.coroutines.delay |
|
|
|
import kotlinx.coroutines.delay |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.init.appCtx |
|
|
|
import kotlin.math.max |
|
|
|
|
|
|
|
import kotlin.math.min |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("MemberVisibilityCanBePrivate") |
|
|
|
@Suppress("MemberVisibilityCanBePrivate") |
|
|
|
object ReadBook { |
|
|
|
object ReadBook : CoroutineScope by MainScope() { |
|
|
|
var titleDate = MutableLiveData<String>() |
|
|
|
var titleDate = MutableLiveData<String>() |
|
|
|
var book: Book? = null |
|
|
|
var book: Book? = null |
|
|
|
var inBookshelf = false |
|
|
|
var inBookshelf = false |
|
|
@ -145,27 +144,11 @@ object ReadBook { |
|
|
|
curPageChanged() |
|
|
|
curPageChanged() |
|
|
|
Coroutine.async { |
|
|
|
Coroutine.async { |
|
|
|
//预下载 |
|
|
|
//预下载 |
|
|
|
val maxChapterIndex = |
|
|
|
val maxChapterIndex = durChapterIndex + AppConfig.preDownloadNum |
|
|
|
min(chapterSize - 1, durChapterIndex + AppConfig.preDownloadNum) |
|
|
|
|
|
|
|
for (i in durChapterIndex.plus(2)..maxChapterIndex) { |
|
|
|
for (i in durChapterIndex.plus(2)..maxChapterIndex) { |
|
|
|
delay(1000) |
|
|
|
delay(1000) |
|
|
|
download(i) |
|
|
|
download(i) |
|
|
|
} |
|
|
|
} |
|
|
|
book?.let { book -> |
|
|
|
|
|
|
|
//最后一章时检查更新 |
|
|
|
|
|
|
|
if (durChapterPos == 0 && durChapterIndex == chapterSize - 1) { |
|
|
|
|
|
|
|
webBook?.getChapterList(this, book) |
|
|
|
|
|
|
|
?.onSuccess(IO) { cList -> |
|
|
|
|
|
|
|
if (book.bookUrl == ReadBook.book?.bookUrl |
|
|
|
|
|
|
|
&& cList.size > chapterSize |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
appDb.bookChapterDao.insert(*cList.toTypedArray()) |
|
|
|
|
|
|
|
chapterSize = cList.size |
|
|
|
|
|
|
|
nextTextChapter ?: loadContent(1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return true |
|
|
|
return true |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -194,7 +177,7 @@ object ReadBook { |
|
|
|
curPageChanged() |
|
|
|
curPageChanged() |
|
|
|
Coroutine.async { |
|
|
|
Coroutine.async { |
|
|
|
//预下载 |
|
|
|
//预下载 |
|
|
|
val minChapterIndex = max(0, durChapterIndex - 5) |
|
|
|
val minChapterIndex = durChapterIndex - 5 |
|
|
|
for (i in durChapterIndex.minus(2) downTo minChapterIndex) { |
|
|
|
for (i in durChapterIndex.minus(2) downTo minChapterIndex) { |
|
|
|
delay(1000) |
|
|
|
delay(1000) |
|
|
|
download(i) |
|
|
|
download(i) |
|
|
@ -301,6 +284,11 @@ object ReadBook { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun download(index: Int) { |
|
|
|
private fun download(index: Int) { |
|
|
|
|
|
|
|
if (index < 0) return |
|
|
|
|
|
|
|
if (index > chapterSize - 1) { |
|
|
|
|
|
|
|
upToc() |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
if (book.isLocalBook()) return |
|
|
|
if (book.isLocalBook()) return |
|
|
|
if (addLoading(index)) { |
|
|
|
if (addLoading(index)) { |
|
|
@ -404,6 +392,23 @@ object ReadBook { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
fun upToc() { |
|
|
|
|
|
|
|
val webBook = webBook ?: return |
|
|
|
|
|
|
|
val book = book ?: return |
|
|
|
|
|
|
|
if (System.currentTimeMillis() - book.lastCheckTime < 600000) return |
|
|
|
|
|
|
|
book.lastCheckTime = System.currentTimeMillis() |
|
|
|
|
|
|
|
webBook.getChapterList(this, book).onSuccess(IO) { cList -> |
|
|
|
|
|
|
|
if (book.bookUrl == ReadBook.book?.bookUrl |
|
|
|
|
|
|
|
&& cList.size > chapterSize |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
appDb.bookChapterDao.insert(*cList.toTypedArray()) |
|
|
|
|
|
|
|
chapterSize = cList.size |
|
|
|
|
|
|
|
nextTextChapter ?: loadContent(1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun pageAnim(): Int { |
|
|
|
fun pageAnim(): Int { |
|
|
|
book?.let { |
|
|
|
book?.let { |
|
|
|
return if (it.getPageAnim() < 0) |
|
|
|
return if (it.getPageAnim() < 0) |
|
|
|