Skip to content

Commit 2bbabc2

Browse files
committed
fix: cancel connection when back button is pressed
- by yielding the coroutine in three key points - error handling is also simplified.
1 parent 1da98ed commit 2bbabc2

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/main/kotlin/com/coder/toolbox/views/ConnectStep.kt

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.coder.toolbox.cli.CoderCLIManager
55
import com.coder.toolbox.cli.ensureCLI
66
import com.coder.toolbox.plugin.PluginManager
77
import com.coder.toolbox.sdk.CoderRestClient
8-
import com.coder.toolbox.util.humanizeConnectionError
98
import com.coder.toolbox.util.toURL
109
import com.coder.toolbox.views.state.AuthWizardState
1110
import com.jetbrains.toolbox.api.localization.LocalizableString
@@ -15,6 +14,10 @@ import com.jetbrains.toolbox.api.ui.components.ValidationErrorField
1514
import kotlinx.coroutines.Job
1615
import kotlinx.coroutines.flow.update
1716
import kotlinx.coroutines.launch
17+
import kotlinx.coroutines.yield
18+
import java.util.concurrent.CancellationException
19+
20+
private const val USER_HIT_THE_BACK_BUTTON = "User hit the back button"
1821

1922
/**
2023
* A page that connects a REST client and cli to Coder.
@@ -27,12 +30,9 @@ class ConnectStep(
2730
cli: CoderCLIManager,
2831
) -> Unit,
2932
) : WizardStep {
30-
private val settings = context.settingsStore.readOnly()
3133
private var signInJob: Job? = null
3234

3335
private val statusField = LabelField(context.i18n.pnotr(""))
34-
35-
// override val description: LocalizableString = context.i18n.pnotr("Please wait while we configure Toolbox for ${url.host}.")
3636
private val errorField = ValidationErrorField(context.i18n.pnotr(""))
3737

3838
override val panel: RowGroup = RowGroup(
@@ -75,21 +75,29 @@ class ConnectStep(
7575
proxyValues = null,
7676
PluginManager.pluginInfo.version,
7777
)
78+
// allows interleaving with the back/cancel action
79+
yield()
7880
client.authenticate()
79-
updateStatus(context.i18n.ptrl("Checking Coder binary..."), error = null)
81+
statusField.textState.update { (context.i18n.ptrl("Checking Coder binary...")) }
8082
val cli = ensureCLI(context, client.url, client.buildVersion)
8183
// We only need to log in if we are using token-based auth.
8284
if (client.token != null) {
83-
updateStatus(context.i18n.ptrl("Configuring CLI..."), error = null)
85+
statusField.textState.update { (context.i18n.ptrl("Configuring CLI...")) }
86+
// allows interleaving with the back/cancel action
87+
yield()
8488
cli.login(client.token)
8589
}
90+
// allows interleaving with the back/cancel action
91+
yield()
8692
onConnect(client, cli)
8793
AuthWizardState.resetSteps()
88-
94+
} catch (ex: CancellationException) {
95+
if (ex.message == USER_HIT_THE_BACK_BUTTON) {
96+
return@launch
97+
}
98+
notify("Connection to ${url.host} was configured", ex)
8999
} catch (ex: Exception) {
90-
val msg = humanizeConnectionError(url, settings.requireTokenAuth, ex)
91100
notify("Failed to configure ${url.host}", ex)
92-
updateStatus(context.i18n.pnotr("Failed to configure ${url.host}"), msg)
93101
}
94102
}
95103
}
@@ -99,24 +107,10 @@ class ConnectStep(
99107
}
100108

101109
override fun onBack() {
102-
AuthWizardState.goToPreviousStep()
103-
}
104-
105-
/**
106-
* Update the status and error fields then refresh.
107-
*/
108-
private fun updateStatus(newStatus: LocalizableString, error: String?) {
109-
statusField.textState.update { newStatus }
110-
if (!error.isNullOrBlank()) {
111-
errorField.textState.update { context.i18n.pnotr(error) }
110+
try {
111+
signInJob?.cancel(CancellationException(USER_HIT_THE_BACK_BUTTON))
112+
} finally {
113+
AuthWizardState.goToPreviousStep()
112114
}
113115
}
114-
//
115-
// /**
116-
// * Try connecting again after an error.
117-
// */
118-
// private fun retry() {
119-
// updateStatus(context.i18n.pnotr("Connecting to ${url.host}..."), null)
120-
// connect()
121-
// }
122116
}

0 commit comments

Comments
 (0)