feat: 重写 SDK 支持 iOS 12.2,移除 async/await 和 Combine
- 所有公开 API 改为 completion handler (Result<T, Error>) - 状态通知改为 DuooomiBleSDKDelegate 协议 - 移除 Sendable、@MainActor、AsyncStream、CheckedContinuation - 网络请求改为 URLSession.dataTask 回调 - BLE 通信改为 delegate + 闭包模式 - deployment target 降至 iOS 12.2 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Foundation
|
||||
|
||||
/// 固件信息
|
||||
public struct FirmwareInfo: Codable, Equatable, Sendable {
|
||||
public struct FirmwareInfo: Codable, Equatable {
|
||||
public let version: String
|
||||
public let fileUrl: String
|
||||
public let description: String?
|
||||
@@ -25,20 +25,30 @@ final class FirmwareService {
|
||||
self.config = config
|
||||
}
|
||||
|
||||
func fetchLatest(identifier: String? = nil, status: String? = nil) async throws -> FirmwareInfo? {
|
||||
func fetchLatest(
|
||||
identifier: String? = nil,
|
||||
status: String? = nil,
|
||||
completion: @escaping (Result<FirmwareInfo?, Error>) -> Void
|
||||
) {
|
||||
let id = (identifier ?? config.firmwareIdentifier).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let st = status ?? config.firmwareStatus
|
||||
guard !id.isEmpty else {
|
||||
throw DuooomiBleError.transferFailed("Invalid firmware identifier")
|
||||
completion(.failure(DuooomiBleError.transferFailed("Invalid firmware identifier")))
|
||||
return
|
||||
}
|
||||
|
||||
var comps = URLComponents(url: config.apiHost.appendingPathComponent(latestPath), resolvingAgainstBaseURL: false)!
|
||||
let urlString = "\(config.apiHost)/\(latestPath)"
|
||||
guard var comps = URLComponents(string: urlString) else {
|
||||
completion(.failure(DuooomiBleError.transferFailed("Invalid firmware URL")))
|
||||
return
|
||||
}
|
||||
comps.queryItems = [
|
||||
URLQueryItem(name: "identifier", value: id),
|
||||
URLQueryItem(name: "status", value: st)
|
||||
]
|
||||
guard let url = comps.url else {
|
||||
throw DuooomiBleError.transferFailed("Invalid firmware URL")
|
||||
completion(.failure(DuooomiBleError.transferFailed("Invalid firmware URL")))
|
||||
return
|
||||
}
|
||||
|
||||
var req = URLRequest(url: url)
|
||||
@@ -46,15 +56,30 @@ final class FirmwareService {
|
||||
req.setValue(config.apiKey, forHTTPHeaderField: "x-api-key")
|
||||
req.setValue("application/json", forHTTPHeaderField: "accept")
|
||||
|
||||
let (data, resp) = try await URLSession.shared.data(for: req)
|
||||
guard let http = resp as? HTTPURLResponse else {
|
||||
throw DuooomiBleError.transferFailed("Invalid response")
|
||||
}
|
||||
guard (200...299).contains(http.statusCode) else {
|
||||
throw DuooomiBleError.transferFailed("HTTP \(http.statusCode)")
|
||||
}
|
||||
URLSession.shared.dataTask(with: req) { data, resp, error in
|
||||
if let error = error {
|
||||
completion(.failure(error))
|
||||
return
|
||||
}
|
||||
guard let http = resp as? HTTPURLResponse else {
|
||||
completion(.failure(DuooomiBleError.transferFailed("Invalid response")))
|
||||
return
|
||||
}
|
||||
guard (200...299).contains(http.statusCode) else {
|
||||
completion(.failure(DuooomiBleError.transferFailed("HTTP \(http.statusCode)")))
|
||||
return
|
||||
}
|
||||
guard let data = data else {
|
||||
completion(.failure(DuooomiBleError.transferFailed("Empty response")))
|
||||
return
|
||||
}
|
||||
|
||||
let decoded = try JSONDecoder().decode(FirmwareResponse.self, from: data)
|
||||
return decoded.data
|
||||
do {
|
||||
let decoded = try JSONDecoder().decode(FirmwareResponse.self, from: data)
|
||||
completion(.success(decoded.data))
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}.resume()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user