From 19e1d395f4a4c515f2751aad2a5054e911641800 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 11 Mar 2020 19:35:00 +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 --- .../legado/app/model/localBook/LocalBook.kt | 66 +++++++++++-------- .../app/ui/book/local/ImportBookViewModel.kt | 6 +- 2 files changed, 40 insertions(+), 32 deletions(-) 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 fc94613d8..c5e7ffab5 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 @@ -6,37 +6,43 @@ import io.legado.app.App import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp import io.legado.app.utils.FileUtils +import io.legado.app.utils.isContentPath +import java.io.File object LocalBook { - fun importFile(doc: DocumentFile) { - doc.name?.let { fileName -> - val str = fileName.substringBeforeLast(".") - val authorIndex = str.indexOf("作者") - var name: String - var author: String - if (authorIndex == -1) { - name = str - author = "" - } else { - name = str.substring(0, authorIndex) - author = str.substring(authorIndex) - author = BookHelp.formatAuthor(author) - } - val smhStart = name.indexOf("《") - val smhEnd = name.indexOf("》") - if (smhStart != -1 && smhEnd != -1) { - name = (name.substring(smhStart + 1, smhEnd)) - } - val book = Book( - bookUrl = doc.uri.toString(), - name = name, - author = author, - originName = fileName - ) - App.db.bookDao().insert(book) + fun importFile(path: String) { + val fileName = if (path.isContentPath()) { + val doc = DocumentFile.fromSingleUri(App.INSTANCE, Uri.parse(path)) + doc?.name ?: "" + } else { + File(path).name + } + val str = fileName.substringBeforeLast(".") + val authorIndex = str.indexOf("作者") + var name: String + var author: String + if (authorIndex == -1) { + name = str + author = "" + } else { + name = str.substring(0, authorIndex) + author = str.substring(authorIndex) + author = BookHelp.formatAuthor(author) + } + val smhStart = name.indexOf("《") + val smhEnd = name.indexOf("》") + if (smhStart != -1 && smhEnd != -1) { + name = (name.substring(smhStart + 1, smhEnd)) } + val book = Book( + bookUrl = path, + name = name, + author = author, + originName = fileName + ) + App.db.bookDao().insert(book) } fun deleteBook(book: Book, deleteOriginal: Boolean) { @@ -47,8 +53,12 @@ object LocalBook { } if (deleteOriginal) { - val uri = Uri.parse(book.bookUrl) - DocumentFile.fromSingleUri(App.INSTANCE, uri)?.delete() + if (book.bookUrl.isContentPath()) { + val uri = Uri.parse(book.bookUrl) + DocumentFile.fromSingleUri(App.INSTANCE, uri)?.delete() + } else { + FileUtils.deleteFile(book.bookUrl) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt index 847a692bd..d3be59c5c 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt @@ -11,10 +11,8 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) fun addToBookshelf(uriList: HashSet, finally: () -> Unit) { execute { - uriList.forEach { uriStr -> - DocumentFile.fromSingleUri(context, Uri.parse(uriStr))?.let { doc -> - LocalBook.importFile(doc) - } + uriList.forEach { + LocalBook.importFile(it) } }.onFinally { finally.invoke()