|
|
@ -3,9 +3,11 @@ package io.legado.app.ui.download |
|
|
|
import android.app.Application |
|
|
|
import android.app.Application |
|
|
|
import android.net.Uri |
|
|
|
import android.net.Uri |
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
import io.legado.app.App |
|
|
|
|
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
|
|
|
|
import io.legado.app.utils.FileUtils |
|
|
|
|
|
|
|
import io.legado.app.utils.isContentPath |
|
|
|
|
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DownloadViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class DownloadViewModel(application: Application) : BaseViewModel(application) { |
|
|
@ -13,18 +15,30 @@ class DownloadViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
|
|
fun export(path: String, book: Book) { |
|
|
|
fun export(path: String, book: Book) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
val uri = Uri.parse(path) |
|
|
|
if (path.isContentPath()) { |
|
|
|
DocumentFile.fromTreeUri(context, uri) |
|
|
|
val uri = Uri.parse(path) |
|
|
|
?.createFile("txt", book.name) |
|
|
|
DocumentFile.fromTreeUri(context, uri) |
|
|
|
?.let { |
|
|
|
?.createFile("txt", book.name) |
|
|
|
context.contentResolver.openOutputStream(it.uri)?.let { |
|
|
|
?.let { |
|
|
|
App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { |
|
|
|
export(it.uri, book) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
FileUtils.createFolderIfNotExist(path).let { |
|
|
|
|
|
|
|
export(it, book) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}.onError { |
|
|
|
|
|
|
|
toast(it.localizedMessage ?: "ERROR") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun export(uri: Uri, book: Book) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun export(file: File, book: Book) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |