|
|
@@ -3,7 +3,6 @@ package io.legado.app.ui.book.toc
|
|
|
|
|
|
import android.app.Application
|
|
|
import android.net.Uri
|
|
|
-import androidx.documentfile.provider.DocumentFile
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import io.legado.app.R
|
|
|
import io.legado.app.base.BaseViewModel
|
|
|
@@ -12,16 +11,11 @@ import io.legado.app.data.appDb
|
|
|
import io.legado.app.data.entities.Book
|
|
|
import io.legado.app.exception.NoStackTraceException
|
|
|
import io.legado.app.model.localBook.LocalBook
|
|
|
-import io.legado.app.utils.FileUtils
|
|
|
+import io.legado.app.utils.FileDoc
|
|
|
import io.legado.app.utils.GSON
|
|
|
-import io.legado.app.utils.isContentScheme
|
|
|
+import io.legado.app.utils.createFileIfNotExist
|
|
|
import io.legado.app.utils.toastOnUi
|
|
|
-import io.legado.app.utils.writeToOutputStream
|
|
|
-import java.io.File
|
|
|
-import java.io.FileOutputStream
|
|
|
-import java.text.SimpleDateFormat
|
|
|
-import java.util.Date
|
|
|
-import java.util.Locale
|
|
|
+import io.legado.app.utils.writeText
|
|
|
|
|
|
class TocViewModel(application: Application) : BaseViewModel(application) {
|
|
|
var bookUrl: String = ""
|
|
|
@@ -78,34 +72,17 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
|
|
|
bookMarkCallBack?.upBookmark(newText)
|
|
|
}
|
|
|
|
|
|
- @Suppress("BlockingMethodInNonBlockingContext")
|
|
|
fun saveBookmark(treeUri: Uri) {
|
|
|
execute {
|
|
|
val book = bookData.value
|
|
|
?: throw NoStackTraceException(context.getString(R.string.no_book))
|
|
|
- val dateFormat = SimpleDateFormat("yyMMddHHmmss", Locale.getDefault())
|
|
|
- val bookmark = appDb.bookmarkDao.getByBook(book.name, book.author)
|
|
|
- if (treeUri.isContentScheme()) {
|
|
|
- val doc = DocumentFile.fromTreeUri(context, treeUri)
|
|
|
- ?.createFile("", "bookmark-${dateFormat.format(Date())}")
|
|
|
- doc?.let {
|
|
|
- context.contentResolver.openOutputStream(doc.uri)!!.use {
|
|
|
- GSON.writeToOutputStream(it, bookmark)
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- val path = treeUri.path!!
|
|
|
- val file = FileUtils.createFileIfNotExist(
|
|
|
- File(path), "bookmark-${
|
|
|
- dateFormat.format(
|
|
|
- Date()
|
|
|
- )
|
|
|
- }"
|
|
|
+ val fileName = "bookmark-${book.name} ${book.author}"
|
|
|
+ val doc = FileDoc.fromUri(treeUri, true)
|
|
|
+ doc.createFileIfNotExist(fileName).writeText(
|
|
|
+ GSON.toJson(
|
|
|
+ appDb.bookmarkDao.getByBook(book.name, book.author)
|
|
|
)
|
|
|
- FileOutputStream(file).use {
|
|
|
- GSON.writeToOutputStream(it, bookmark)
|
|
|
- }
|
|
|
- }
|
|
|
+ )
|
|
|
}.onError {
|
|
|
AppLog.put("导出失败\n${it.localizedMessage}", it, true)
|
|
|
}.onSuccess {
|