Bladeren bron

新增 删除按钮 以及alert view 确认按钮

yichael 3 weken geleden
bovenliggende
commit
9174f04cba

+ 27 - 5
nodejs/adb-connect.js

@@ -13,10 +13,32 @@ if (!deviceIp) {
   process.exit(1)
 }
 
+// Connect to device
 const connectCommand = `"${adbPath}" connect ${deviceIp}:${devicePort}`
-const output = execSync(connectCommand, { encoding: 'utf-8' })
-const result = output.trim()
-const isConnected = result.includes('connected') || result.includes('already connected')
+const connectOutput = execSync(connectCommand, { encoding: 'utf-8' })
+const connectResult = connectOutput.trim()
+const connectSuccess = connectResult.includes('connected') || connectResult.includes('already connected')
 
-process.stdout.write(isConnected ? 'true\n' : 'false\n')
-process.exit(isConnected ? 0 : 1)
+if (!connectSuccess) {
+  process.stdout.write('false\n')
+  process.exit(1)
+}
+
+// Verify device is actually available in device list
+try {
+  const devicesCommand = `"${adbPath}" devices`
+  const devicesOutput = execSync(devicesCommand, { encoding: 'utf-8' })
+  const deviceAddress = `${deviceIp}:${devicePort}`
+  const isDeviceAvailable = devicesOutput.includes(deviceAddress) && devicesOutput.includes('device')
+  
+  if (isDeviceAvailable) {
+    process.stdout.write('success\n')
+    process.exit(0)
+  } else {
+    process.stdout.write('false\n')
+    process.exit(1)
+  }
+} catch (error) {
+  process.stdout.write('false\n')
+  process.exit(1)
+}

+ 16 - 54
src/page/device/device.js

@@ -55,81 +55,43 @@ class DeviceClass {
     async handleAdd() {
   
         const ip = document.querySelector('.ip-input input').value
-        hintView.setContent(ip)
-        hintView.show()
         const result = await window.electronAPI.runNodejsScript('adb-connect', ip, '5555')
     
         if(result.stdout == 'success') {
-            const newDevice = await this.addDevice(ip)
-            if (newDevice && this.setDeviceList) { 
-                this.setDeviceList(prev => [...prev, newDevice])
+            hintView.setContent('设备添加成功')
+            hintView.show()
 
-                hintView.setContent('设备添加成功')
-                hintView.show()
-            }
-            return
+            await this.addDevice(ip);
+          
+            return;
         }
-
         alertView.show()
         alertView.setContent('无法检测到连接设备')
     }
 
-    async addDevice(ip) {
-        const readResult = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
-        
-        let jsonData
-        if (readResult.stdout === '') {
-            await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
-            jsonData = {devices: []}
-        } else {
-            const parsed = JSON.parse(readResult.stdout)
-            jsonData = parsed.data || {devices: []}
-        }
-        
+    async addDevice(ip) {   
         // 数组中是字符串,直接 push IP 字符串
-        jsonData.devices.push(ip)
-        await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: jsonData.devices}))
+        this.deviceArr.push(ip)
+        await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: this.deviceArr}))
 
-        // 更新 this.deviceArr
-        this.deviceArr = jsonData.devices
-        this.setDeviceList(jsonData.devices);
-        return ip
+        this.setDeviceList(this.deviceArr);
     }
 
     async removeDevice(ip) {
         
         comfirmView.show()
         comfirmView.setContent('确定要删除设备吗?')
+
         comfirmView.onConfirm = async () => {
             comfirmView.hide()
-
-            const readResult = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
-        
-            if (readResult.stdout === '') {
-                return
-            }
-            
-            const parsed = JSON.parse(readResult.stdout)
-            const jsonData = parsed.data || {devices: []}
-            
-            // 过滤掉要删除的设备(数组中是字符串,直接比较)
-            const filteredDevices = jsonData.devices.filter(deviceIp => deviceIp !== ip)
             
+            // 从数组中移除指定 IP(deviceIp 是数组中的每个字符串元素)
+            this.deviceArr = this.deviceArr.filter(deviceIp => deviceIp !== ip)
+
             // 更新 JSON 文件
-            await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: filteredDevices}))
-            
-            // 更新 this.deviceArr
-            this.deviceArr = filteredDevices
-            
-            // 更新 deviceArrRef
-            if (this.deviceArrRef) {
-                this.deviceArrRef.current = filteredDevices
-            }
-            
-            // 更新 React state
-            if (this.setDeviceList) {
-                this.setDeviceList(filteredDevices)
-            }
+            await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: this.deviceArr}))
+          
+            this.setDeviceList(this.deviceArr)
         }
         comfirmView.onCancel = () => {
             comfirmView.hide()

+ 3 - 0
src/page/device/device.jsx

@@ -92,8 +92,11 @@ function Device({ show }) {
           comfirmView._onCancel()
         }
       }} onConfirm={() => {
+        console.log('ComfirmView onConfirm clicked, _onConfirm:', comfirmView._onConfirm)
         if (comfirmView._onConfirm) {
           comfirmView._onConfirm()
+        } else {
+          console.warn('comfirmView._onConfirm is null')
         }
       }} title={comfirmView._title} content={comfirmView._content} />}
     </>

+ 8 - 2
src/page/public/alert-view/alert-view.jsx

@@ -1,12 +1,18 @@
 import React from 'react'
 import './alert-view.scss'
-import { handleConfirm, defaultTitle, defaultContent } from './alert-view.js'
+import { defaultTitle, defaultContent } from './alert-view.js'
 
 function AlertView({ show, onClose, title = defaultTitle, content = defaultContent }) {
   if (!show) {
     return null
   }
 
+  const handleConfirmClick = () => {
+    if (onClose) {
+      onClose()
+    }
+  }
+
   return (
     <div className="alert-container">
       <div className="alert-bg">
@@ -17,7 +23,7 @@ function AlertView({ show, onClose, title = defaultTitle, content = defaultConte
           <label>{content}</label>
         </div>
         <div className="footer">
-          <div className="confirm-button" onClick={() => handleConfirm(onClose)}>确定</div>
+          <div className="confirm-button" onClick={handleConfirmClick}>确定</div>
         </div>
       </div>
     </div>

+ 10 - 0
src/page/public/comfirm-view/comfirm-view.js

@@ -56,6 +56,16 @@ const comfirmViewAPI = {
 
   setShowCallback(callback) {
     this._setShowCallback = callback
+  },
+
+  set onConfirm(callback) {
+    console.log('comfirmView.onConfirm setter called, callback:', callback)
+    this._onConfirm = callback
+  },
+
+  set onCancel(callback) {
+    console.log('comfirmView.onCancel setter called, callback:', callback)
+    this._onCancel = callback
   }
 }
 

+ 22 - 4
src/page/public/comfirm-view/comfirm-view.jsx

@@ -1,12 +1,30 @@
 import React from 'react'
 import './comfirm-view.scss'
-import { handleConfirm, handleCancel, defaultTitle, defaultContent } from './comfirm-view.js'
+import { defaultTitle, defaultContent } from './comfirm-view.js'
 
-function comfirmView({ show, onClose, title = defaultTitle, content = defaultContent }) {
+function comfirmView({ show, onClose, onConfirm, onCancel, title = defaultTitle, content = defaultContent }) {
   if (!show) {
     return null
   }
 
+  const handleConfirmClick = () => {
+    if (onConfirm) {
+      onConfirm()
+    }
+    if (onClose) {
+      onClose()
+    }
+  }
+
+  const handleCancelClick = () => {
+    if (onCancel) {
+      onCancel()
+    }
+    if (onClose) {
+      onClose()
+    }
+  }
+
   return (
     <div className="comfirm-container">
       <div className="alert-bg">
@@ -17,8 +35,8 @@ function comfirmView({ show, onClose, title = defaultTitle, content = defaultCon
           <label>{content}</label>
         </div>
         <div className="footer">
-          <div className="cancel-button" onClick={() => handleCancel(onClose)}>取消</div>
-          <div className="confirm-button" onClick={() => handleConfirm(onClose)}>确定</div>
+          <div className="cancel-button" onClick={handleCancelClick}>取消</div>
+          <div className="confirm-button" onClick={handleConfirmClick}>确定</div>
         </div>
       </div>
     </div>