feat: implement new functionality in SDom
This commit is contained in:
parent
d44e00afa0
commit
c4221464e8
@ -1,10 +1,17 @@
|
|||||||
package io.gitlab.jfronny.sdom
|
package io.gitlab.jfronny.sdom
|
||||||
|
|
||||||
import com.intellij.ide.util.PropertiesComponent
|
import com.intellij.ide.util.PropertiesComponent
|
||||||
|
import com.intellij.openapi.actionSystem.*
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
import com.intellij.openapi.diagnostic.LogLevel
|
import com.intellij.openapi.diagnostic.LogLevel
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
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.SDLoginResult
|
||||||
|
import io.gitlab.jfronny.sdom.model.SDResult
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.call.*
|
import io.ktor.client.call.*
|
||||||
import io.ktor.client.engine.cio.*
|
import io.ktor.client.engine.cio.*
|
||||||
@ -14,9 +21,12 @@ import io.ktor.client.plugins.contentnegotiation.*
|
|||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.serialization.kotlinx.json.*
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
object SDom {
|
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 val logger = Logger.getInstance(SDom.javaClass).apply { setLevel(LogLevel.DEBUG) }
|
||||||
private var propertiesComponent: PropertiesComponent? = null
|
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 logoutListeners: MutableList<() -> Unit> = mutableListOf()
|
||||||
|
private val resultFlowListeners: MutableList<(SharedFlow<Result<SDResult>>, Problem) -> Unit> = mutableListOf()
|
||||||
|
|
||||||
fun registerLoginListener(listener: () -> Unit) {
|
fun registerLoginListener(listener: () -> Unit) {
|
||||||
loginListeners.add(listener)
|
loginListeners.add(listener)
|
||||||
@ -46,6 +68,10 @@ object SDom {
|
|||||||
logoutListeners.add(listener)
|
logoutListeners.add(listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun registerResultFlowListener(listener: (SharedFlow<Result<SDResult>>, Problem) -> Unit) {
|
||||||
|
resultFlowListeners.add(listener)
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun login(username: String, password: String, url: String) {
|
suspend fun login(username: String, password: String, url: String) {
|
||||||
val fixedApi = url.trimEnd('/') + "/v4/"
|
val fixedApi = url.trimEnd('/') + "/v4/"
|
||||||
val result: SDLoginResult = client.get(url = Url(fixedApi + "user")).body()
|
val result: SDLoginResult = client.get(url = Url(fixedApi + "user")).body()
|
||||||
@ -61,6 +87,53 @@ object SDom {
|
|||||||
SDCredentials.logOut()
|
SDCredentials.logOut()
|
||||||
logoutListeners.forEach { ApplicationManager.getApplication().invokeLater(it) }
|
logoutListeners.forEach { ApplicationManager.getApplication().invokeLater(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var contests: List<Contest> = listOf()
|
||||||
|
var currentContest: Contest? = null
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
propertiesComponent?.setValue(CONTEST_ID_PROPERTY, value?.id, "")
|
||||||
|
}
|
||||||
|
var problems: List<Problem> = listOf()
|
||||||
|
var currentProblem: Problem? = null
|
||||||
|
|
||||||
|
suspend fun getContests() {
|
||||||
|
val result = client.get("${SDCredentials.url}contests")
|
||||||
|
if (result.status.isSuccess()) {
|
||||||
|
contests = result.body<List<Contest>>()
|
||||||
|
|
||||||
|
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<List<Problem>>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val loggedIn: Boolean
|
val loggedIn: Boolean
|
||||||
get() = SDCredentials.credentials.first != null && SDCredentials.credentials.second != null
|
get() = SDCredentials.credentials.first != null && SDCredentials.credentials.second != null
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user