pull/1434/head
gedoor 3 years ago
parent 176a6114fd
commit 6180a654ae
  1. 6
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  2. 20
      app/src/main/java/io/legado/app/utils/GsonExtensions.kt

@ -10,6 +10,7 @@ import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.utils.* import io.legado.app.utils.*
import java.io.FileOutputStream
class BookSourceViewModel(application: Application) : BaseViewModel(application) { 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)) { fun shareSelection(sources: List<BookSource>, success: ((intent: Intent) -> Unit)) {
execute { execute {
val tmpSharePath = "${context.filesDir}/shareBookSource.json" val tmpSharePath = "${context.filesDir}/shareBookSource.json"
FileUtils.delete(tmpSharePath) FileUtils.delete(tmpSharePath)
val intent = Intent(Intent.ACTION_SEND) val intent = Intent(Intent.ACTION_SEND)
val file = FileUtils.createFileWithReplace(tmpSharePath) 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) val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file)
intent.type = "text/*" intent.type = "text/*"
intent.putExtra(Intent.EXTRA_STREAM, fileUri) intent.putExtra(Intent.EXTRA_STREAM, fileUri)

@ -3,7 +3,10 @@ package io.legado.app.utils
import com.google.gson.* import com.google.gson.*
import com.google.gson.internal.LinkedTreeMap import com.google.gson.internal.LinkedTreeMap
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonWriter
import timber.log.Timber import timber.log.Timber
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.lang.reflect.ParameterizedType import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type import java.lang.reflect.Type
import kotlin.math.ceil import kotlin.math.ceil
@ -39,6 +42,23 @@ inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? {
}.getOrNull() }.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 { class ParameterizedTypeImpl(private val clazz: Class<*>) : ParameterizedType {
override fun getRawType(): Type = List::class.java override fun getRawType(): Type = List::class.java

Loading…
Cancel
Save