From 8cb833436e3cc2f074d21de6a6d86ef3f19ea4bf Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 19 Feb 2020 14:33:06 +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 | 6 ++--- .../java/io/legado/app/utils/DocumentUtils.kt | 24 +++++++++++++++++-- 2 files changed, 25 insertions(+), 5 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 dc001a17b..da12d3d53 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 @@ -36,13 +36,13 @@ class DownloadViewModel(application: Application) : BaseViewModel(application) { } private fun export(doc: DocumentFile, book: Book) { - DocumentUtils.createFileIfNotExist(doc, book.name) - ?.uri + DocumentUtils.createFileIfNotExist(doc, "${book.name}.txt") ?.writeText(context, getAllContents(book)) } private fun export(file: File, book: Book) { - FileUtils.createFileIfNotExist(file, book.name).writeText(getAllContents(book)) + FileUtils.createFileIfNotExist(file, "${book.name}.txt") + .writeText(getAllContents(book)) } private fun getAllContents(book: Book): String { diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index e10cd8ccf..f4fd0e8eb 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -7,7 +7,7 @@ import android.provider.DocumentsContract import androidx.documentfile.provider.DocumentFile import java.util.* -@Suppress("unused") + object DocumentUtils { fun exists(root: DocumentFile, fileName: String, vararg subDirs: String): Boolean { @@ -138,4 +138,24 @@ data class DocItem( val isDir: Boolean by lazy { DocumentsContract.Document.MIME_TYPE_DIR == attr } -} \ No newline at end of file +} + +@Throws(Exception::class) +fun DocumentFile.writeText(context: Context, data: String) { + DocumentUtils.writeText(context, data, this.uri) +} + +@Throws(Exception::class) +fun DocumentFile.writeBytes(context: Context, data: ByteArray) { + DocumentUtils.writeBytes(context, data, this.uri) +} + +@Throws(Exception::class) +fun DocumentFile.readText(context: Context): String? { + return DocumentUtils.readText(context, this.uri) +} + +@Throws(Exception::class) +fun DocumentFile.readBytes(context: Context): ByteArray? { + return DocumentUtils.readBytes(context, this.uri) +}