feat: 优化代码

pull/133/head
kunfei 5 years ago
parent 77bbd731c1
commit c29b77f88b
  1. 2
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  2. 14
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  3. 35
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  4. 6
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  5. 1
      app/src/main/res/values/strings.xml

@ -20,7 +20,7 @@ object AnalyzeTxtFile {
private const val BUFFER_SIZE = 512 * 1024
//没有标题的时候,每个章节的最大长度
private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024
private val cacheFolder: File by lazy {
val cacheFolder: File by lazy {
val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir

@ -1,8 +1,10 @@
package io.legado.app.model.localBook
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.App
import io.legado.app.data.entities.Book
import io.legado.app.utils.FileUtils
object LocalBook {
@ -27,4 +29,16 @@ object LocalBook {
}
}
fun deleteBook(book: Book, deleteOriginal: Boolean) {
if (book.isTxt()) {
val bookFile =
FileUtils.getFile(AnalyzeTxtFile.cacheFolder, book.originName, subDirs = *arrayOf())
bookFile.delete()
}
if (deleteOriginal) {
val uri = Uri.parse(book.bookUrl)
DocumentFile.fromSingleUri(App.INSTANCE, uri)?.delete()
}
}
}

@ -20,6 +20,9 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BlurTransformation
import io.legado.app.help.ImageLoader
import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.okButton
import io.legado.app.ui.audio.AudioPlayActivity
import io.legado.app.ui.book.group.GroupSelectDialog
import io.legado.app.ui.book.info.edit.BookInfoEditActivity
@ -186,9 +189,7 @@ class BookInfoActivity :
}
tv_shelf.onClick {
if (viewModel.inBookshelf) {
viewModel.delBook {
upTvBookshelf()
}
deleteBook()
} else {
viewModel.addToBookshelf {
upTvBookshelf()
@ -223,6 +224,32 @@ class BookInfoActivity :
}
}
private fun deleteBook() {
viewModel.bookData.value?.let {
if (it.isLocalBook()) {
alert(
titleResource = R.string.sure,
messageResource = R.string.sure_delete_book_file
) {
okButton {
viewModel.delBook(true) {
finish()
}
}
noButton {
viewModel.delBook(false) {
finish()
}
}
}
} else {
viewModel.delBook {
upTvBookshelf()
}
}
}
}
private fun openChapterList() {
if (viewModel.chapterListData.value.isNullOrEmpty()) {
toast(R.string.chapter_list_empty)
@ -320,7 +347,7 @@ class BookInfoActivity :
}
} else {
if (!viewModel.inBookshelf) {
viewModel.delBook(null)
viewModel.delBook()
}
}
}

@ -11,6 +11,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.model.localBook.LocalBook
import kotlinx.coroutines.Dispatchers.IO
class BookInfoViewModel(application: Application) : BaseViewModel(application) {
@ -198,9 +199,12 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
}
}
fun delBook(success: (() -> Unit)?) {
fun delBook(deleteOriginal: Boolean = false, success: (() -> Unit)? = null) {
execute {
bookData.value?.let {
if (it.isLocalBook()) {
LocalBook.deleteBook(it, deleteOriginal)
}
App.db.bookDao().delete(it)
}
inBookshelf = false

@ -630,4 +630,5 @@
<string name="plus"></string>
<string name="other_aloud_setting">其它朗读设置</string>
<string name="system_typeface">系统内置字体样式</string>
<string name="sure_delete_book_file">是否删除源文件</string>
</resources>

Loading…
Cancel
Save