Android Studio 插件开发3将数据存储到Service

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 插件开发3将数据存储到Service相关的知识,希望对你有一定的参考价值。

密码等数据放在代码里面 是看起来不太可行的。第一步优化可以先从一点一点开始

新建一个数据bean 存放这些密码。虽然密码还在这个地方。那最起码他有了动态表变化的能力

data class TrelloState(
    var apiKey:String="xxx",
    var token:String="xxx",
    var fromListId:String="xx",
    var toListId:String="xx",
)

新建package service 和 action同级

 

新建 

TrelloService

关注这个类就行了

package com.anguomob.anguo.services

import com.anguomob.anguo.actions.trello.TrelloState
import com.intellij.credentialStore.CredentialAttributes
import com.intellij.credentialStore.Credentials
import com.intellij.ide.passwordSafe.PasswordSafe
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project

@Service
@State(name = "TrelloConfiguration", storages = [Storage(value = "trelloConfiguration.xml")])
class TrelloService : PersistentStateComponent<TrelloState> 

    private var trelloStatus: TrelloState = TrelloState()

    override fun getState(): TrelloState = trelloStatus

    override fun loadState(state: TrelloState) 
        trelloStatus = state
    

    companion object 
        fun getInstance(project: Project): TrelloService = project.service()
    

需要添加到plugins.xml当中

 

    <extensions defaultExtensionNs="com.intellij">
        <projectService serviceImplementation="com.anguomob.anguo.services.TrelloService"/>

    </extensions>

然后针对代码做下优化  

TrelloActionPresenter 就是多价格一个参数 state
package com.anguomob.anguo.actions.trello

import com.intellij.notification.NotificationDisplayType
import com.intellij.notification.NotificationGroup
import com.intellij.notification.NotificationType
import org.jetbrains.kotlin.idea.caches.project.NotUnderContentRootModuleInfo.project

interface TrelloActionPresenter 
    fun loadCards()
    fun moveCard(card: Card)


class TrelloActionPresenterImp(
    private val view: TrelloFromView,
    private val repository: TrelloRepository,
    private val state: TrelloState
    ) :
    TrelloActionPresenter 


    override fun loadCards() 
        repository.getCards(state.fromListId, state.apiKey, state.token)
            .subscribe( cards -> view.showCards(cards) ,  error -> view.error(error) )
    

    override fun moveCard(card: Card) 
        repository.moveCard(card.id, state.toListId, state.apiKey,state. token)
            .subscribe( view.success(card) ,  error -> view.error(error) )
    

同理。

TrelloInjector 也会发生一些变化
package com.anguomob.anguo.actions.trello

import com.anguomob.anguo.services.TrelloService
import com.intellij.openapi.project.Project
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

interface TrelloInjector 
    val trelloRepository: TrelloRepository
    val trelloServiceApi: TrelloServiceApi
    fun trelloState(project: Project): TrelloState
    fun trelloActionPresenter(view: TrelloFromView, project: Project): TrelloActionPresenter


class TrelloInjectorImp : TrelloInjector 
    override val trelloRepository: TrelloRepository by lazy 
        TrelloRepositoryImp(trelloServiceApi)
    
    override val trelloServiceApi: TrelloServiceApi by lazy 

        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        val httpClient = OkHttpClient().newBuilder().addInterceptor(httpLoggingInterceptor)
            .connectTimeout(30 * 1000, TimeUnit.MILLISECONDS)
            .readTimeout(20 * 1000, TimeUnit.MILLISECONDS).build()



        //查看网络访问日志
        Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())

            .addConverterFactory(GsonConverterFactory.create())

            .baseUrl("https://api.trello.com/")
            .client(httpClient)
            .build()

            .create(TrelloServiceApi::class.java)
    

    override fun trelloState(project: Project): TrelloState 
        return TrelloService.getInstance(project).state
    

    override fun trelloActionPresenter(view: TrelloFromView, project: Project): TrelloActionPresenter 
        return TrelloActionPresenterImp(view, trelloRepository, trelloState(project))
    

但是其目的与插件开发2效果一致 就不做截图了

以上是关于Android Studio 插件开发3将数据存储到Service的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio Gradle Plugin开发入门指南

Android Studio Gradle Plugin开发入门指南

如何使用Android Studio开发Gradle插件

设置Android Studio开发无线调试

设置Android Studio开发无线调试

[Android Studio 1A] – 插件