@@ -6,11 +6,16 @@ import com.coder.toolbox.plugin.PluginManager
66import com.coder.toolbox.sdk.CoderRestClient
77import com.coder.toolbox.settings.CoderSettings
88import com.coder.toolbox.util.humanizeConnectionError
9+ import com.jetbrains.toolbox.api.core.ServiceLocator
10+ import com.jetbrains.toolbox.api.localization.LocalizableString
11+ import com.jetbrains.toolbox.api.localization.LocalizableStringFactory
912import com.jetbrains.toolbox.api.ui.actions.RunnableActionDescription
1013import com.jetbrains.toolbox.api.ui.components.LabelField
1114import com.jetbrains.toolbox.api.ui.components.UiField
1215import kotlinx.coroutines.CoroutineScope
1316import kotlinx.coroutines.Job
17+ import kotlinx.coroutines.flow.MutableStateFlow
18+ import kotlinx.coroutines.flow.StateFlow
1419import kotlinx.coroutines.launch
1520import okhttp3.OkHttpClient
1621import java.net.URL
@@ -19,22 +24,26 @@ import java.net.URL
1924 * A page that connects a REST client and cli to Coder.
2025 */
2126class ConnectPage (
27+ private val serviceLocator : ServiceLocator ,
2228 private val url : URL ,
2329 private val token : String? ,
2430 private val settings : CoderSettings ,
2531 private val httpClient : OkHttpClient ,
26- private val coroutineScope : CoroutineScope ,
32+ title : LocalizableString ,
2733 private val onCancel : () -> Unit ,
2834 private val onConnect : (
2935 client: CoderRestClient ,
3036 cli: CoderCLIManager ,
3137 ) -> Unit ,
32- ) : CoderPage(" Connecting to Coder" ) {
38+ ) : CoderPage(serviceLocator, title) {
39+ private val coroutineScope = serviceLocator.getService(CoroutineScope ::class .java)
40+ private val i18n = serviceLocator.getService(LocalizableStringFactory ::class .java)
41+
3342 private var signInJob: Job ? = null
3443
35- private var statusField = LabelField (" Connecting to ${url.host} ..." )
44+ private var statusField = LabelField (i18n.pnotr( " Connecting to ${url.host} ..." ) )
3645
37- override val description: String = " Please wait while we configure Toolbox for ${url.host} ."
46+ override val description: LocalizableString = i18n.pnotr( " Please wait while we configure Toolbox for ${url.host} ." )
3847
3948 init {
4049 connect()
@@ -45,23 +54,26 @@ class ConnectPage(
4554 *
4655 * TODO@JB: This looks kinda sparse. A centered spinner would be welcome.
4756 */
48- override val fields: MutableList <UiField > = listOfNotNull(
57+ override val fields: StateFlow <List <UiField >> = MutableStateFlow (
58+ listOfNotNull(
4959 statusField,
50- errorField,
51- ).toMutableList()
60+ errorField
61+ )
62+ )
5263
5364 /* *
5465 * Show a retry button on error.
5566 */
56- override val actionButtons: MutableList <RunnableActionDescription > = listOfNotNull(
57- if (errorField != null ) Action (" Retry" , closesPage = false ) { retry() } else null ,
58- if (errorField != null ) Action (" Cancel" , closesPage = false ) { onCancel() } else null ,
59- ).toMutableList()
67+ override val actionButtons: StateFlow <List <RunnableActionDescription >> = MutableStateFlow (
68+ listOfNotNull(
69+ if (errorField != null ) Action (i18n.ptrl(" Retry" ), closesPage = false ) { retry() } else null ,
70+ if (errorField != null ) Action (i18n.ptrl(" Cancel" ), closesPage = false ) { onCancel() } else null ,
71+ ))
6072
6173 /* *
6274 * Update the status and error fields then refresh.
6375 */
64- private fun updateStatus (newStatus : String , error : String? ) {
76+ private fun updateStatus (newStatus : LocalizableString , error : String? ) {
6577 statusField = LabelField (newStatus)
6678 updateError(error) // Will refresh.
6779 }
@@ -70,7 +82,7 @@ class ConnectPage(
7082 * Try connecting again after an error.
7183 */
7284 private fun retry () {
73- updateStatus(" Connecting to ${url.host} ..." , null )
85+ updateStatus(i18n.pnotr( " Connecting to ${url.host} ..." ) , null )
7486 connect()
7587 }
7688
@@ -92,21 +104,21 @@ class ConnectPage(
92104 httpClient
93105 )
94106 client.authenticate()
95- updateStatus(" Checking Coder binary..." , error = null )
107+ updateStatus(i18n.ptrl( " Checking Coder binary..." ) , error = null )
96108 val cli = ensureCLI(client.url, client.buildVersion, settings) { status ->
97- updateStatus(status, error = null )
109+ updateStatus(i18n.pnotr( status) , error = null )
98110 }
99111 // We only need to log in if we are using token-based auth.
100112 if (client.token != null ) {
101- updateStatus(" Configuring CLI..." , error = null )
113+ updateStatus(i18n.ptrl( " Configuring CLI..." ) , error = null )
102114 cli.login(client.token)
103115 }
104116 onConnect(client, cli)
105117
106118 } catch (ex: Exception ) {
107119 val msg = humanizeConnectionError(url, settings.requireTokenAuth, ex)
108120 notify(" Failed to configure ${url.host} " , ex)
109- updateStatus(" Failed to configure ${url.host} " , msg)
121+ updateStatus(i18n.pnotr( " Failed to configure ${url.host} " ) , msg)
110122 }
111123 }
112124 }
0 commit comments