网络请求
点击这里就出来一个弹框
更新Actions
关注这个
<actions>
<action id="TrelloAction"
class="com.anguomob.anguo.actions.trello.TrelloAction"
text="TrelloAction"
description="Move a trello card from to do to the next list"/>
<group id="MyPlugin.TopMnu" text="安果" description="安果专用组件">
<reference id="TrelloAction"/>
<action id="Demo Action"
class="com.anguomob.anguo.actions.HelloWordAction"
text="测试标题"
description="测试通知标题"
>
<keyboard-shortcut first-keystroke="ctrl alt p" keymap="$default"/>
</action>
<add-to-group group-id="MainMenu" anchor="last"/>
</group>
<group id="SecondGroup" text="_My Second Group" popup="true">
<add-to-group group-id="MyPlugin.TopMnu" anchor="first"/>
<reference id="TrelloAction"/>
</group>
</actions>
程序底部导入网络请求api
dependencies {
//导入 retrofit 和rxjava
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.retrofit2:adapter-rxjava2:2.9.0")
implementation("io.reactivex.rxjava2:rxjava:2.2.21")
implementation("io.reactivex.rxjava2:rxandroid:2.1.1")
//导入swing 0.3.0
implementation("com.github.akarnokd:rxjava2-swing:0.3.0")
}
出来弹框我们先看到的就是布局
直接从头开始写吧
package com.anguomob.anguo.actions.trello
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
class TrelloAction :AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val dialog=TrelloForm(event.project!!,TrelloInjectorImp())
dialog.show()
}
}
创建一个dialog 并且展示布局
采用了mvp模式
package com.anguomob.anguo.actions.trello
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
interface TrelloInjector {
val trelloRepository: TrelloRepository
val trelloServiceApi: TrelloServiceApi
fun trelloActionPresenter(view: TrelloFromView): TrelloActionPresenter
}
class TrelloInjectorImp : TrelloInjector {
override val trelloRepository: TrelloRepository by lazy {
TrelloRepositoryImp(trelloServiceApi)
}
override val trelloServiceApi: TrelloServiceApi by lazy {
Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create()).baseUrl("https://api.trello.com/").build()
.create(TrelloServiceApi::class.java)
}
override fun trelloActionPresenter(view: TrelloFromView): TrelloActionPresenter {
return TrelloActionPresenterImp(view, trelloRepository)
}
}
仓库api
package com.anguomob.anguo.actions.trello
import hu.akarnokd.rxjava2.swing.SwingSchedulers
import io.reactivex.Completable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
interface TrelloRepository {
fun getCards(formListId: String, key: String, token: String): Single<List<Card>>
fun moveCard(cardId: String, toListId: String, key: String, token: String): Completable
}
class TrelloRepositoryImp(private val trelloServiceApi: TrelloServiceApi) : TrelloRepository {
override fun getCards(formListId: String, key: String, token: String): Single<List<Card>> {
return trelloServiceApi.getCards(formListId, key, token).subscribeOn(Schedulers.io())
.observeOn(SwingSchedulers.edt())
}
override fun moveCard(cardId: String, toListId: String, key: String, token: String): Completable {
return trelloServiceApi.moveCardToList(cardId, toListId, key, token)
.subscribeOn(Schedulers.io())
.observeOn(SwingSchedulers.edt())
}
}
servicesApi
package com.anguomob.anguo.actions.trello
import io.reactivex.Completable
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query
interface TrelloServiceApi {
@GET("1/boards/{listId}/cards")
fun getCards(
@Path("listId") listId: String, @Query("key") key: String, @Query("token") token: String
): Single<List<Card>>
@PUT("1/cards/{cardId}")
fun moveCardToList(
@Path("cardId") cardId: String,
@Query("idList") idList: String,
@Query("key") key: String,
@Query("token") token: String
): Completable
}
数据bean
package com.anguomob.anguo.actions.trello
data class Card(val id: String, val name: String, val desc: String)
view点击产生的回调
package com.anguomob.anguo.actions.trello
interface TrelloFromView {
fun showCards(cards:List<Card>)
fun success(card: Card)
fun error(error: Throwable)
}
布局内容
package com.anguomob.anguo.actions.trello
import com.intellij.notification.NotificationDisplayType
import com.intellij.notification.NotificationGroup
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.layout.panel
import java.awt.Dimension
import javax.swing.*
class TrelloForm(
val project: Project, trelloInjector: TrelloInjector
) : DialogWrapper(project), TrelloFromView {
private var nameCombo: ComboBox<Card> = ComboBox<Card>().apply {
name = "nameCombo"
}
private var descriptionTextPanel: JTextPane = JTextPane().apply {
name = "descriptionTextPanel"
text = "test"
isEnabled = false
}
private val presenter: TrelloActionPresenter by lazy {
trelloInjector.trelloActionPresenter(this)
}
init {
init()
nameCombo.addActionListener {
val card = nameCombo.selectedItem as Card?
card?.let {
descriptionTextPanel.text = it.desc
}
}
presenter.loadCards()
}
override fun createCenterPanel(): JComponent = panel {
row("Name: ") {
nameCombo(grow)
}
row("Description") {
descriptionTextPanel()
}
}.apply {
minimumSize = Dimension(500, 340)
preferredSize = Dimension(500, 340)
}
override fun showCards(cards: List<Card>) {
cards.map {
nameCombo.addItem(it)
}
if (cards.isNotEmpty()) {
nameCombo.selectedIndex = 0
}
}
override fun success(card: Card) {
close(OK_EXIT_CODE)
ApplicationManager.getApplication().invokeLater {
//创建通知
val notification = NotificationGroup("success", NotificationDisplayType.BALLOON, true).createNotification(
"成功啦",
"Move card to newList",
NotificationType.INFORMATION,
null
)
notification.notify(project)
}
}
override fun error(error: Throwable) {
ApplicationManager.getApplication().invokeLater {
//创建通知
val notification = NotificationGroup("error", NotificationDisplayType.BALLOON, true).createNotification(
"error",
error.localizedMessage,
NotificationType.INFORMATION,
null
)
notification.notify(project)
}
}
}
核心代码
package com.anguomob.anguo.actions.trello
interface TrelloActionPresenter {
fun loadCards()
fun moveCard(card: Card)
}
class TrelloActionPresenterImp(private val view: TrelloFromView, private val repository: TrelloRepository) :
TrelloActionPresenter {
val fromListId="xxx"
val toListId="xxx"
val apiKey="xx"
val token="xxxxx"
override fun loadCards() {
repository.getCards(fromListId, apiKey, token)
.subscribe({ cards -> view.showCards(cards) }, { error -> view.error(error) })
}
override fun moveCard(card: Card) {
repository.moveCard(card.id, toListId, apiKey, token)
.subscribe({ view.success(card) }, { error -> view.error(error) })
}
}
moveCard 还没用到。
这个loadCards用到了
这个用的是 fromListId
这个id可获取有点费劲
来我教你们
GET获取看板列表
https://api.trello.com/1/members/me/boards?key={{APIKey}}&token={{APIToken}}
var jsonData = pm.response.json();
var boardCardId= jsonData['0']['id']
pm.environment.set("BoardCardId", boardCardId);
然后取出来第一个参数 那么这个参数就是看板咯
看板的cards的 列表id就是这个。
稍微一等 布局就会加载列表
并且选了以后可以把描述打出来
这个插件功能先到这里咯。更多精彩我还在学习。。