pull/84/head
kunfei 5 years ago
parent 2639db7b6c
commit 2aa0be91ea
  1. 5
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 5
      app/src/main/java/io/legado/app/help/BookHelp.kt
  3. 31
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  4. 1
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  5. 47
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -10,6 +10,7 @@ import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize
import java.nio.charset.Charset
import kotlin.math.max
@Parcelize
@ -81,6 +82,10 @@ data class Book(
variable = GSON.toJson(variableMap)
}
fun getCharset(): Charset {
return charset(charset ?: "UTF-8")
}
fun toSearchBook(): SearchBook {
return SearchBook(
name = name,

@ -7,6 +7,7 @@ import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.utils.*
import org.apache.commons.text.similarity.JaccardSimilarity
import java.io.File
@ -126,7 +127,9 @@ object BookHelp {
}
fun getContent(book: Book, bookChapter: BookChapter): String? {
if (downloadUri.isDocumentUri(App.INSTANCE)) {
if (book.isLocalBook()) {
AnalyzeTxtFile.getContent(book, bookChapter)
} else if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
return DocumentUtils.getDirDocument(
root,

@ -28,14 +28,7 @@ object AnalyzeTxtFile {
}
fun analyze(context: Context, book: Book): ArrayList<BookChapter> {
val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf())
if (!bookFile.exists()) {
bookFile.createNewFile()
DocumentUtils.readBytes(context, uri)?.let {
bookFile.writeBytes(it)
}
}
val bookFile = getBookFile(context, book)
val charset = charset(EncodingDetect.getEncode(bookFile))
book.charset = charset.name()
val toc = arrayListOf<BookChapter>()
@ -189,6 +182,28 @@ object AnalyzeTxtFile {
return toc
}
fun getContent(book: Book, bookChapter: BookChapter): String {
val bookFile = getBookFile(App.INSTANCE, book)
//获取文件流
val bookStream = RandomAccessFile(bookFile, "r")
bookStream.seek(bookChapter.start ?: 0)
val extent = (bookChapter.end!! - bookChapter.start!!).toInt()
val content = ByteArray(extent)
bookStream.read(content, 0, extent)
return String(content, book.getCharset())
}
private fun getBookFile(context: Context, book: Book): File {
val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf())
if (!bookFile.exists()) {
bookFile.createNewFile()
DocumentUtils.readBytes(context, uri)?.let {
bookFile.writeBytes(it)
}
}
return bookFile
}
private fun getTocRule(bookStream: RandomAccessFile, charset: Charset): Pattern? {
val tocRules = getTocRules()

@ -197,6 +197,7 @@ object ReadBook {
private fun download(index: Int) {
book?.let { book ->
if (book.isLocalBook()) return
if (addLoading(index)) {
Coroutine.async {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->

@ -10,6 +10,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp
import io.legado.app.model.WebBook
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook
@ -79,10 +80,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) {
execute {
ReadBook.webBook?.getBookInfo(book, this)
?.onSuccess {
loadChapterList(book, changeDruChapterIndex)
}
if (book.isLocalBook()) {
loadChapterList(book, changeDruChapterIndex)
} else {
ReadBook.webBook?.getBookInfo(book, this)
?.onSuccess {
loadChapterList(book, changeDruChapterIndex)
}
}
}
}
@ -91,22 +96,30 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) {
execute {
ReadBook.webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) {
if (changeDruChapterIndex == null) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
ReadBook.chapterSize = cList.size
ReadBook.loadContent()
if (book.isLocalBook()) {
AnalyzeTxtFile.analyze(context, book).let {
App.db.bookChapterDao().insert(*it.toTypedArray())
ReadBook.chapterSize = it.size
ReadBook.loadContent()
}
} else {
ReadBook.webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) {
if (changeDruChapterIndex == null) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
ReadBook.chapterSize = cList.size
ReadBook.loadContent()
} else {
changeDruChapterIndex(cList)
}
} else {
changeDruChapterIndex(cList)
toast(R.string.error_load_toc)
}
} else {
}?.onError {
toast(R.string.error_load_toc)
}
}?.onError {
toast(R.string.error_load_toc)
} ?: autoChangeSource()
} ?: autoChangeSource()
}
}
}

Loading…
Cancel
Save