From b4d2de8717f3f02aa2509e0595194c82235c0c3b Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 19 Feb 2020 11:47:16 +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/ui/download/DownloadViewModel.kt | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt b/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt index 94af92ca0..069acf6f4 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt @@ -8,8 +8,10 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp +import io.legado.app.utils.DocumentUtils import io.legado.app.utils.FileUtils import io.legado.app.utils.isContentPath +import io.legado.app.utils.writeText import java.io.File @@ -20,28 +22,25 @@ class DownloadViewModel(application: Application) : BaseViewModel(application) { execute { if (path.isContentPath()) { val uri = Uri.parse(path) - DocumentFile.fromTreeUri(context, uri) - ?.createFile("txt", book.name) - ?.let { - export(it.uri, book) - } - } else { - FileUtils.createFolderIfNotExist(path).let { + DocumentFile.fromTreeUri(context, uri)?.let { export(it, book) } + } else { + export(FileUtils.createFolderIfNotExist(path), book) } }.onError { toast(it.localizedMessage ?: "ERROR") } } - - private fun export(uri: Uri, book: Book) { - + private fun export(doc: DocumentFile, book: Book) { + DocumentUtils.createFileIfNotExist(doc, book.name) + ?.uri + ?.writeText(context, getAllContents(book)) } private fun export(file: File, book: Book) { - + FileUtils.createFileIfNotExist(file, book.name).writeText(getAllContents(book)) } private fun getAllContents(book: Book): String {