Skip to content

Commit f66debc

Browse files
committed
Fix: enable/disable start/stop actions after they were triggered
- also includes some refactorings
1 parent e2f8fe6 commit f66debc

File tree

1 file changed

+58
-56
lines changed

1 file changed

+58
-56
lines changed

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

+58-56
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.coder.gateway.sdk.getOS
2222
import com.coder.gateway.sdk.toURL
2323
import com.coder.gateway.sdk.v2.models.Workspace
2424
import com.coder.gateway.sdk.withPath
25+
import com.intellij.ide.ActivityTracker
2526
import com.intellij.ide.BrowserUtil
2627
import com.intellij.ide.IdeBundle
2728
import com.intellij.openapi.Disposable
@@ -85,9 +86,6 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
8586
WorkspaceStatusColumnInfo("Status")
8687
)
8788

88-
private val startWorkspaceAction = StartWorkspaceAction()
89-
private val stopWorkspaceAction = StopWorkspaceAction()
90-
9189
private var tableOfWorkspaces = TableView(listTableModelOfWorkspaces).apply {
9290
setEnableAntialiasing(true)
9391
rowSelectionAllowed = true
@@ -103,24 +101,13 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
103101
setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
104102
selectionModel.addListSelectionListener {
105103
enableNextButtonCallback(selectedObject != null && selectedObject?.agentStatus == RUNNING)
106-
when (selectedObject?.agentStatus) {
107-
RUNNING -> {
108-
startWorkspaceAction.isEnabled = false
109-
stopWorkspaceAction.isEnabled = true
110-
}
111-
112-
STOPPED, FAILED -> {
113-
startWorkspaceAction.isEnabled = true
114-
stopWorkspaceAction.isEnabled = false
115-
}
116-
117-
else -> {
118-
disableAllWorkspaceActions()
119-
}
120-
}
104+
updateWorkspaceActions()
121105
}
122106
}
123107

108+
private val startWorkspaceAction = StartWorkspaceAction()
109+
private val stopWorkspaceAction = StopWorkspaceAction()
110+
124111
private val toolbar = ToolbarDecorator.createDecorator(tableOfWorkspaces)
125112
.disableAddAction()
126113
.disableRemoveAction()
@@ -178,7 +165,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
178165
withContext(Dispatchers.IO) {
179166
try {
180167
coderClient.startWorkspace(workspace.workspaceID, workspace.workspaceName)
181-
startWorkspaceAction.isEnabled = false
168+
loadWorkspaces()
182169
} catch (e: WorkspaceResponseException) {
183170
logger.warn("Could not build workspace ${workspace.name}, reason: $e")
184171
}
@@ -196,7 +183,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
196183
withContext(Dispatchers.IO) {
197184
try {
198185
coderClient.stopWorkspace(workspace.workspaceID, workspace.workspaceName)
199-
stopWorkspaceAction.isEnabled = false
186+
loadWorkspaces()
200187
} catch (e: WorkspaceResponseException) {
201188
logger.warn("Could not stop workspace ${workspace.name}, reason: $e")
202189
}
@@ -206,14 +193,29 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
206193
}
207194
}
208195

209-
private fun disableAllWorkspaceActions() {
210-
startWorkspaceAction.isEnabled = false
211-
stopWorkspaceAction.isEnabled = false
212-
}
213-
214196
override fun onInit(wizardModel: CoderWorkspacesWizardModel) {
215197
enableNextButtonCallback(false)
216-
disableAllWorkspaceActions()
198+
updateWorkspaceActions()
199+
}
200+
201+
private fun updateWorkspaceActions() {
202+
when (tableOfWorkspaces.selectedObject?.agentStatus) {
203+
RUNNING -> {
204+
startWorkspaceAction.isEnabled = false
205+
stopWorkspaceAction.isEnabled = true
206+
}
207+
208+
STOPPED, FAILED -> {
209+
startWorkspaceAction.isEnabled = true
210+
stopWorkspaceAction.isEnabled = false
211+
}
212+
213+
else -> {
214+
startWorkspaceAction.isEnabled = false
215+
stopWorkspaceAction.isEnabled = false
216+
}
217+
}
218+
ActivityTracker.getInstance().inc()
217219
}
218220

219221
private fun loginAndLoadWorkspace() {
@@ -272,7 +274,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
272274
token = wizardModel.token
273275
}
274276
ProgressManager.getInstance().run(authTask)
275-
loadWorkspaces()
277+
triggerWorkspacePolling()
276278
}
277279

278280
private fun askToken(): String? {
@@ -296,32 +298,32 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
296298
}
297299
}
298300

299-
private fun loadWorkspaces() {
301+
private fun triggerWorkspacePolling() {
300302
poller?.cancel()
301303

302304
poller = cs.launch {
303305
while (isActive) {
304-
val workspaceList = withContext(Dispatchers.IO) {
305-
try {
306-
return@withContext coderClient.workspaces().collectAgents()
307-
} catch (e: Exception) {
308-
logger.error("Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL}. Reason: $e")
309-
emptyList()
310-
}
311-
}
306+
loadWorkspaces()
307+
delay(5000)
308+
}
309+
}
310+
}
312311

313-
val selectedWorkspace = withContext(Dispatchers.Main) {
314-
tableOfWorkspaces.selectedObject
315-
}
312+
private suspend fun loadWorkspaces() {
313+
val workspaceList = withContext(Dispatchers.IO) {
314+
try {
315+
return@withContext coderClient.workspaces().collectAgents()
316+
} catch (e: Exception) {
317+
logger.error("Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL}. Reason: $e")
318+
emptyList()
319+
}
320+
}
316321

317-
// if we just run the update on the main dispatcher, the code will block because it cant get some AWT locks
318-
ApplicationManager.getApplication().invokeLater {
319-
listTableModelOfWorkspaces.updateItems(workspaceList)
320-
if (selectedWorkspace != null) {
321-
tableOfWorkspaces.selectItem(selectedWorkspace)
322-
}
323-
}
324-
delay(5000)
322+
withContext(Dispatchers.Main) {
323+
val selectedWorkspace = tableOfWorkspaces.selectedObject?.name
324+
listTableModelOfWorkspaces.items = workspaceList
325+
if (selectedWorkspace != null) {
326+
tableOfWorkspaces.selectItem(selectedWorkspace)
325327
}
326328
}
327329
}
@@ -510,15 +512,15 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
510512
}
511513
}
512514

513-
private fun ListTableModel<WorkspaceAgentModel>.updateItems(workspaces: Collection<WorkspaceAgentModel>) {
514-
while (this.rowCount > 0) this.removeRow(0)
515-
this.addRows(workspaces)
516-
}
517-
518-
private fun TableView<WorkspaceAgentModel>.selectItem(workspace: WorkspaceAgentModel) {
519-
this.items.forEachIndexed { index, workspaceAgentModel ->
520-
if (workspaceAgentModel.name == workspace.name)
521-
this.setRowSelectionInterval(index, index)
515+
private fun TableView<WorkspaceAgentModel>.selectItem(workspaceName: String?) {
516+
if (workspaceName != null) {
517+
this.items.forEachIndexed { index, workspaceAgentModel ->
518+
if (workspaceAgentModel.name == workspaceName) {
519+
selectionModel.addSelectionInterval(convertRowIndexToView(index), convertRowIndexToView(index))
520+
// fix cell selection case
521+
columnModel.selectionModel.addSelectionInterval(0, columnCount - 1)
522+
}
523+
}
522524
}
523525
}
524526

0 commit comments

Comments
 (0)