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) { fun loadContent(resetPageOffset: Boolean, success: (() -> Unit)? = null) {
loadContent(durChapterIndex, resetPageOffset = resetPageOffset) loadContent(durChapterIndex, resetPageOffset = resetPageOffset) {
success?.invoke()
}
loadContent(durChapterIndex + 1, resetPageOffset = resetPageOffset) loadContent(durChapterIndex + 1, resetPageOffset = resetPageOffset)
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 -> book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
Coroutine.async { Coroutine.async {
App.db.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let { BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(book, chapter, it, upContent, resetPageOffset) contentLoadFinish(book, chapter, it, upContent, resetPageOffset) {
success?.invoke()
}
removeLoading(chapter.index) removeLoading(chapter.index)
} ?: download(chapter, resetPageOffset = resetPageOffset) } ?: download(chapter, resetPageOffset = resetPageOffset)
} ?: removeLoading(index) } ?: 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 book = book
val webBook = webBook val webBook = webBook
if (book != null && webBook != null) { if (book != null && webBook != null) {
@ -290,7 +303,9 @@ object ReadBook {
} else if (book != null) { } else if (book != null) {
contentLoadFinish( contentLoadFinish(
book, chapter, "没有书源", resetPageOffset = resetPageOffset book, chapter, "没有书源", resetPageOffset = resetPageOffset
) ) {
success?.invoke()
}
removeLoading(chapter.index) removeLoading(chapter.index)
} else { } else {
removeLoading(chapter.index) removeLoading(chapter.index)
@ -379,7 +394,8 @@ object ReadBook {
chapter: BookChapter, chapter: BookChapter,
content: String, content: String,
upContent: Boolean = true, upContent: Boolean = true,
resetPageOffset: Boolean resetPageOffset: Boolean,
success: (() -> Unit)? = null
) { ) {
Coroutine.async { Coroutine.async {
ImageProvider.clearOut(durChapterIndex) ImageProvider.clearOut(durChapterIndex)
@ -420,6 +436,8 @@ object ReadBook {
}.onError { }.onError {
it.printStackTrace() it.printStackTrace()
App.INSTANCE.toast("ChapterProvider ERROR:\n${it.getStackTraceString()}") 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) { private fun skipToSearch(index: Int, indexWithinChapter: Int) {
launch(IO) { viewModel.openChapter(index) {
viewModel.openChapter(index) launch(IO) {
// block until load correct chapter and pages val pages = ReadBook.curTextChapter?.pages ?: return@launch
var pages = ReadBook.curTextChapter?.pages val positions = ReadBook.searchResultPositions(
while (ReadBook.durChapterIndex != index || pages == null) { pages,
delay(100L) indexWithinChapter,
pages = ReadBook.curTextChapter?.pages viewModel.searchContentQuery
} )
val positions = ReadBook.searchResultPositions( while (ReadBook.durPageIndex() != positions[0]) {
pages, delay(100L)
indexWithinChapter, ReadBook.skipToPage(positions[0])
viewModel.searchContentQuery }
) withContext(Main) {
while (ReadBook.durPageIndex() != positions[0]) { binding.readView.curPage.selectStartMoveIndex(0, positions[1], positions[2])
delay(100L) delay(20L)
ReadBook.skipToPage(positions[0]) when (positions[3]) {
} 0 -> binding.readView.curPage.selectEndMoveIndex(
withContext(Main) { 0,
binding.readView.curPage.selectStartMoveIndex(0, positions[1], positions[2]) positions[1],
delay(20L) positions[2] + viewModel.searchContentQuery.length - 1
when (positions[3]) { )
0 -> binding.readView.curPage.selectEndMoveIndex( 1 -> binding.readView.curPage.selectEndMoveIndex(
0, 0,
positions[1], positions[1] + 1,
positions[2] + viewModel.searchContentQuery.length - 1 positions[4]
) )
1 -> binding.readView.curPage.selectEndMoveIndex( //consider change page, jump to scroll position
0, -1 -> binding.readView.curPage
positions[1] + 1, .selectEndMoveIndex(1, 0, positions[4])
positions[4] }
) binding.readView.isTextSelected = true
//consider change page, jump to scroll position delay(100L)
-1 -> binding.readView.curPage
.selectEndMoveIndex(1, 0, positions[4])
} }
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.prevTextChapter = null
ReadBook.curTextChapter = null ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null ReadBook.nextTextChapter = null
@ -260,7 +260,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.durChapterPos = durChapterPos ReadBook.durChapterPos = durChapterPos
} }
ReadBook.saveRead() ReadBook.saveRead()
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true) {
success?.invoke()
}
} }
fun removeFromBookshelf(success: (() -> Unit)?) { 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_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="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="tg_url" translatable="false">https://t.me/yueduguanfang</string>
<string name="discord_url" translatable="false">https://discord.gg/qDE52P5xGW</string>
</resources> </resources>

@ -53,6 +53,12 @@
android:summary="@string/this_github_url" android:summary="@string/this_github_url"
app:iconSpaceReserved="false" /> 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 <io.legado.app.ui.widget.prefs.Preference
android:key="tg" android:key="tg"
android:title="TG" android:title="TG"

Loading…
Cancel
Save