@@ -6,11 +6,16 @@ import com.coder.toolbox.plugin.PluginManager
6
6
import com.coder.toolbox.sdk.CoderRestClient
7
7
import com.coder.toolbox.settings.CoderSettings
8
8
import 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
9
12
import com.jetbrains.toolbox.api.ui.actions.RunnableActionDescription
10
13
import com.jetbrains.toolbox.api.ui.components.LabelField
11
14
import com.jetbrains.toolbox.api.ui.components.UiField
12
15
import kotlinx.coroutines.CoroutineScope
13
16
import kotlinx.coroutines.Job
17
+ import kotlinx.coroutines.flow.MutableStateFlow
18
+ import kotlinx.coroutines.flow.StateFlow
14
19
import kotlinx.coroutines.launch
15
20
import okhttp3.OkHttpClient
16
21
import java.net.URL
@@ -19,22 +24,26 @@ import java.net.URL
19
24
* A page that connects a REST client and cli to Coder.
20
25
*/
21
26
class ConnectPage (
27
+ private val serviceLocator : ServiceLocator ,
22
28
private val url : URL ,
23
29
private val token : String? ,
24
30
private val settings : CoderSettings ,
25
31
private val httpClient : OkHttpClient ,
26
- private val coroutineScope : CoroutineScope ,
32
+ title : LocalizableString ,
27
33
private val onCancel : () -> Unit ,
28
34
private val onConnect : (
29
35
client: CoderRestClient ,
30
36
cli: CoderCLIManager ,
31
37
) -> 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
+
33
42
private var signInJob: Job ? = null
34
43
35
- private var statusField = LabelField (" Connecting to ${url.host} ..." )
44
+ private var statusField = LabelField (i18n.pnotr( " Connecting to ${url.host} ..." ) )
36
45
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} ." )
38
47
39
48
init {
40
49
connect()
@@ -45,23 +54,26 @@ class ConnectPage(
45
54
*
46
55
* TODO@JB: This looks kinda sparse. A centered spinner would be welcome.
47
56
*/
48
- override val fields: MutableList <UiField > = listOfNotNull(
57
+ override val fields: StateFlow <List <UiField >> = MutableStateFlow (
58
+ listOfNotNull(
49
59
statusField,
50
- errorField,
51
- ).toMutableList()
60
+ errorField
61
+ )
62
+ )
52
63
53
64
/* *
54
65
* Show a retry button on error.
55
66
*/
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
+ ))
60
72
61
73
/* *
62
74
* Update the status and error fields then refresh.
63
75
*/
64
- private fun updateStatus (newStatus : String , error : String? ) {
76
+ private fun updateStatus (newStatus : LocalizableString , error : String? ) {
65
77
statusField = LabelField (newStatus)
66
78
updateError(error) // Will refresh.
67
79
}
@@ -70,7 +82,7 @@ class ConnectPage(
70
82
* Try connecting again after an error.
71
83
*/
72
84
private fun retry () {
73
- updateStatus(" Connecting to ${url.host} ..." , null )
85
+ updateStatus(i18n.pnotr( " Connecting to ${url.host} ..." ) , null )
74
86
connect()
75
87
}
76
88
@@ -92,21 +104,21 @@ class ConnectPage(
92
104
httpClient
93
105
)
94
106
client.authenticate()
95
- updateStatus(" Checking Coder binary..." , error = null )
107
+ updateStatus(i18n.ptrl( " Checking Coder binary..." ) , error = null )
96
108
val cli = ensureCLI(client.url, client.buildVersion, settings) { status ->
97
- updateStatus(status, error = null )
109
+ updateStatus(i18n.pnotr( status) , error = null )
98
110
}
99
111
// We only need to log in if we are using token-based auth.
100
112
if (client.token != null ) {
101
- updateStatus(" Configuring CLI..." , error = null )
113
+ updateStatus(i18n.ptrl( " Configuring CLI..." ) , error = null )
102
114
cli.login(client.token)
103
115
}
104
116
onConnect(client, cli)
105
117
106
118
} catch (ex: Exception ) {
107
119
val msg = humanizeConnectionError(url, settings.requireTokenAuth, ex)
108
120
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)
110
122
}
111
123
}
112
124
}
0 commit comments