pull/811/head
gedoor 4 years ago
parent d3e0a1f605
commit 14bc109a2b
  1. 32
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 68
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  4. 1
      app/src/main/res/values/pref_key_value.xml
  5. 6
      app/src/main/res/xml/about.xml

@ -240,19 +240,28 @@ object ReadBook {
/**
* 加载章节内容
*/
fun loadContent(resetPageOffset: Boolean) {
loadContent(durChapterIndex, resetPageOffset = resetPageOffset)
fun loadContent(resetPageOffset: Boolean, success: (() -> Unit)? = null) {
loadContent(durChapterIndex, resetPageOffset = resetPageOffset) {
success?.invoke()
}
loadContent(durChapterIndex + 1, resetPageOffset = resetPageOffset)
loadContent(durChapterIndex - 1, resetPageOffset = resetPageOffset)
}
fun loadContent(index: Int, upContent: Boolean = true, resetPageOffset: Boolean) {
fun loadContent(
index: Int,
upContent: Boolean = true,
resetPageOffset: Boolean,
success: (() -> Unit)? = null
) {
book?.let { book ->
if (addLoading(index)) {
Coroutine.async {
App.db.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(book, chapter, it, upContent, resetPageOffset)
contentLoadFinish(book, chapter, it, upContent, resetPageOffset) {
success?.invoke()
}
removeLoading(chapter.index)
} ?: download(chapter, resetPageOffset = resetPageOffset)
} ?: removeLoading(index)
@ -282,7 +291,11 @@ object ReadBook {
}
}
private fun download(chapter: BookChapter, resetPageOffset: Boolean) {
private fun download(
chapter: BookChapter,
resetPageOffset: Boolean,
success: (() -> Unit)? = null
) {
val book = book
val webBook = webBook
if (book != null && webBook != null) {
@ -290,7 +303,9 @@ object ReadBook {
} else if (book != null) {
contentLoadFinish(
book, chapter, "没有书源", resetPageOffset = resetPageOffset
)
) {
success?.invoke()
}
removeLoading(chapter.index)
} else {
removeLoading(chapter.index)
@ -379,7 +394,8 @@ object ReadBook {
chapter: BookChapter,
content: String,
upContent: Boolean = true,
resetPageOffset: Boolean
resetPageOffset: Boolean,
success: (() -> Unit)? = null
) {
Coroutine.async {
ImageProvider.clearOut(durChapterIndex)
@ -420,6 +436,8 @@ object ReadBook {
}.onError {
it.printStackTrace()
App.INSTANCE.toast("ChapterProvider ERROR:\n${it.getStackTraceString()}")
}.onSuccess {
success?.invoke()
}
}

@ -806,43 +806,39 @@ class ReadBookActivity : ReadBookBaseActivity(),
}
private fun skipToSearch(index: Int, indexWithinChapter: Int) {
launch(IO) {
viewModel.openChapter(index)
// block until load correct chapter and pages
var pages = ReadBook.curTextChapter?.pages
while (ReadBook.durChapterIndex != index || pages == null) {
delay(100L)
pages = ReadBook.curTextChapter?.pages
}
val positions = ReadBook.searchResultPositions(
pages,
indexWithinChapter,
viewModel.searchContentQuery
)
while (ReadBook.durPageIndex() != positions[0]) {
delay(100L)
ReadBook.skipToPage(positions[0])
}
withContext(Main) {
binding.readView.curPage.selectStartMoveIndex(0, positions[1], positions[2])
delay(20L)
when (positions[3]) {
0 -> binding.readView.curPage.selectEndMoveIndex(
0,
positions[1],
positions[2] + viewModel.searchContentQuery.length - 1
)
1 -> binding.readView.curPage.selectEndMoveIndex(
0,
positions[1] + 1,
positions[4]
)
//consider change page, jump to scroll position
-1 -> binding.readView.curPage
.selectEndMoveIndex(1, 0, positions[4])
viewModel.openChapter(index) {
launch(IO) {
val pages = ReadBook.curTextChapter?.pages ?: return@launch
val positions = ReadBook.searchResultPositions(
pages,
indexWithinChapter,
viewModel.searchContentQuery
)
while (ReadBook.durPageIndex() != positions[0]) {
delay(100L)
ReadBook.skipToPage(positions[0])
}
withContext(Main) {
binding.readView.curPage.selectStartMoveIndex(0, positions[1], positions[2])
delay(20L)
when (positions[3]) {
0 -> binding.readView.curPage.selectEndMoveIndex(
0,
positions[1],
positions[2] + viewModel.searchContentQuery.length - 1
)
1 -> binding.readView.curPage.selectEndMoveIndex(
0,
positions[1] + 1,
positions[4]
)
//consider change page, jump to scroll position
-1 -> binding.readView.curPage
.selectEndMoveIndex(1, 0, positions[4])
}
binding.readView.isTextSelected = true
delay(100L)
}
binding.readView.isTextSelected = true
delay(100L)
}
}
}

@ -250,7 +250,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
fun openChapter(index: Int, durChapterPos: Int = 0) {
fun openChapter(index: Int, durChapterPos: Int = 0, success: (() -> Unit)? = null) {
ReadBook.prevTextChapter = null
ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null
@ -260,7 +260,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.durChapterPos = durChapterPos
}
ReadBook.saveRead()
ReadBook.loadContent(resetPageOffset = true)
ReadBook.loadContent(resetPageOffset = true) {
success?.invoke()
}
}
fun removeFromBookshelf(success: (() -> Unit)?) {

@ -13,4 +13,5 @@
<string name="latest_release_url" translatable="false">https://github.com/gedoor/legado/releases/latest</string>
<string name="latest_release_api" translatable="false">https://api.github.com/repos/gedoor/legado/releases/latest</string>
<string name="tg_url" translatable="false">https://t.me/yueduguanfang</string>
<string name="discord_url" translatable="false">https://discord.gg/qDE52P5xGW</string>
</resources>

@ -53,6 +53,12 @@
android:summary="@string/this_github_url"
app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.Preference
android:key="discord"
android:title="Discord"
android:summary="@string/tg_url"
app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.Preference
android:key="tg"
android:title="TG"

Loading…
Cancel
Save