From c4221464e8ad6a9606a4b4d6588e085f8405ecd4 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sun, 12 May 2024 13:37:39 +0200 Subject: [PATCH] feat: implement new functionality in SDom --- .../kotlin/io/gitlab/jfronny/sdom/SDom.kt | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt b/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt index b46628b..367fbfd 100644 --- a/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt +++ b/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt @@ -1,10 +1,17 @@ package io.gitlab.jfronny.sdom import com.intellij.ide.util.PropertiesComponent +import com.intellij.openapi.actionSystem.* import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.diagnostic.LogLevel import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.project.Project +import io.gitlab.jfronny.sdom.actions.SDGetContestsAction +import io.gitlab.jfronny.sdom.actions.SDGetProblemsAction +import io.gitlab.jfronny.sdom.model.Contest +import io.gitlab.jfronny.sdom.model.Problem import io.gitlab.jfronny.sdom.model.SDLoginResult +import io.gitlab.jfronny.sdom.model.SDResult import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.engine.cio.* @@ -14,9 +21,12 @@ import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.request.* import io.ktor.http.* import io.ktor.serialization.kotlinx.json.* +import kotlinx.coroutines.flow.SharedFlow import kotlinx.serialization.json.Json object SDom { + private const val CONTEST_ID_PROPERTY = "io.gitlab.jfronny.sdom.contestId" + private val logger = Logger.getInstance(SDom.javaClass).apply { setLevel(LogLevel.DEBUG) } private var propertiesComponent: PropertiesComponent? = null @@ -35,8 +45,20 @@ object SDom { } } - private val loginListeners: MutableList<() -> Unit> = mutableListOf() //TODO add listener for refreshing tasks + private val loginListeners: MutableList<() -> Unit> = mutableListOf({ + SDGetContestsAction().actionPerformed( + AnActionEvent( + null, + DataContext.EMPTY_CONTEXT, + ActionPlaces.UNKNOWN, + Presentation(), + ActionManager.getInstance(), + 0 + ) + ) + }) private val logoutListeners: MutableList<() -> Unit> = mutableListOf() + private val resultFlowListeners: MutableList<(SharedFlow>, Problem) -> Unit> = mutableListOf() fun registerLoginListener(listener: () -> Unit) { loginListeners.add(listener) @@ -46,6 +68,10 @@ object SDom { logoutListeners.add(listener) } + fun registerResultFlowListener(listener: (SharedFlow>, Problem) -> Unit) { + resultFlowListeners.add(listener) + } + suspend fun login(username: String, password: String, url: String) { val fixedApi = url.trimEnd('/') + "/v4/" val result: SDLoginResult = client.get(url = Url(fixedApi + "user")).body() @@ -61,6 +87,53 @@ object SDom { SDCredentials.logOut() logoutListeners.forEach { ApplicationManager.getApplication().invokeLater(it) } } + + var contests: List = listOf() + var currentContest: Contest? = null + set(value) { + field = value + propertiesComponent?.setValue(CONTEST_ID_PROPERTY, value?.id, "") + } + var problems: List = listOf() + var currentProblem: Problem? = null + + suspend fun getContests() { + val result = client.get("${SDCredentials.url}contests") + if (result.status.isSuccess()) { + contests = result.body>() + + logger.warn(contests.toString()) + } + } + + fun loadProperties(project: Project?) { + propertiesComponent = project?.let { PropertiesComponent.getInstance(it) } ?: PropertiesComponent.getInstance() + val contestId = propertiesComponent?.getValue(CONTEST_ID_PROPERTY, "")?.takeIf { it != "" } + val contest = contests.find { it.id == contestId } + if (contestId != null && contest != null) { + currentContest = contest + + SDGetProblemsAction().actionPerformed( + AnActionEvent( + null, + DataContext.EMPTY_CONTEXT, + ActionPlaces.UNKNOWN, + Presentation(), + ActionManager.getInstance(), + 0 + ) + ) + } + } + + suspend fun getProblems() { + if (currentContest == null) return + val result = client.get("${SDCredentials.url}contests/${currentContest!!.id}/problems") + if (result.status.isSuccess()) { + problems = result.body>() + } + } + val loggedIn: Boolean get() = SDCredentials.credentials.first != null && SDCredentials.credentials.second != null } \ No newline at end of file