|
|
|
@ -1,10 +1,8 @@ |
|
|
|
|
package io.legado.app.service.help |
|
|
|
|
|
|
|
|
|
import android.util.Log |
|
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
|
import com.hankcs.hanlp.HanLP |
|
|
|
|
import io.legado.app.App |
|
|
|
|
import io.legado.app.R |
|
|
|
|
import io.legado.app.constant.BookType |
|
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
@ -61,6 +59,9 @@ object ReadBook { |
|
|
|
|
titleDate.postValue(book.name) |
|
|
|
|
upWebBook(book) |
|
|
|
|
ImageProvider.clearAllCache() |
|
|
|
|
synchronized(this) { |
|
|
|
|
loadingChapters.clear() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun upWebBook(book: Book) { |
|
|
|
@ -270,35 +271,7 @@ object ReadBook { |
|
|
|
|
val book = book |
|
|
|
|
val webBook = webBook |
|
|
|
|
if (book != null && webBook != null) { |
|
|
|
|
webBook.getContent(book, chapter) |
|
|
|
|
.onSuccess(Dispatchers.IO) { content -> |
|
|
|
|
if (content.isEmpty()) { |
|
|
|
|
contentLoadFinish( |
|
|
|
|
book, |
|
|
|
|
chapter, |
|
|
|
|
App.INSTANCE.getString(R.string.content_empty), |
|
|
|
|
resetPageOffset = resetPageOffset |
|
|
|
|
) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
|
contentLoadFinish( |
|
|
|
|
book, |
|
|
|
|
chapter, |
|
|
|
|
content, |
|
|
|
|
resetPageOffset = resetPageOffset |
|
|
|
|
) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
}.onError { |
|
|
|
|
contentLoadFinish( |
|
|
|
|
book, |
|
|
|
|
chapter, |
|
|
|
|
it.localizedMessage ?: "未知错误", |
|
|
|
|
resetPageOffset = resetPageOffset |
|
|
|
|
) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
CacheBook.download(webBook, book, chapter) |
|
|
|
|
} else if (book != null) { |
|
|
|
|
contentLoadFinish( |
|
|
|
|
book, |
|
|
|
@ -320,14 +293,17 @@ object ReadBook { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun removeLoading(index: Int) { |
|
|
|
|
fun removeLoading(index: Int) { |
|
|
|
|
synchronized(this) { |
|
|
|
|
loadingChapters.remove(index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun searchResultPositions(pages: List<TextPage>, indexWithinChapter: Int, query: String): Array<Int>{ |
|
|
|
|
// |
|
|
|
|
fun searchResultPositions( |
|
|
|
|
pages: List<TextPage>, |
|
|
|
|
indexWithinChapter: Int, |
|
|
|
|
query: String |
|
|
|
|
): Array<Int> { |
|
|
|
|
// calculate search result's pageIndex |
|
|
|
|
var content = "" |
|
|
|
|
pages.map { |
|
|
|
@ -336,7 +312,7 @@ object ReadBook { |
|
|
|
|
var count = 1 |
|
|
|
|
var index = content.indexOf(query) |
|
|
|
|
while (count != indexWithinChapter) { |
|
|
|
|
index = content.indexOf(query, index + 1); |
|
|
|
|
index = content.indexOf(query, index + 1) |
|
|
|
|
count += 1 |
|
|
|
|
} |
|
|
|
|
val contentPosition = index |
|
|
|
@ -386,7 +362,7 @@ object ReadBook { |
|
|
|
|
/** |
|
|
|
|
* 内容加载完成 |
|
|
|
|
*/ |
|
|
|
|
private fun contentLoadFinish( |
|
|
|
|
fun contentLoadFinish( |
|
|
|
|
book: Book, |
|
|
|
|
chapter: BookChapter, |
|
|
|
|
content: String, |
|
|
|
|