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. 13
      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 io.legado.app.utils.fromJsonObject
import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import java.nio.charset.Charset
import kotlin.math.max import kotlin.math.max
@Parcelize @Parcelize
@ -81,6 +82,10 @@ data class Book(
variable = GSON.toJson(variableMap) variable = GSON.toJson(variableMap)
} }
fun getCharset(): Charset {
return charset(charset ?: "UTF-8")
}
fun toSearchBook(): SearchBook { fun toSearchBook(): SearchBook {
return SearchBook( return SearchBook(
name = name, 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.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.utils.* import io.legado.app.utils.*
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
import java.io.File import java.io.File
@ -126,7 +127,9 @@ object BookHelp {
} }
fun getContent(book: Book, bookChapter: BookChapter): String? { 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 -> DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
return DocumentUtils.getDirDocument( return DocumentUtils.getDirDocument(
root, root,

@ -28,14 +28,7 @@ object AnalyzeTxtFile {
} }
fun analyze(context: Context, book: Book): ArrayList<BookChapter> { fun analyze(context: Context, book: Book): ArrayList<BookChapter> {
val uri = Uri.parse(book.bookUrl) val bookFile = getBookFile(context, book)
val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf())
if (!bookFile.exists()) {
bookFile.createNewFile()
DocumentUtils.readBytes(context, uri)?.let {
bookFile.writeBytes(it)
}
}
val charset = charset(EncodingDetect.getEncode(bookFile)) val charset = charset(EncodingDetect.getEncode(bookFile))
book.charset = charset.name() book.charset = charset.name()
val toc = arrayListOf<BookChapter>() val toc = arrayListOf<BookChapter>()
@ -189,6 +182,28 @@ object AnalyzeTxtFile {
return toc 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? { private fun getTocRule(bookStream: RandomAccessFile, charset: Charset): Pattern? {
val tocRules = getTocRules() val tocRules = getTocRules()

@ -197,6 +197,7 @@ object ReadBook {
private fun download(index: Int) { private fun download(index: Int) {
book?.let { book -> book?.let { book ->
if (book.isLocalBook()) return
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 ->

@ -10,6 +10,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
@ -79,18 +80,29 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
if (book.isLocalBook()) {
loadChapterList(book, changeDruChapterIndex)
} else {
ReadBook.webBook?.getBookInfo(book, this) ReadBook.webBook?.getBookInfo(book, this)
?.onSuccess { ?.onSuccess {
loadChapterList(book, changeDruChapterIndex) loadChapterList(book, changeDruChapterIndex)
} }
} }
} }
}
private fun loadChapterList( private fun loadChapterList(
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
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) ReadBook.webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList -> ?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) { if (!cList.isNullOrEmpty()) {
@ -109,6 +121,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} ?: autoChangeSource() } ?: autoChangeSource()
} }
} }
}
fun changeTo(book1: Book) { fun changeTo(book1: Book) {
execute { execute {

Loading…
Cancel
Save