Skip to content

Commit

Permalink
重构版本内联修复
Browse files Browse the repository at this point in the history
  • Loading branch information
mdddj committed Jul 3, 2024
1 parent 4649f26 commit 4e628fb
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 153 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package shop.itbug.fluttercheckversionx.actions

import com.google.gson.Gson
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import kotlinx.serialization.json.Json
import shop.itbug.fluttercheckversionx.form.socket.Request
import shop.itbug.fluttercheckversionx.socket.service.AppService

Expand All @@ -13,12 +13,12 @@ fun Request.getDataJson(): Any {
}
if (data is String) {
return try {
Json.decodeFromString<Map<String, Any>>(data)
Gson().fromJson(data, Map::class.java)
} catch (e: Exception) {
data
}
}
return data ?: ""
return data
}

///获取当前选中的项目
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.components.JBScrollPane
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import shop.itbug.fluttercheckversionx.common.jsonToFreezedRun
import shop.itbug.fluttercheckversionx.i18n.PluginBundle
import shop.itbug.fluttercheckversionx.tools.emptyBorder
import shop.itbug.fluttercheckversionx.util.findPropertiesMaxLenObject
import shop.itbug.fluttercheckversionx.widget.JsonEditorTextPanel
import javax.swing.JComponent

Expand Down Expand Up @@ -56,34 +52,6 @@ class JsonToFreezedInputDialog(val project: Project) : DialogWrapper(project) {
}


/**
* 获取可以转换的 json object
*/
fun String.getParseJsonObject(): JsonObject? {
if (this.validParseToFreezed()) {
val isArr = this.isJsonArray()
if (isArr) {
val parseArray = Json.parseToJsonElement(this) as JsonArray
val json = parseArray.findPropertiesMaxLenObject() //属性最多的那一个对象
return json.jsonObject
} else {
return Json.decodeFromString(this)
}

}
return null
}


fun String.isJsonArray(): Boolean {
return try {
Json.parseToJsonElement(this) is JsonArray
} catch (e: Exception) {
false
}
}


/**
* 判断是否可以转换为 freezed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun requestDetailPanel(request: Request, project: Project): DialogPanel {
}
row("Time") {
label("${request.timestamp}ms").apply {
if (request.timestamp != null && request.timestamp!! > 2000) {
if (request.timestamp > 2000) {
component.foreground = JBColor.ORANGE
} else {
component.foreground = UIUtil.getLabelInfoForeground()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package shop.itbug.fluttercheckversionx.form.sub

import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import com.google.gson.JsonSyntaxException
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.project.Project
import shop.itbug.fluttercheckversionx.widget.JsonEditorTextPanel


/**
* json viewer
*
Expand All @@ -29,6 +32,15 @@ open class JsonValueRender(p: Project) : JsonEditorTextPanel(p) {
}
}

private fun isValidJson(jsonString: String?): Boolean {
try {
JsonParser.parseString(jsonString)
return true
} catch (e: JsonSyntaxException) {
return false
}
}

/**
* 改变显示内容
*
Expand All @@ -38,10 +50,12 @@ open class JsonValueRender(p: Project) : JsonEditorTextPanel(p) {
val builder = GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()
if (json is String) {
try {
val map = builder.fromJson(json, Map::class.java)
return builder.toJson(map)
if (isValidJson(json)) {
val map = builder.fromJson(json, Map::class.java)
return builder.toJson(map)
}
} catch (e: Exception) {
e.printStackTrace()
println("尝试String转json失败:${e.localizedMessage} \n String is $json")
}
}
if (json is Map<*, *>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ class HiveBoxList : JBList<String>(), DioApiService.NativeMessageProcessing, Lis


override fun handleFlutterAppMessage(nativeMessage: String, jsonObject: Map<String, Any>?, aio: AioSession?) {
println("处理Hive盒子消息:$jsonObject")
jsonObject?.apply {
val type = this["type"]
if (type == "getBoxList") {
val data = jsonObject["data"]
if (data is Array<*>) {
model = ItemModel(data.map { it.toString() })
data?.let {
if (data is ArrayList<*>) {
model = ItemModel(data.map { it.toString() })
}
}

}
}
}
Expand Down Expand Up @@ -121,7 +125,7 @@ class HiveKeysList(private val boxList: JBList<String>) : JBList<String>(), DioA
val type = jsonObject["type"]
if (type == "getKeys") {
val data = jsonObject["data"]
if (data is Array<*>) {
if (data is ArrayList<*>) {
model = ItemModel(data.map { it.toString() })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class HiveValueComponent(project: Project) : BorderLayoutPanel(), DioApiService.
}

override fun handleFlutterAppMessage(nativeMessage: String, jsonObject: Map<String, Any>?, aio: AioSession?) {
println("处理Hive值:$jsonObject")
jsonObject?.apply {
val type = jsonObject["type"]
if (type == "getValue" && get("data") != null) {
if (get("data") != null && get("type") == "getValue") {
changeJsonValue(get("data"))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package shop.itbug.fluttercheckversionx.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import shop.itbug.fluttercheckversionx.util.*


Expand Down Expand Up @@ -41,7 +40,6 @@ fun PubVersionDataModel.getLastVersionText(model: DartPluginVersionName): String
}


@Serializable
data class PubVersionDataModel(
val name: String, val latest: Latest, val versions: List<Version>
) {
Expand All @@ -65,22 +63,19 @@ data class PubVersionDataModel(
val lastVersionUpdateTimeString get() = getLastUpdateTime()
}

@Serializable
data class Latest(
val version: String,
val pubspec: Pubspec,
@SerialName("archive_url") val archiveURL: String,
val published: String
)

@Serializable
data class Pubspec(
val name: String, val version: String, val homepage: String?, val description: String
)

val Pubspec.dartPluginModel get() = DartPluginVersionName(name, version)

@Serializable
data class Version(
val version: String, val published: String, val pubspec: Pubspec
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package shop.itbug.fluttercheckversionx.services

import PluginVersionModel
import com.google.gson.Gson
import com.intellij.util.io.HttpRequests
import kotlinx.serialization.json.Json
import shop.itbug.fluttercheckversionx.model.PubSearchResult
import shop.itbug.fluttercheckversionx.model.PubVersionDataModel

Expand All @@ -21,9 +21,9 @@ object PubService {
val url = "https://pub.dartlang.org/api/packages/$plugName"
try {
val resposne = HttpRequests.request(url).readString()
return Json.decodeFromString(resposne)
return Gson().fromJson(resposne, PubVersionDataModel::class.java)
} catch (e: Exception) {
println("获取插件信息失败${plugName}:${e.message} url:$url")
println("获取插件信息失败${plugName} url:$url ${e.localizedMessage}")
return null
}
}
Expand All @@ -35,7 +35,7 @@ object PubService {
val url = "https://pub.dartlang.org/packages/$pluginName.json"
try {
val resposne = HttpRequests.request(url).readString()
return Json.decodeFromString(resposne)
return Gson().fromJson(resposne, PluginVersionModel::class.java)
} catch (e: Exception) {
return null
}
Expand All @@ -48,7 +48,7 @@ object PubService {
val url = "https://pub.dartlang.org/api/search?q=$pluginName"
try {
val resposne = HttpRequests.request(url).readString()
return Json.decodeFromString(resposne)
return Gson().fromJson(resposne, PubSearchResult::class.java)
} catch (e: Exception) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ class DioApiService {
}

private fun sendMessage(message: String) {
println("发送JSON:$message")
sessions.forEach {
it.send(message)
}
}

fun sendByMap(map: Map<String, Any>) {
sendMessage(Gson().toJson(map))
}

fun sendByAnyObject(obj: Any) {
try {
val json: String = Gson().toJson(obj)
val map = Gson().fromJson(json, Map::class.java)
sendByMap(map.toMapAny)
if (obj is String) {
sendMessage(obj)
} else {
val jsonString = Gson().toJson(obj)
sendMessage(jsonString)
}
} catch (e: Exception) {
e.printStackTrace()
}
Expand All @@ -63,7 +63,6 @@ class DioApiService {

interface NativeMessageProcessing {


fun register() {
getInstance().addHandle(this)
}
Expand Down Expand Up @@ -92,9 +91,6 @@ class DioApiService {
try {
if (jsonObject != null && jsonObject["projectName"] != null) {
val model = Gson().fromJson(nativeMessage, ProjectSocketService.SocketResponseModel::class.java)
println("-----start")
println(model)
println("-----end")
handleModel(model)
}
} catch (e: Exception) {
Expand Down Expand Up @@ -130,7 +126,9 @@ object MyMessageProcessor : AbstractMessageProcessor<String>() {

private fun jsonStringToModel(msg: String, session: AioSession?) {
try {
println("收到message:$msg")
val json = Gson().fromJson(msg, Map::class.java).toMapAny
println("json is $json")
handle.forEach {
it.handleFlutterAppMessage(msg, json, session)
}
Expand Down
Loading

0 comments on commit 4e628fb

Please sign in to comment.