From 5b18f180b4f4575264c897cc2025622c393b5317 Mon Sep 17 00:00:00 2001 From: Naman Dwivedi Date: Fri, 12 Mar 2021 11:29:24 +0530 Subject: [PATCH] get device list on background thread --- adbconnect/AdbHelper.swift | 16 ++++++++++------ adbconnect/ContentView.swift | 22 ++++++++++------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/adbconnect/AdbHelper.swift b/adbconnect/AdbHelper.swift index 0afdb30..cf3605b 100644 --- a/adbconnect/AdbHelper.swift +++ b/adbconnect/AdbHelper.swift @@ -57,15 +57,19 @@ class AdbHelper { } func makeTCPConnection(deviceId: String) { - let deviceIp = getDeviceIp(deviceId: deviceId); - let tcpCommand = "-s " + deviceId + " tcpip 5555" - _ = runAdbCommand(tcpCommand) - let connectCommand = "-s " + deviceId + " connect " + deviceIp + ":5555" - _ = runAdbCommand(connectCommand) + DispatchQueue.global(qos: .background).async { + let deviceIp = self.getDeviceIp(deviceId: deviceId); + let tcpCommand = "-s " + deviceId + " tcpip 5555" + _ = self.runAdbCommand(tcpCommand) + let connectCommand = "-s " + deviceId + " connect " + deviceIp + ":5555" + _ = self.runAdbCommand(connectCommand) + } } func disconnectTCPConnection(deviceId: String) { - _ = runAdbCommand("-s " + deviceId + " disconnect") + DispatchQueue.global(qos: .background).async { + _ = self.runAdbCommand("-s " + deviceId + " disconnect") + } } func getDeviceIp(deviceId: String) -> String { diff --git a/adbconnect/ContentView.swift b/adbconnect/ContentView.swift index 1783e00..f3c0c3a 100644 --- a/adbconnect/ContentView.swift +++ b/adbconnect/ContentView.swift @@ -15,11 +15,10 @@ struct ContentView: View { @State private var statusMessage: String = "" - init() { - _devices = State(initialValue: adb.getDevices()) - } - var body: some View { + DispatchQueue.global(qos: .background).async { + devices = adb.getDevices() + } return VStack { ScrollView(.vertical) { if (devices.isEmpty) { @@ -77,12 +76,11 @@ struct DeviceActionsView: View { }.contentShape(Rectangle()) .onTapGesture { if(isTcpConnected()) { - adb.disconnectTCPConnection(deviceId: device.id) statusMessaage = "Disconnected remoted connection" - + adb.disconnectTCPConnection(deviceId: device.id) } else { - adb.makeTCPConnection(deviceId: device.id) statusMessaage = "Connected to adb remotely" + adb.makeTCPConnection(deviceId: device.id) } // refresh device list DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { @@ -96,8 +94,8 @@ struct DeviceActionsView: View { Text("Take screenshot") }.contentShape(Rectangle()) .onTapGesture { + statusMessaage = "Screenshot will be saved in Desktop" adb.takeScreenshot(deviceId: device.id) - statusMessaage = "Screenshot saved in Desktop" } // record screen @@ -107,13 +105,13 @@ struct DeviceActionsView: View { }.contentShape(Rectangle()) .onTapGesture { if (isRecordingScreen) { + statusMessaage = "Recording will be saved in Desktop" adb.stopScreenRecording(deviceId: device.id) isRecordingScreen = false - statusMessaage = "Recording saved in Desktop" } else { + statusMessaage = "Started recording screen.." adb.recordScreen(deviceId: device.id) isRecordingScreen = true - statusMessaage = "Started recording screen.." } } @@ -132,8 +130,8 @@ struct DeviceActionsView: View { Image("DeeplinkIcon").resizable().frame(width: 18.0, height: 18.0) TextField("deeplink", text: $deeplink).padding(.leading, 5) Button(action: { - adb.openDeeplink(deviceId: device.id, deeplink: deeplink) statusMessaage = "Opening deeplink.." + adb.openDeeplink(deviceId: device.id, deeplink: deeplink) }, label: { Text("Open") }) @@ -145,8 +143,8 @@ struct DeviceActionsView: View { Text("Capture logcat") }.contentShape(Rectangle()).padding(.leading, 20) .onTapGesture { - adb.captureBugReport(deviceId: device.id) statusMessaage = "Logcat saved in Desktop" + adb.captureBugReport(deviceId: device.id) } }