Forráskód Böngészése

remote(client): refactor common connect logic

osy 4 hónapja
szülő
commit
83f63e3c49
1 módosított fájl, 25 hozzáadás és 40 törlés
  1. 25 40
      Platform/iOS/UTMRemoteConnectView.swift

+ 25 - 40
Platform/iOS/UTMRemoteConnectView.swift

@@ -268,54 +268,39 @@ private struct ServerConnectView: View {
                 remoteClientState.showErrorAlert(NSLocalizedString("Timed out trying to connect.", comment: "UTMRemoteConnectView"))
             }
             if #available(iOS 15, *) {
-                do {
-                    try await remoteClient.connect(server)
-                } catch {
-                    if case UTMRemoteClient.ConnectionError.passwordRequired = error {
-                        withAnimation {
-                            isPasswordRequired = true
-                            isTrustButton = false
-                        }
-                    } else if case UTMRemoteClient.ConnectionError.fingerprintUntrusted(let fingerprint) = error, server.fingerprint.isEmpty {
-                        withAnimation {
-                            server.fingerprint = fingerprint
-                            isTrustButton = true
-                        }
-                        remoteClientState.showErrorAlert(error.localizedDescription)
-                    } else if error is CancellationError {
-                        // ignore it
-                    } else {
-                        remoteClientState.showErrorAlert(error.localizedDescription)
-                    }
-                }
+                await _connect()
             } else {
                 Task(priority: .userInteractive) {
-                    do {
-                        try await remoteClient.connect(server)
-                    } catch {
-                        if case UTMRemoteClient.ConnectionError.passwordRequired = error {
-                            withAnimation {
-                                isPasswordRequired = true
-                                isTrustButton = false
-                            }
-                        } else if case UTMRemoteClient.ConnectionError.fingerprintUntrusted(let fingerprint) = error, server.fingerprint.isEmpty {
-                            withAnimation {
-                                server.fingerprint = fingerprint
-                                isTrustButton = true
-                            }
-                            remoteClientState.showErrorAlert(error.localizedDescription)
-                        } else if error is CancellationError {
-                            // ignore it
-                        } else {
-                            remoteClientState.showErrorAlert(error.localizedDescription)
-                        }
-                    }
+                    await _connect()
                 }
             }
             timeoutTask.cancel()
             connectionTask = nil
         }
     }
+
+    private func _connect() async {
+        do {
+            try await remoteClient.connect(server)
+        } catch {
+            if case UTMRemoteClient.ConnectionError.passwordRequired = error {
+                withAnimation {
+                    isPasswordRequired = true
+                    isTrustButton = false
+                }
+            } else if case UTMRemoteClient.ConnectionError.fingerprintUntrusted(let fingerprint) = error, server.fingerprint.isEmpty {
+                withAnimation {
+                    server.fingerprint = fingerprint
+                    isTrustButton = true
+                }
+                remoteClientState.showErrorAlert(error.localizedDescription)
+            } else if error is CancellationError {
+                // ignore it
+            } else {
+                remoteClientState.showErrorAlert(error.localizedDescription)
+            }
+        }
+    }
 }
 
 @available(iOS 15, *)