This commit is contained in:
gedoor 2021-11-19 21:26:28 +08:00
parent 176a6114fd
commit 6180a654ae
2 changed files with 25 additions and 1 deletions

View File

@ -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<BookSource>, 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)

View File

@ -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 <reified T> Gson.fromJsonArray(json: String?): List<T>? {
}.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