From 6180a654aed3dca3804074e0cffc63ebe9be3392 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 19 Nov 2021 21:26:28 +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 --- .../book/source/manage/BookSourceViewModel.kt | 6 +++++- .../io/legado/app/utils/GsonExtensions.kt | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index d24047480..11d678a8f 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -10,6 +10,7 @@ import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.utils.* +import java.io.FileOutputStream class BookSourceViewModel(application: Application) : BaseViewModel(application) { @@ -135,13 +136,16 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } + @Suppress("BlockingMethodInNonBlockingContext") fun shareSelection(sources: List, success: ((intent: Intent) -> Unit)) { execute { val tmpSharePath = "${context.filesDir}/shareBookSource.json" FileUtils.delete(tmpSharePath) val intent = Intent(Intent.ACTION_SEND) val file = FileUtils.createFileWithReplace(tmpSharePath) - file.writeText(GSON.toJson(sources)) + FileOutputStream(file).use { + GSON.writeToOutputStream(it, sources) + } val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file) intent.type = "text/*" intent.putExtra(Intent.EXTRA_STREAM, fileUri) 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 08d82b2ff..138ed2821 100644 --- a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -3,7 +3,10 @@ package io.legado.app.utils import com.google.gson.* import com.google.gson.internal.LinkedTreeMap import com.google.gson.reflect.TypeToken +import com.google.gson.stream.JsonWriter import timber.log.Timber +import java.io.OutputStream +import java.io.OutputStreamWriter import java.lang.reflect.ParameterizedType import java.lang.reflect.Type import kotlin.math.ceil @@ -39,6 +42,23 @@ inline fun Gson.fromJsonArray(json: String?): List? { }.getOrNull() } +fun Gson.writeToOutputStream(out: OutputStream, any: Any) { + val writer = JsonWriter(OutputStreamWriter(out, "UTF-8")) + writer.setIndent(" ") + if (any is List<*>) { + writer.beginArray() + any.forEach { + it?.let { + GSON.toJson(it, it::class.java, writer) + } + } + writer.endArray() + } else { + GSON.toJson(any, any::class.java, writer) + } + writer.close() +} + class ParameterizedTypeImpl(private val clazz: Class<*>) : ParameterizedType { override fun getRawType(): Type = List::class.java