@@ -152,34 +152,52 @@ fun ProxyServerListPage(
152152 }
153153
154154 fun deleteProxyServer (server : ProxyServerState ) {
155+ if (serviceOperationInProgress) return
156+ val remarks = server.server.getInfo().remarks
157+
158+ fun removeServer (stopResult : ProxyServiceResult .Success ? = null): Boolean {
159+ var deleted = false
160+ updateAppState { state ->
161+ val nextServers = state.proxyServers.filterNot { it.id == server.id }
162+ if (nextServers.size == state.proxyServers.size) {
163+ stopResult?.let { result ->
164+ state.copy(
165+ proxyRunning = result.proxyRunning,
166+ localProxyPort = result.appState?.localProxyPort ? : state.localProxyPort,
167+ )
168+ } ? : state
169+ } else {
170+ deleted = true
171+ val selectedProxyServerId = if (state.selectedProxyServerId == server.id) {
172+ nextServers.firstOrNull()?.id ? : state.selectedProxyServerId
173+ } else {
174+ state.selectedProxyServerId
175+ }
176+ state.copy(
177+ proxyServers = nextServers,
178+ selectedProxyServerId = selectedProxyServerId,
179+ proxyRunning = stopResult?.proxyRunning ? : state.proxyRunning,
180+ localProxyPort = stopResult?.appState?.localProxyPort ? : state.localProxyPort,
181+ )
182+ }
183+ }
184+ return deleted
185+ }
186+
187+ val stateSnapshot = stateStore.state.value
188+ if (stateSnapshot.selectedProxyServerId != server.id || ! stateSnapshot.proxyRunning) {
189+ if (removeServer()) {
190+ services.appScope.launch {
191+ tipNotifier.show(messages.deletedTemplate.formatTemplate(" name" to remarks))
192+ }
193+ }
194+ return
195+ }
196+
155197 runProxyServiceOperation {
156198 when (val stopResult = proxyServiceUseCase.stop(stateStore.state.value.runMode)) {
157199 is ProxyServiceResult .Success -> {
158- val remarks = server.server.getInfo().remarks
159- var deleted = false
160- updateAppState { state ->
161- val nextServers = state.proxyServers.filterNot { it.id == server.id }
162- if (nextServers.size == state.proxyServers.size) {
163- state.copy(
164- proxyRunning = stopResult.proxyRunning,
165- localProxyPort = stopResult.appState?.localProxyPort ? : state.localProxyPort,
166- )
167- } else {
168- deleted = true
169- val selectedProxyServerId = if (state.selectedProxyServerId == server.id) {
170- nextServers.firstOrNull()?.id ? : state.selectedProxyServerId
171- } else {
172- state.selectedProxyServerId
173- }
174- state.copy(
175- proxyServers = nextServers,
176- selectedProxyServerId = selectedProxyServerId,
177- proxyRunning = stopResult.proxyRunning,
178- localProxyPort = stopResult.appState?.localProxyPort ? : state.localProxyPort,
179- )
180- }
181- }
182- if (deleted) {
200+ if (removeServer(stopResult)) {
183201 tipNotifier.show(messages.deletedTemplate.formatTemplate(" name" to remarks))
184202 }
185203 }
0 commit comments