From c29b77f88b63f2c38fd85c85da9beff0e4c2b3ff Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 2 Mar 2020 15:18:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/localBook/AnalyzeTxtFile.kt | 2 +- .../legado/app/model/localBook/LocalBook.kt | 14 ++++++++ .../app/ui/book/info/BookInfoActivity.kt | 35 ++++++++++++++++--- .../app/ui/book/info/BookInfoViewModel.kt | 6 +++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index fad747b34..16426a036 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -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 diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 44ee337b2..c58410cd7 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -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() + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index efcddba7c..7db4032aa 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -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() } } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 1a675e21a..0364696a6 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -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 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7903cadfb..e7122ec5f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -630,4 +630,5 @@ 其它朗读设置 系统内置字体样式 + 是否删除源文件