feat: 优化代码

pull/105/head
kunfei 5 years ago
parent 99bbdb558d
commit 8cb833436e
  1. 6
      app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt
  2. 22
      app/src/main/java/io/legado/app/utils/DocumentUtils.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 {

@ -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 {
@ -139,3 +139,23 @@ data class DocItem(
DocumentsContract.Document.MIME_TYPE_DIR == attr
}
}
@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)
}

Loading…
Cancel
Save