Skip to content

Commit

Permalink
Fix a linting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
winsmith committed Sep 16, 2024
1 parent abfd1f1 commit 5d9ab8e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 53 deletions.
50 changes: 25 additions & 25 deletions Sources/TelemetryClient/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
#endif

/// Note: only use this when posting to the deprecated V1 ingest API
internal struct SignalPostBody: Codable, Equatable {
struct SignalPostBody: Codable, Equatable {
/// When was this signal generated
let receivedAt: Date

Expand Down Expand Up @@ -212,7 +212,7 @@ extension DefaultSignalPayload {
var modelIdentifier: String?

if let modelData = IORegistryEntryCreateCFProperty(service, "model" as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? Data {
if let modelIdentifierCString = String(decoding: modelData, as: UTF8.self).cString(using: .utf8) {
if let modelIdentifierCString = String(data: modelData, encoding: .utf8)?.cString(using: .utf8) {
modelIdentifier = String(cString: modelIdentifierCString)
}
}
Expand Down Expand Up @@ -342,50 +342,50 @@ extension DefaultSignalPayload {
@MainActor
static var screenResolutionWidth: String {
#if os(iOS) || os(tvOS)
return "\(UIScreen.main.bounds.width)"
return "\(UIScreen.main.bounds.width)"
#elseif os(watchOS)
return "\(WKInterfaceDevice.current().screenBounds.width)"
return "\(WKInterfaceDevice.current().screenBounds.width)"
#elseif os(macOS)
if let screen = NSScreen.main {
return "\(screen.frame.width)"
}
return "Unknown"
if let screen = NSScreen.main {
return "\(screen.frame.width)"
}
return "Unknown"
#else
return "N/A"
return "N/A"
#endif
}

/// The current devices screen resolution height in points.
@MainActor
static var screenResolutionHeight: String {
#if os(iOS) || os(tvOS)
return "\(UIScreen.main.bounds.height)"
return "\(UIScreen.main.bounds.height)"
#elseif os(watchOS)
return "\(WKInterfaceDevice.current().screenBounds.height)"
return "\(WKInterfaceDevice.current().screenBounds.height)"
#elseif os(macOS)
if let screen = NSScreen.main {
return "\(screen.frame.height)"
}
return "Unknown"
if let screen = NSScreen.main {
return "\(screen.frame.height)"
}
return "Unknown"
#else
return "N/A"
return "N/A"
#endif
}

/// The current devices screen orientation. Returns `Fixed` for devices that don't support an orientation change.
@MainActor
static var orientation: String {
#if os(iOS)
switch UIDevice.current.orientation {
case .portrait, .portraitUpsideDown:
return "Portrait"
case .landscapeLeft, .landscapeRight:
return "Landscape"
default:
return "Unknown"
}
switch UIDevice.current.orientation {
case .portrait, .portraitUpsideDown:
return "Portrait"
case .landscapeLeft, .landscapeRight:
return "Landscape"
default:
return "Unknown"
}
#else
return "Fixed"
return "Fixed"
#endif
}

Expand Down
59 changes: 31 additions & 28 deletions Sources/TelemetryClient/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import WatchKit
import TVUIKit
#endif

internal protocol SignalManageable {
protocol SignalManageable {
func processSignal(_ signalName: String, parameters: [String: String], floatValue: Double?, customUserID: String?, configuration: TelemetryManagerConfiguration)
func attemptToSendNextBatchOfCachedSignals()
}

internal final class SignalManager: SignalManageable, @unchecked Sendable {
internal static let minimumSecondsToPassBetweenRequests: Double = 10
final class SignalManager: SignalManageable, @unchecked Sendable {
static let minimumSecondsToPassBetweenRequests: Double = 10

private var signalCache: SignalCache<SignalPostBody>
let configuration: TelemetryManagerConfiguration
Expand Down Expand Up @@ -82,7 +82,7 @@ internal final class SignalManager: SignalManageable, @unchecked Sendable {
DispatchQueue.global(qos: .utility).async {
let enrichedMetadata: [String: String] = configuration.metadataEnrichers
.map { $0.enrich(signalType: signalName, for: customUserID, floatValue: floatValue) }
.reduce([String: String](), { $0.applying($1) })
.reduce([String: String]()) { $0.applying($1) }

let payload = defaultParameters
.applying(enrichedMetadata)
Expand Down Expand Up @@ -110,7 +110,7 @@ internal final class SignalManager: SignalManageable, @unchecked Sendable {
/// If signals fail to send, we put them back into the cache to try again later.
@objc
@Sendable
internal func attemptToSendNextBatchOfCachedSignals() {
func attemptToSendNextBatchOfCachedSignals() {
configuration.logHandler?.log(.debug, message: "Current signal cache count: \(signalCache.count())")

let queuedSignals: [SignalPostBody] = signalCache.pop()
Expand All @@ -136,8 +136,8 @@ internal final class SignalManager: SignalManageable, @unchecked Sendable {
return
}

if let data = data {
configuration.logHandler?.log(.debug, message: String(decoding: data, as: UTF8.self))
if let data = data, let messageString = String(data: data, encoding: .utf8) {
configuration.logHandler?.log(.debug, message: messageString)
}
}
}
Expand Down Expand Up @@ -196,7 +196,10 @@ private extension SignalManager {
}

urlRequest.httpBody = body
self.configuration.logHandler?.log(.debug, message: String(decoding: urlRequest.httpBody!, as: UTF8.self))

if let data = urlRequest.httpBody, let messageString = String(data: data, encoding: .utf8) {
self.configuration.logHandler?.log(.debug, message: messageString)
}

let task = URLSession.shared.dataTask(with: urlRequest, completionHandler: completionHandler)
task.resume()
Expand All @@ -210,7 +213,7 @@ private extension SignalManager {
#if os(macOS)
/// A custom ``UserDefaults`` instance specific to TelemetryDeck and the current application.
private var customDefaults: UserDefaults? {
let appIdHash = CryptoHashing.sha256(string: self.configuration.telemetryAppID, salt: "")
let appIdHash = CryptoHashing.sha256(string: configuration.telemetryAppID, salt: "")
return UserDefaults(suiteName: "com.telemetrydeck.\(appIdHash.suffix(12))")
}
#endif
Expand All @@ -221,28 +224,28 @@ private extension SignalManager {
guard configuration.defaultUser == nil else { return configuration.defaultUser! }

#if os(iOS) || os(tvOS) || os(visionOS)
return UIDevice.current.identifierForVendor?.uuidString ?? "unknown user \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
return UIDevice.current.identifierForVendor?.uuidString ?? "unknown user \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
#elseif os(watchOS)
if #available(watchOS 6.2, *) {
return WKInterfaceDevice.current().identifierForVendor?.uuidString ?? "unknown user \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
} else {
return "unknown user \(DefaultSignalPayload.platform) \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
}
if #available(watchOS 6.2, *) {
return WKInterfaceDevice.current().identifierForVendor?.uuidString ?? "unknown user \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
} else {
return "unknown user \(DefaultSignalPayload.platform) \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
}
#elseif os(macOS)
if let customDefaults = self.customDefaults, let defaultUserIdentifier = customDefaults.string(forKey: "defaultUserIdentifier") {
return defaultUserIdentifier
} else {
let defaultUserIdentifier = UUID().uuidString
self.customDefaults?.set(defaultUserIdentifier, forKey: "defaultUserIdentifier")
return defaultUserIdentifier
}
if let customDefaults = customDefaults, let defaultUserIdentifier = customDefaults.string(forKey: "defaultUserIdentifier") {
return defaultUserIdentifier
} else {
let defaultUserIdentifier = UUID().uuidString
customDefaults?.set(defaultUserIdentifier, forKey: "defaultUserIdentifier")
return defaultUserIdentifier
}
#else
#if DEBUG
let line1 = "[Telemetry] On this platform, Telemetry can't generate a unique user identifier."
let line2 = "It is recommended you supply one yourself. More info: https://telemetrydeck.com/pages/signal-reference.html"
configuration.logHandler?.log(message: "\(line1) \(line2)")
#endif
return "unknown user \(DefaultSignalPayload.platform) \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
#if DEBUG
let line1 = "[Telemetry] On this platform, Telemetry can't generate a unique user identifier."
let line2 = "It is recommended you supply one yourself. More info: https://telemetrydeck.com/pages/signal-reference.html"
configuration.logHandler?.log(message: "\(line1) \(line2)")
#endif
return "unknown user \(DefaultSignalPayload.platform) \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
#endif
}
}
Expand Down

0 comments on commit 5d9ab8e

Please sign in to comment.