|
| 1 | +package com.coder.toolbox.feed |
| 2 | + |
| 3 | +import com.squareup.moshi.FromJson |
| 4 | +import com.squareup.moshi.Json |
| 5 | +import com.squareup.moshi.JsonClass |
| 6 | +import com.squareup.moshi.ToJson |
| 7 | + |
| 8 | +/** |
| 9 | + * Represents a JetBrains IDE product from the feed API. |
| 10 | + * |
| 11 | + * The API returns an array of products, each with a code and a list of releases. |
| 12 | + */ |
| 13 | +@JsonClass(generateAdapter = true) |
| 14 | +data class IdeProduct( |
| 15 | + @Json(name = "code") val code: String, |
| 16 | + @Json(name = "intellijProductCode") val intellijProductCode: String, |
| 17 | + @Json(name = "name") val name: String, |
| 18 | + @Json(name = "releases") val releases: List<IdeRelease> = emptyList() |
| 19 | +) |
| 20 | + |
| 21 | +/** |
| 22 | + * Represents an individual release of a JetBrains IDE product. |
| 23 | + */ |
| 24 | +@JsonClass(generateAdapter = true) |
| 25 | +data class IdeRelease( |
| 26 | + @Json(name = "build") val build: String, |
| 27 | + @Json(name = "version") val version: String, |
| 28 | + @Json(name = "type") val type: IdeType, |
| 29 | + @Json(name = "date") val date: String |
| 30 | +) |
| 31 | + |
| 32 | +/** |
| 33 | + * Type of IDE release: release or EAP (Early Access Program) |
| 34 | + */ |
| 35 | +enum class IdeType { |
| 36 | + RELEASE, |
| 37 | + EAP, |
| 38 | + UNSUPPORTED; |
| 39 | + |
| 40 | + val value: String |
| 41 | + get() = when (this) { |
| 42 | + RELEASE -> "release" |
| 43 | + EAP -> "eap" |
| 44 | + UNSUPPORTED -> "unsupported" |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class IdeTypeAdapter { |
| 49 | + @FromJson |
| 50 | + fun fromJson(type: String): IdeType { |
| 51 | + return when (type.lowercase()) { |
| 52 | + "release" -> IdeType.RELEASE |
| 53 | + "eap" -> IdeType.EAP |
| 54 | + else -> IdeType.UNSUPPORTED |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @ToJson |
| 59 | + fun toJson(type: IdeType): String = type.value |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Simplified representation of an IDE for use in the plugin. |
| 64 | + * |
| 65 | + * Contains the essential information: product code, build number, version, and type. |
| 66 | + */ |
| 67 | +@JsonClass(generateAdapter = true) |
| 68 | +data class Ide( |
| 69 | + val code: String, |
| 70 | + val build: String, |
| 71 | + val version: String, |
| 72 | + val type: IdeType |
| 73 | +) { |
| 74 | + companion object { |
| 75 | + /** |
| 76 | + * Create an Ide from an IdeProduct and IdeRelease. |
| 77 | + */ |
| 78 | + fun from(product: IdeProduct, release: IdeRelease): Ide { |
| 79 | + return Ide( |
| 80 | + code = product.intellijProductCode, |
| 81 | + build = release.build, |
| 82 | + version = release.version, |
| 83 | + type = release.type |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments