From 4f465fcb692c303a196ea19fad51e2b44ffc6ea9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 21 Nov 2021 12:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/main/bookshelf/BookshelfViewModel.kt | 38 ++++++++++++++----- .../io/legado/app/utils/GsonExtensions.kt | 4 +- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index 4366edfaf..471a0d976 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.main.bookshelf import android.app.Application +import com.google.gson.stream.JsonWriter import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb @@ -14,6 +15,9 @@ import io.legado.app.model.webBook.WebBook import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.isActive +import java.io.File +import java.io.FileOutputStream +import java.io.OutputStreamWriter class BookshelfViewModel(application: Application) : BaseViewModel(application) { @@ -66,19 +70,33 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } } - fun exportBookshelf(books: List?, success: (json: String) -> Unit) { + fun exportBookshelf(books: List?, success: (file: File) -> Unit) { execute { - val exportList = arrayListOf>() - books?.forEach { - val bookMap = hashMapOf() - bookMap["name"] = it.name - bookMap["author"] = it.author - bookMap["intro"] = it.getDisplayIntro() - exportList.add(bookMap) - } - GSON.toJson(exportList) + books?.let { + val path = "${context.filesDir}/books.json" + FileUtils.delete(path) + val file = FileUtils.createFileWithReplace(path) + @Suppress("BlockingMethodInNonBlockingContext") + FileOutputStream(file).use { out -> + val writer = JsonWriter(OutputStreamWriter(out, "UTF-8")) + writer.setIndent(" ") + writer.beginArray() + books.forEach { + val bookMap = hashMapOf() + bookMap["name"] = it.name + bookMap["author"] = it.author + bookMap["intro"] = it.getDisplayIntro() + GSON.toJson(bookMap, bookMap::class.java, writer) + } + writer.endArray() + writer.close() + } + file + } ?: throw NoStackTraceException("书籍不能为空") }.onSuccess { success(it) + }.onError { + context.toastOnUi("导出书籍出错\n${it.localizedMessage}") } } diff --git a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt index 138ed2821..c5535c407 100644 --- a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -49,12 +49,12 @@ fun Gson.writeToOutputStream(out: OutputStream, any: Any) { writer.beginArray() any.forEach { it?.let { - GSON.toJson(it, it::class.java, writer) + toJson(it, it::class.java, writer) } } writer.endArray() } else { - GSON.toJson(any, any::class.java, writer) + toJson(any, any::class.java, writer) } writer.close() }