mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
176a6114fd
commit
6180a654ae
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user