Skip to content

Commit

Permalink
[ML4SE-241] fixed problem with Promise types.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrise2 authored and nbirillo committed Dec 1, 2023
1 parent 1014b2e commit 537d239
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,19 @@ data class Scenario(

@Suppress("ReturnCount")
fun getNextUnit(project: Project): ScenarioUnit? {
if (!currentStepIterator.notNullAndHasNext()) {
return null
}
cleanStepSettings()
val currentStep = getNextStep() ?: return null
currentStep.prepareSettings(project)
currentStepIterator = currentStep.getUnits().iterator()
return if (currentStepIterator.notNullAndHasNext()) {
currentStepIterator?.next()
} else {
null
if (currentStepIterator.notNullAndHasNext()) {
cleanStepSettings()
val currentStep = getNextStep() ?: return null
currentStep.prepareSettings(project)
currentStepIterator = currentStep.getUnits().iterator()
if (currentStepIterator.notNullAndHasNext()) {
return null
}
}
return currentStepIterator?.next()
}

private fun Iterator<ScenarioUnit>?.notNullAndHasNext() = this?.hasNext() ?: false
private fun Iterator<ScenarioUnit>?.notNullAndHasNext() = this?.hasNext() != true

private fun cleanStepSettings() =
MainPanelStorage.activeIdeHandlers.forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jetbrains.research.tasktracker.ui.main.panel

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.concurrency.Promise
import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig
import org.jetbrains.research.tasktracker.ui.main.panel.models.ButtonState
import org.jetbrains.research.tasktracker.ui.main.panel.models.LinkType
Expand Down Expand Up @@ -79,3 +81,12 @@ fun saveAgreements(agreementString: String) {
FileUtil.createIfDoesntExist(agreementFile)
agreementFile.writeText(agreementString)
}

fun<T> Promise<T>.runOnSuccess(task: (response: T) -> Unit) =
onSuccess {
ApplicationManager.getApplication().invokeLater {
task(it)
}
}.onError {
error(it.localizedMessage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.research.tasktracker.config.scenario.models.*
import org.jetbrains.research.tasktracker.tracking.TaskFileHandler
import org.jetbrains.research.tasktracker.tracking.activity.ActivityTracker
import org.jetbrains.research.tasktracker.ui.main.panel.MainPluginPanelFactory
import org.jetbrains.research.tasktracker.ui.main.panel.runOnSuccess
import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage
import org.jetbrains.research.tasktracker.ui.main.panel.template.*
import org.jetbrains.research.tasktracker.util.UIBundle
Expand All @@ -25,17 +26,13 @@ typealias Panel = MainPluginPanelFactory
fun Panel.agreementAcceptance() {
loadBasePage(AgreementTemplate.loadCurrentTemplate(), "ui.button.next", false)
setNextAction {
checkInputs()
.onSuccess {
if (!it) {
welcomePage()
} else {
notifyError(project, UIBundle.message("ui.please.fill"))
}
}
.onError {
error(it.localizedMessage)
checkInputs().runOnSuccess {
if (!it) {
welcomePage()
} else {
notifyError(project, UIBundle.message("ui.please.fill"))
}
}
}
}

Expand All @@ -58,10 +55,8 @@ private fun Panel.selectTask(taskIds: List<String>, allRequired: Boolean = true)
val tasks = TaskTrackerPlugin.mainConfig.taskContentConfig?.tasks?.filter { it.id in taskIds } ?: emptyList()
loadBasePage(TasksPageTemplate(tasks))
setNextAction {
mainWindow.getElementValue("tasks").onSuccess { name ->
mainWindow.getElementValue("tasks").runOnSuccess { name ->
solveTask(name.toString(), if (allRequired) taskIds.filter { it != name } else emptyList())
}.onError {
error(it.localizedMessage)
}
}
}
Expand Down

0 comments on commit 537d239

Please sign in to comment.