pull/48/head
kunfei 5 years ago
parent 3d7a6ff6b1
commit aa52663f19
  1. 3
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  2. 3
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt
  3. 4
      app/src/main/java/io/legado/app/utils/GsonExtensions.kt

@ -11,6 +11,7 @@ import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.OldRule
import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.help.storage.Restore.jsonPath
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers
import java.io.File import java.io.File
class BookSourceViewModel(application: Application) : BaseViewModel(application) { class BookSourceViewModel(application: Application) : BaseViewModel(application) {
@ -130,7 +131,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
} }
fun importSource(text: String) { fun importSource(text: String) {
execute { execute(context = Dispatchers.Main) {
val text1 = text.trim() val text1 = text.trim()
if (text1.isJsonObject()) { if (text1.isJsonObject()) {
val json = JsonPath.parse(text1) val json = JsonPath.parse(text1)

@ -9,6 +9,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import kotlinx.coroutines.Dispatchers
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { class RssSourceEditViewModel(application: Application) : BaseViewModel(application) {
@ -50,7 +51,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
} }
fun pasteSource(onSuccess: (source: RssSource) -> Unit) { fun pasteSource(onSuccess: (source: RssSource) -> Unit) {
execute { execute(context = Dispatchers.Main) {
var source: RssSource? = null var source: RssSource? = null
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
clipboard?.primaryClip?.let { clipboard?.primaryClip?.let {

@ -2,6 +2,7 @@ package io.legado.app.utils
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import org.jetbrains.anko.attempt import org.jetbrains.anko.attempt
import java.lang.reflect.ParameterizedType import java.lang.reflect.ParameterizedType
@ -16,6 +17,8 @@ val GSON: Gson by lazy {
inline fun <reified T> genericType() = object : TypeToken<T>() {}.type inline fun <reified T> genericType() = object : TypeToken<T>() {}.type
@Throws(JsonSyntaxException::class)
inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型 inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型
return attempt { return attempt {
val result: T? = fromJson(json, genericType<T>()) val result: T? = fromJson(json, genericType<T>())
@ -23,6 +26,7 @@ inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意
}.value }.value
} }
@Throws(JsonSyntaxException::class)
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? { inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? {
return attempt { return attempt {
val result: List<T>? = fromJson(json, ParameterizedTypeImpl(T::class.java)) val result: List<T>? = fromJson(json, ParameterizedTypeImpl(T::class.java))

Loading…
Cancel
Save