pull/464/head
Robot 4 years ago
commit 55f5f2e543
  1. 8
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 10
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  3. 11
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  4. 6
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditViewModel.kt

@ -14,6 +14,7 @@ import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import java.nio.charset.Charset import java.nio.charset.Charset
import kotlin.math.max import kotlin.math.max
import kotlin.math.min
@Parcelize @Parcelize
@TypeConverters(Book.Converters::class) @TypeConverters(Book.Converters::class)
@ -53,7 +54,7 @@ data class Book(
var originOrder: Int = 0, //书源排序 var originOrder: Int = 0, //书源排序
var variable: String? = null, // 自定义书籍变量信息(用于书源规则检索书籍信息) var variable: String? = null, // 自定义书籍变量信息(用于书源规则检索书籍信息)
var readConfig: ReadConfig? = null var readConfig: ReadConfig? = null
): Parcelable, BaseBook { ) : Parcelable, BaseBook {
fun isLocalBook(): Boolean { fun isLocalBook(): Boolean {
return origin == BookType.local return origin == BookType.local
@ -146,7 +147,10 @@ data class Book(
} }
fun getFolderName(): String { fun getFolderName(): String {
return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl) //防止书名过长,只取9位
var folderName = name.replace(AppPattern.fileNameRegex, "")
folderName = folderName.substring(0, min(9, folderName.length))
return folderName + MD5Utils.md5Encode16(bookUrl)
} }
fun toSearchBook() = SearchBook( fun toSearchBook() = SearchBook(

@ -239,7 +239,7 @@ class AnalyzeTxtFile {
} }
companion object { companion object {
private const val folderName = "bookTxt"
private const val BLANK: Byte = 0x0a private const val BLANK: Byte = 0x0a
//默认从文件中获取数据的长度 //默认从文件中获取数据的长度
@ -247,12 +247,6 @@ class AnalyzeTxtFile {
//没有标题的时候,每个章节的最大长度 //没有标题的时候,每个章节的最大长度
private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024 private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024
val cacheFolder: File by lazy {
val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir
FileUtils.createFolderIfNotExist(rootFile, folderName)
}
fun getContent(book: Book, bookChapter: BookChapter): String { fun getContent(book: Book, bookChapter: BookChapter): String {
val bookFile = getBookFile(book) val bookFile = getBookFile(book)
@ -269,7 +263,7 @@ class AnalyzeTxtFile {
private fun getBookFile(book: Book): File { private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentPath()) { if (book.bookUrl.isContentPath()) {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(cacheFolder, book.originName) val bookFile = FileUtils.getFile(LocalBook.cacheFolder, book.originName)
if (!bookFile.exists()) { if (!bookFile.exists()) {
bookFile.createNewFile() bookFile.createNewFile()
DocumentUtils.readBytes(App.INSTANCE, uri)?.let { DocumentUtils.readBytes(App.INSTANCE, uri)?.let {

@ -11,6 +11,13 @@ import java.io.File
object LocalBook { object LocalBook {
private const val folderName = "bookTxt"
val cacheFolder: File by lazy {
val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir
FileUtils.createFolderIfNotExist(rootFile, folderName)
}
fun getChapterList(book: Book): ArrayList<BookChapter> { fun getChapterList(book: Book): ArrayList<BookChapter> {
return if (book.isEpub()) { return if (book.isEpub()) {
@ -34,7 +41,7 @@ object LocalBook {
path = uri.toString() path = uri.toString()
val doc = DocumentFile.fromSingleUri(App.INSTANCE, uri) val doc = DocumentFile.fromSingleUri(App.INSTANCE, uri)
doc?.let { doc?.let {
val bookFile = FileUtils.getFile(AnalyzeTxtFile.cacheFolder, it.name!!) val bookFile = FileUtils.getFile(cacheFolder, it.name!!)
if (!bookFile.exists()) { if (!bookFile.exists()) {
bookFile.createNewFile() bookFile.createNewFile()
doc.readBytes(App.INSTANCE)?.let { bytes -> doc.readBytes(App.INSTANCE)?.let { bytes ->
@ -82,7 +89,7 @@ object LocalBook {
fun deleteBook(book: Book, deleteOriginal: Boolean) { fun deleteBook(book: Book, deleteOriginal: Boolean) {
kotlin.runCatching { kotlin.runCatching {
if (book.isLocalTxt()) { if (book.isLocalTxt()) {
val bookFile = FileUtils.getFile(AnalyzeTxtFile.cacheFolder, book.originName) val bookFile = FileUtils.getFile(cacheFolder, book.originName)
bookFile.delete() bookFile.delete()
} }

@ -51,7 +51,7 @@ class BookInfoActivity :
ChangeCoverDialog.CallBack { ChangeCoverDialog.CallBack {
private val requestCodeChapterList = 568 private val requestCodeChapterList = 568
private val requestCodeSourceEdit = 562 private val requestCodeInfoEdit = 562
private val requestCodeRead = 432 private val requestCodeRead = 432
override val viewModel: BookInfoViewModel override val viewModel: BookInfoViewModel
@ -82,7 +82,7 @@ class BookInfoActivity :
if (viewModel.inBookshelf) { if (viewModel.inBookshelf) {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
startActivityForResult<BookInfoEditActivity>( startActivityForResult<BookInfoEditActivity>(
requestCodeSourceEdit, requestCodeInfoEdit,
Pair("bookUrl", it.bookUrl) Pair("bookUrl", it.bookUrl)
) )
} }
@ -368,7 +368,7 @@ class BookInfoActivity :
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
when (requestCode) { when (requestCode) {
requestCodeSourceEdit -> requestCodeInfoEdit ->
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
viewModel.upEditBook() viewModel.upEditBook()
} }

@ -25,7 +25,7 @@ class BookInfoEditViewModel(application: Application) : BaseViewModel(applicatio
if (ReadBook.book?.bookUrl == book.bookUrl) { if (ReadBook.book?.bookUrl == book.bookUrl) {
ReadBook.book = book ReadBook.book = book
} }
App.db.bookDao().insert(book) App.db.bookDao().update(book)
}.onSuccess { }.onSuccess {
success?.invoke() success?.invoke()
} }

Loading…
Cancel
Save