@@ -66,6 +66,7 @@ internal fun ProxyServerListTopBar(
6666 clipboard : Clipboard ,
6767 tipNotifier : AndroidToastTipNotifier ,
6868 scope : CoroutineScope ,
69+ backgroundScope : CoroutineScope ,
6970 messages : ProxyServerListMessages ,
7071 resultKey : String ,
7172 serviceOperationInProgress : Boolean ,
@@ -92,6 +93,7 @@ internal fun ProxyServerListTopBar(
9293 clipboard = clipboard,
9394 tipNotifier = tipNotifier,
9495 scope = scope,
96+ backgroundScope = backgroundScope,
9597 messages = messages,
9698 resultKey = resultKey,
9799 )
@@ -109,6 +111,7 @@ internal fun ProxyServerListTopBar(
109111 clipboard = clipboard,
110112 tipNotifier = tipNotifier,
111113 scope = scope,
114+ backgroundScope = backgroundScope,
112115 messages = messages,
113116 serviceOperationInProgress = serviceOperationInProgress,
114117 runProxyServiceOperation = runProxyServiceOperation,
@@ -151,6 +154,7 @@ private fun handleProxyServerListAddAction(
151154 clipboard : Clipboard ,
152155 tipNotifier : AndroidToastTipNotifier ,
153156 scope : CoroutineScope ,
157+ backgroundScope : CoroutineScope ,
154158 messages : ProxyServerListMessages ,
155159 resultKey : String ,
156160) {
@@ -160,24 +164,15 @@ private fun handleProxyServerListAddAction(
160164 runCatching { qrScanner() }
161165 .onSuccess { scanText ->
162166 if (scanText.isNullOrBlank()) return @onSuccess
163- if (
164- installSubscriptionFromText(
165- text = scanText,
166- stateStore = stateStore,
167- subscriptionFetcher = subscriptionFetcher,
168- tipNotifier = tipNotifier,
169- messages = messages,
170- )
171- ) {
172- return @onSuccess
173- }
174- importProxyServers(
167+ importProxyServersInBackground(
175168 text = scanText,
176169 source = ProxyServerImportSource .QrCode ,
177170 groupState = groupState,
171+ stateStore = stateStore,
178172 subscriptionFetcher = subscriptionFetcher,
179173 updateAppState = updateAppState,
180174 tipNotifier = tipNotifier,
175+ backgroundScope = backgroundScope,
181176 messages = messages,
182177 )
183178 }
@@ -188,55 +183,39 @@ private fun handleProxyServerListAddAction(
188183 ProxyServerListAddAction .Clipboard -> {
189184 scope.launch {
190185 val text = clipboard.getPlainText().orEmpty()
191- if (
192- installSubscriptionFromText(
193- text = text,
194- stateStore = stateStore,
195- subscriptionFetcher = subscriptionFetcher,
196- tipNotifier = tipNotifier,
197- messages = messages,
198- )
199- ) {
200- return @launch
201- }
202- importProxyServers(
186+ importProxyServersInBackground(
203187 text = text,
204188 source = ProxyServerImportSource .Clipboard ,
205189 groupState = groupState,
190+ stateStore = stateStore,
206191 subscriptionFetcher = subscriptionFetcher,
207192 updateAppState = updateAppState,
208193 tipNotifier = tipNotifier,
194+ backgroundScope = backgroundScope,
209195 messages = messages,
210196 )
211197 }
212198 }
213199
214200 ProxyServerListAddAction .File -> {
215201 scope.launch {
216- runCatching {
217- proxyServerImportFileUseCase.readText()?.let { text ->
218- if (
219- installSubscriptionFromText(
220- text = text,
202+ runCatching { proxyServerImportFileUseCase.readText() }
203+ .onSuccess { text ->
204+ text?.let {
205+ importProxyServersInBackground(
206+ text = it,
207+ source = ProxyServerImportSource .File ,
208+ groupState = groupState,
221209 stateStore = stateStore,
222210 subscriptionFetcher = subscriptionFetcher,
211+ updateAppState = updateAppState,
223212 tipNotifier = tipNotifier,
213+ backgroundScope = backgroundScope,
224214 messages = messages,
225215 )
226- ) {
227- return @let
228216 }
229- importProxyServers(
230- text = text,
231- source = ProxyServerImportSource .File ,
232- groupState = groupState,
233- subscriptionFetcher = subscriptionFetcher,
234- updateAppState = updateAppState,
235- tipNotifier = tipNotifier,
236- messages = messages,
237- )
238217 }
239- } .onFailure { error -> tipNotifier.showError(error) }
218+ .onFailure { error -> tipNotifier.showError(error) }
240219 }
241220 }
242221
@@ -260,6 +239,43 @@ private fun handleProxyServerListAddAction(
260239 }
261240}
262241
242+ private fun importProxyServersInBackground (
243+ text : String ,
244+ source : ProxyServerImportSource ,
245+ groupState : ProxyServerListGroups ,
246+ stateStore : AndroidAppStateStore ,
247+ subscriptionFetcher : AndroidSubscriptionFetcher ,
248+ updateAppState : ((AppState ) -> AppState ) -> Unit ,
249+ tipNotifier : AndroidToastTipNotifier ,
250+ backgroundScope : CoroutineScope ,
251+ messages : ProxyServerListMessages ,
252+ ) {
253+ backgroundScope.launch {
254+ runCatching {
255+ if (
256+ installSubscriptionFromText(
257+ text = text,
258+ stateStore = stateStore,
259+ subscriptionFetcher = subscriptionFetcher,
260+ tipNotifier = tipNotifier,
261+ messages = messages,
262+ )
263+ ) {
264+ return @runCatching
265+ }
266+ importProxyServers(
267+ text = text,
268+ source = source,
269+ groupState = groupState,
270+ subscriptionFetcher = subscriptionFetcher,
271+ updateAppState = updateAppState,
272+ tipNotifier = tipNotifier,
273+ messages = messages,
274+ )
275+ }.onFailure { error -> tipNotifier.showError(error) }
276+ }
277+ }
278+
263279private suspend fun installSubscriptionFromText (
264280 text : String ,
265281 stateStore : AndroidAppStateStore ,
@@ -335,6 +351,7 @@ private fun handleProxyServerListToolAction(
335351 clipboard : Clipboard ,
336352 tipNotifier : AndroidToastTipNotifier ,
337353 scope : CoroutineScope ,
354+ backgroundScope : CoroutineScope ,
338355 messages : ProxyServerListMessages ,
339356 serviceOperationInProgress : Boolean ,
340357 runProxyServiceOperation : (suspend () -> Unit ) -> Unit ,
@@ -389,7 +406,7 @@ private fun handleProxyServerListToolAction(
389406 updateAppState = updateAppState,
390407 subscriptionFetcher = subscriptionFetcher,
391408 tipNotifier = tipNotifier,
392- scope = scope ,
409+ backgroundScope = backgroundScope ,
393410 messages = messages,
394411 )
395412 }
@@ -483,11 +500,11 @@ private fun updateSubscriptionGroups(
483500 updateAppState : ((AppState ) -> AppState ) -> Unit ,
484501 subscriptionFetcher : AndroidSubscriptionFetcher ,
485502 tipNotifier : AndroidToastTipNotifier ,
486- scope : CoroutineScope ,
503+ backgroundScope : CoroutineScope ,
487504 messages : ProxyServerListMessages ,
488505) {
489506 val subscriptionGroups = proxyListState.subscriptionGroups.updatableSubscriptionGroups()
490- scope .launch {
507+ backgroundScope .launch {
491508 if (subscriptionGroups.isEmpty()) {
492509 tipNotifier.show(messages.noSubscriptionUpdates)
493510 return @launch
0 commit comments