代码优化
authorliangzhenying <867174124@qq.com>
Thu, 25 Dec 2025 09:02:31 +0000 (17:02 +0800)
committerliangzhenying <867174124@qq.com>
Thu, 25 Dec 2025 09:02:31 +0000 (17:02 +0800)
12 files changed:
ShenYin/ShenYin/Api/HttpClient.swift [deleted file]
ShenYin/ShenYin/JoySdk/JoyBleManager.swift
ShenYin/ShenYin/JoySdk/JoyChat.swift
ShenYin/ShenYin/JoySdk/JoySdkManager.swift
ShenYin/ShenYin/Launch/AppLaunch.swift
ShenYin/ShenYin/Launch/AppManager.swift
ShenYin/ShenYin/Ota/OtaManager.swift
ShenYin/ShenYin/UI/Customer/CustomerVC.swift
ShenYin/ShenYin/UI/Home/Chinese2English/Chinese2EnglishVC.swift
ShenYin/ShenYin/UI/Home/Device/HomeDeviceCell.swift
ShenYin/ShenYin/UI/Home/Dialog/DialogTranlateVC.swift
ShenYin/ShenYin/UI/Home/FieldRecording/FieldRecordingVC.swift

diff --git a/ShenYin/ShenYin/Api/HttpClient.swift b/ShenYin/ShenYin/Api/HttpClient.swift
deleted file mode 100644 (file)
index cf2cac3..0000000
+++ /dev/null
@@ -1,140 +0,0 @@
-//
-//  Client.swift
-//  Magpie
-//
-//  Created by incar on 2024/1/9.
-//
-
-//import Foundation
-//import Moya
-//import HandyJSON
-//import DeviceKit
-//// 相关api
-//enum CommomApi
-//{
-//    case sms(phone:String,type:SmsType)
-//    case countryList
-//    case list(pageNO: Int = 1, pageSize: Int = 10)
-//    case register(param:[String:Any])
-//    case login(phone:String,code:String)
-//}
-//
-//// 实现TargetType协议
-//extension CommomApi: TargetType
-//{
-//    // url
-//    var baseURL: URL
-//    {
-////        return URL(string: "https://app-api.raisound.com")!
-////        http://118.145.178.188:32000/uc/country/list
-////        return URL(string: "https://app-api.deepchords.com")!
-//        return URL(string: "http://118.145.178.188:32000")!
-//    }
-//
-//    /// 请求路径
-//    var path: String {
-//        switch self {
-//        case .sms(_,_):
-//            return "/uc/sms"
-//        case .countryList:
-//            return "/uc/country/list"
-//        case .list:
-//            return "list"
-//        case .register( let param):
-//            return "/uc/sign-up?smsCode=\(param["smsCode"]!)"
-//        case .login(_,_):
-//            return "/uc/phone/reg-login"
-//        }
-//    }
-//    
-//    /// 请求方式
-//    var method: Moya.Method {
-//        switch self {
-//        case .sms(_,_):
-//            return .get
-//        case .register(_):
-//            return .post
-//        case .login(_,_):
-//            return .post
-//        default:
-//            return .get
-//        }
-//    }
-//    
-//    /// 解析格式
-//    var sampleData: Data {
-//        return "{}".data(using: String.Encoding.utf8)!
-//    }
-////https://sz.58.com/esf-ajax/activity/ashen_switch/?city_id=4
-////    let idfv = UIDevice.current.identifierForVendor?.uuidString
-//    var task: Task {
-//        // 公共参数
-////        var params: [String: Any] = ["token": "Gz1qYLXeBW8MZuUfDlr9wsAYuVS1cZFMJY9BbaF842L2gRps747o4w=="]
-//        var params: [String: Any] = [:]
-//
-//        // 收集参数
-//        switch self {
-//        case let .sms(phone, type):
-//            params["phone"] = phone
-//            params["type"] = type
-//        case let .list(pageNO, pageSize):
-//            params["pageNO"] = pageNO
-//            params["pageSize"] = pageSize
-//        case let .register(para):
-//            params += para
-//        case let .login(phone,code):
-//            params["phone"] = phone
-//            params["smsCode"] = code
-//            break
-//        default:
-//            break
-//        }
-//        return .requestParameters(parameters: params, encoding: URLEncoding.default)
-//    }
-//    
-//    /// 公共请求头
-//    var headers: [String : String]? {
-//        return [
-//            "devtype": "iOS",
-//            "devid": UIDevice().identifierForVendor?.uuidString ?? "unknow",
-////            "Content - Type": "application/json"
-//        ]
-//    }
-//
-//    
-////https://gz.fang.anjuke.com/xinfang/harmony/adviser/adviser/getMainAdviserBlock?client_source=3&limit=100&target_id=523085&target_type=1&adviser_id=&adviser_type=1&loupan_id=523085&layout_id=&page_source=2
-//}
-//
-//class HttpClient{
-//    static let instance = HttpClient()
-//    let provider = MoyaProvider<CommomApi>()
-//    
-//    func request<T: HandyJSON>(_ target: CommomApi,completionHandler:@escaping ((T, String) -> Void)){
-//        provider.request(target){ (response) in
-//            Logger.debug("url = \(target.baseURL.absoluteString + target.path)")
-//            Logger.debug("param = \(target.task)")
-//            
-//            
-//            switch response {
-//            case .success(let value):
-//                let data = value.data
-////                print("reponse data=>",data.hexEncodedString())
-//                var responseStr = "response转字符串失败"
-//                if let str = String(data: data, encoding: .utf8) {
-//                    responseStr = str
-//                }
-//                Logger.debug("reponse text=>",responseStr)
-//                if let res = T.deserialize(from: responseStr){
-//                    completionHandler(res,responseStr)
-//                    return
-//                }
-//                completionHandler(T(),"解析请求失败")
-//                break
-//            case .failure(let error):
-//                completionHandler(T(),"请求失败".l10n)
-//                Logger.debug("***************\n网络连接失败!\n\(error)\n********************")
-//            }
-//        }
-//    }
-//   
-//}
index d7c3437..c9b1317 100644 (file)
@@ -8,7 +8,11 @@
 import UIKit
 import CoreBluetooth
 import AVFAudio
+
+//#if FEATURE_X
 import DeepChordsSdk
+//#endif
+
 
 class DCPeripheral{
     var device:CBPeripheral!
index 83664a6..2dbac9c 100644 (file)
@@ -28,13 +28,13 @@ class JoyChat:NSObject{
 }
 
 extension JoyChat : JoyGemChatManagerDelegate {
-
     func chatManager(_ manager: JoyGemChatManager, audioData data: Data?, index: UInt, sampleRate: UInt, finish isFinish: Bool) {
         PcmSound.sharedInstance().delegate = self
         if (!isTtsPlaying) {
             isTtsPlaying = true
             Logger.debug("开始播放")
             JoyGemChatManager.shared().playAudioStart()
+            App.shared.preparePlay()
         }
         Logger.debug("收到音频数据", data?.count ?? 0)
         PcmSound.sharedInstance().feed(data)
@@ -47,21 +47,7 @@ extension JoyChat : JoyGemChatManagerDelegate {
         case .startRecord:
             if (isTtsPlaying) {
                 isTtsPlaying = false
-                PcmSound.sharedInstance().cleanup()
-                PcmSound.sharedInstance().setup(16000, withchannels: 1)
-                
-                var options: AVAudioSession.CategoryOptions = [.allowBluetooth]
-                //                if App.shared.isInBackground{
-                //                    options = [.mixWithOthers]
-                //                }
-                
-                
-                do {
-                    try AVAudioSession.sharedInstance().setCategory(.playback, options: [options])
-                    try AVAudioSession.sharedInstance().setActive(true)
-                } catch {
-                    Logger.debug(error)
-                }
+                App.shared.preparePlay()
             }
             break
         case .stopRecord:
index 5f76ab5..edbfc54 100644 (file)
@@ -34,14 +34,6 @@ extension JoySdkManager {
         callRecord = JoyCallRecord()
         chat = JoyChat()
         chat.loadChatCache()
-        
-        let options: AVAudioSession.CategoryOptions = [.allowBluetooth]
-        do {
-            try AVAudioSession.sharedInstance().setCategory(.playback, options: [options])
-            try AVAudioSession.sharedInstance().setActive(true)
-        } catch {
-            Logger.debug(error)
-        }
     }
     
     func setupLog(){
@@ -124,14 +116,7 @@ extension JoySdkManager : JoyGemAIManagerDelegate {
     }
     func joyGemAuthSuccessCallback() {
         Logger.debug("鉴权成功")
-        PcmSound.sharedInstance().setup(16000, withchannels: 1)
-        let options: AVAudioSession.CategoryOptions = [.allowBluetooth]
-        do {
-            try AVAudioSession.sharedInstance().setCategory(.playback, options: [options])
-            try AVAudioSession.sharedInstance().setActive(true)
-        } catch {
-            Logger.debug(error)
-        }
+        App.shared.preparePlay()
         
         DeviceCache.saveLastConnected(mac: JoyBleManager.shared.connectPeripheral!.mac)
         DeviceCache.shared.add(JoyBleManager.shared.connectPeripheral!.device.identifier.uuidString, mac: JoyBleManager.shared.connectPeripheral!.mac)
index 95509b7..3d52d87 100644 (file)
@@ -1,5 +1,4 @@
 //
-//  JoySdkManager.swift
 //  ShenYin
 //
 //  Created by liangshi on 2025/8/28.
index 038c4c7..f86626e 100644 (file)
@@ -19,6 +19,19 @@ class App{
         }
         return getTopViewController(from: vc)
     }
+    func preparePlay(){
+        PcmSound.sharedInstance().stopAudioUnit()
+        PcmSound.sharedInstance().cleanup()
+        PcmSound.sharedInstance().setup(16000, withchannels: 1)
+        
+        let options: AVAudioSession.CategoryOptions = [.allowBluetooth]
+        do {
+            try AVAudioSession.sharedInstance().setCategory(.playback, options: [options])
+            try AVAudioSession.sharedInstance().setActive(true)
+        } catch {
+            Logger.debug(error)
+        }
+    }
     
     func willLogin(){
         CacheManager.shared.user.tokenIsValid {[weak self] success in
index 2b57aa7..b66efae 100644 (file)
@@ -22,7 +22,6 @@ class OtaMananger:NSObject{
         return "AE00"
     }
     static func isOtadata(_ characteristic:CBCharacteristic)->Bool{
-//        guard let <#constant#> = <#expression#> else { return <#return value#> }
         return (characteristic.service?.uuid.uuidString == otaServiceUUId) && (characteristic.uuid.uuidString == "AE02" || characteristic.uuid.uuidString == "AE01")
     }
 }
@@ -33,11 +32,6 @@ extension OtaMananger:JLBleAssistManagerOtaDelegate{
             ProgressHUDHelper.shared.hide()
             YLprogressHUD.showAutoHiden("升级成功".l10n)
             Logger.debug("升级成功")
-//            let mac = JoyBleManager.shared.connectPeripheral?.mac ?? ""
-//            JoyBleManager.shared.cancel()
-//            JoyBleManager.shared.connect(mac: mac) { result in
-//                
-//            }
         }
         else if result == .fail{
             ProgressHUDHelper.shared.hide()
index 025e1ea..1a34d96 100644 (file)
@@ -126,18 +126,5 @@ extension CustomerVC:SendToolBarDelegate{
         
         refreshData()
         toolBar.textField.text = ""
-        
-        
-//        JoyGemAIManager.shared().translate(text, from: from, to: to) {[weak self] result, finish in
-//            guard let self = self else{return}
-//            if !finish{
-//                dataList.last!.response = dataList.last!.response + result
-//                refreshData()
-//            }
-//            
-//        } error: { error in
-//            
-//        }
-        
     }
 }
index a55dad5..4363db3 100644 (file)
@@ -16,10 +16,10 @@ class Chinese2EnglishVC:BaseVC{
     var recordFilePath : String?
     var startIndex = 0//记录开始录音的次数,用于区分返回的数据
 
-    deinit {
-        PcmSound.sharedInstance().stopAudioUnit()
-        PcmSound.sharedInstance().cleanup()
-    }
+//    deinit {
+//        PcmSound.sharedInstance().stopAudioUnit()
+//        PcmSound.sharedInstance().cleanup()
+//    }
     
     override func setupViews() {
         navBar.title = "中英互译".l10n
@@ -42,8 +42,6 @@ class Chinese2EnglishVC:BaseVC{
         tableView.register(Chinese2EnglishCell.self, forCellReuseIdentifier: Chinese2EnglishCell.reuseID)
         
         bg.image = UIImage(named: "translate_bg")
-
-        
     }
     override func setupLayout() {
         toolBar.snp.makeConstraints { make in
@@ -63,15 +61,7 @@ class Chinese2EnglishVC:BaseVC{
     }
     override func setupData() {
         JoyGemSIManager.shared().delegate = self
-        
-        PcmSound.sharedInstance().delegate = self;
-        PcmSound.sharedInstance().setup(16000, withchannels: 1)
-//        do {
-//            try AVAudioSession.sharedInstance().setActive(true)
-//        } catch {
-//            print(error)
-//        }
-        
+
         if TipsCache.needShow(type: .chinese2english) {
             RecordTipsView(type: .chinese2english).show()
         }
@@ -79,17 +69,21 @@ class Chinese2EnglishVC:BaseVC{
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
         navigationController?.fd_fullscreenPopGestureRecognizer?.isEnabled = false
+        
+        PcmSound.sharedInstance().delegate = self;
+        App.shared.preparePlay()
     }
     override func viewDidDisappear(_ animated: Bool) {
         super.viewDidDisappear(animated)
         navigationController?.fd_fullscreenPopGestureRecognizer?.isEnabled = true
+        App.shared.preparePlay()
     }
     override func navleftViewTapped() {
         if toolBar.recordBtn.isSelected {
             CustomAlert.showWith(title: "提示".l10n, messgage: "当前正在录音,确定放弃当前录音吗".l10n, cancelCallback: {}) {
-                JoyGemSIManager.shared().stop()
-                PcmSound.sharedInstance().stopAudioUnit()
-                PcmSound.sharedInstance().cleanup()
+//                JoyGemSIManager.shared().stop()
+//                PcmSound.sharedInstance().stopAudioUnit()
+//                PcmSound.sharedInstance().cleanup()
                 super.navleftViewTapped()
             }
         }
@@ -206,10 +200,7 @@ extension Chinese2EnglishVC : JoyGemSIManagerDelegate {
     func siManager(_ manager: JoyGemSIManager, didUpdateAudioData data: Data, sampleRate: UInt) {
         PcmSound.sharedInstance().feed(data)
     }
-    //    func siManager(_ manager: JoyGemSIManager, didUpdateAudioData data: Data) {
-    //        PcmSound.sharedInstance().feed(data)
-    //    }
-    
+
     func siManager(_ manager: JoyGemSIManager, onError code: JoyGemErrorCode) {
         
     }
@@ -218,6 +209,4 @@ extension Chinese2EnglishVC:PcmSoundDelegate{
     func remaining(_ frames: UInt) {
         
     }
-    
-    
 }
index 20e76c2..aec110b 100644 (file)
@@ -16,7 +16,6 @@ class HomeDeviceCell: BaseCollectionViewCell {
     
     var model:HomeDeviceModel!{
         didSet{
-//            titleLabel.text = model.productName + model.mac.prefix(5)
             titleLabel.text = model.productName
 
             icon.kf.setImage(with: model.image.yc.asUrl)
index fef2eb8..06ce7d9 100644 (file)
@@ -78,7 +78,6 @@ class DialogTranslateVC:BaseVC{
         } catch {
             print(error)
         }
-        PcmSound.sharedInstance().setup(16000, withchannels: 1)
         
         if TipsCache.needShow(type: .dialog) {
             RecordTipsView(type: .dialog).show()
index 7b51575..2c8d63b 100644 (file)
@@ -98,14 +98,6 @@ class FieldRecordingVC:BaseVC{
             textView.text = text
             textView.scrollToBottom()
         }
-        
-        let options: AVAudioSession.CategoryOptions = [.allowBluetooth]
-        do {
-            try AVAudioSession.sharedInstance().setCategory(.playback, options: [options])
-            try AVAudioSession.sharedInstance().setActive(true)
-        } catch {
-            Logger.debug(error)
-        }
         if autoStart{
             startRecord()
         }