Skip to content

Commit

Permalink
Add support for visionOS
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Sep 27, 2023
1 parent 1f76199 commit 0355796
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 27 deletions.
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// swift-tools-version:5.2
// swift-tools-version:5.9

import PackageDescription

let package = Package(
name: "SnowplowTracker",
defaultLocalization: "en",
platforms: [
.macOS("10.13"),
.iOS("11.0"),
.tvOS("12.0"),
.watchOS("6.0")
.watchOS("6.0"),
.visionOS("1.0")
],
products: [
.library(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/Storage/SQLiteEventStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

#if os(iOS) || os(macOS)
#if os(iOS) || os(macOS) || os(visionOS)

import FMDB
import Foundation
Expand Down
8 changes: 4 additions & 4 deletions Sources/Core/Subject/PlatformContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// language governing permissions and limitations there under.

import Foundation
#if os(iOS)
#if os(iOS) || os(visionOS)
import UIKit
#endif

Expand Down Expand Up @@ -44,7 +44,7 @@ class PlatformContext {
self.mobileDictUpdateFrequency = mobileDictUpdateFrequency
self.networkDictUpdateFrequency = networkDictUpdateFrequency
self.deviceInfoMonitor = deviceInfoMonitor
#if os(iOS)
#if os(iOS) || os(visionOS)
UIDevice.current.isBatteryMonitoringEnabled = true
#endif
setPlatformDict()
Expand All @@ -53,7 +53,7 @@ class PlatformContext {
/// Updates and returns payload dictionary with device context information.
/// - Parameter userAnonymisation: Whether to anonymise user identifiers (IDFA values)
func fetchPlatformDict(userAnonymisation: Bool, advertisingIdentifierRetriever: (() -> UUID?)?) -> Payload {
#if os(iOS)
#if os(iOS) || os(visionOS)
objc_sync_enter(self)
let now = Date().timeIntervalSince1970
if now - lastUpdatedEphemeralMobileDict >= mobileDictUpdateFrequency {
Expand Down Expand Up @@ -89,7 +89,7 @@ class PlatformContext {
platformDict[kSPPlatformDeviceManu] = deviceInfoMonitor.deviceVendor
platformDict[kSPPlatformDeviceModel] = deviceInfoMonitor.deviceModel

#if os(iOS)
#if os(iOS) || os(visionOS)
setMobileDict()
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/Tracker/Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class Tracker: NSObject {
super.init()
builder(self)

#if os(iOS)
#if os(iOS) || os(visionOS)
platformContextSchema = kSPMobileContextSchema
#else
platformContextSchema = kSPDesktopContextSchema
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/Tracker/WebViewMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import Foundation

#if os(iOS) || os(macOS)
#if os(iOS) || os(macOS) || os(visionOS)
import WebKit

/// Handler for messages from the JavaScript library embedded in Web views.
Expand Down
20 changes: 11 additions & 9 deletions Sources/Core/Utils/DeviceInfoMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import WatchKit
#if os(iOS)
import CoreTelephony
#endif
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
import UIKit
#endif

Expand All @@ -28,7 +28,7 @@ class DeviceInfoMonitor {
/// Returns the generated identifier for vendors. More info can be found in UIDevice's identifierForVendor documentation.
/// - Returns: A string containing a formatted UUID for example E621E1F8-C36C-495A-93FC-0C247A3E6E5F.
var appleIdfv: String? {
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
if let idfv = UIDevice.current.identifierForVendor?.uuidString {
return idfv
}
Expand Down Expand Up @@ -60,7 +60,7 @@ class DeviceInfoMonitor {
/// This is to detect what the version of mobile OS of the current device.
/// - Returns: The current device's OS version type as a string.
var osVersion: String? {
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
return UIDevice.current.systemVersion
#elseif os(watchOS)
return WKInterfaceDevice.current().systemVersion
Expand All @@ -85,6 +85,8 @@ class DeviceInfoMonitor {
return "tvos"
#elseif os(watchOS)
return "watchos"
#elseif os(visionOS)
return "visionos"
#else
return "osx"
#endif
Expand Down Expand Up @@ -146,7 +148,7 @@ class DeviceInfoMonitor {
/// Returns the Network Type the device is connected to.
/// - Returns: A string containing the Network Type.
var networkType: String? {
#if os(iOS)
#if os(iOS) || os(visionOS)
let networkStatus = SNOWReachability.forInternetConnection()?.networkStatus
switch networkStatus {
case .offline:
Expand All @@ -165,7 +167,7 @@ class DeviceInfoMonitor {
/// Returns remaining battery level as an integer percentage of total battery capacity.
/// - Returns: Battery level.
var batteryLevel: Int? {
#if os(iOS)
#if os(iOS) || os(visionOS)
let batteryLevel = UIDevice.current.batteryLevel
if batteryLevel != Float(UIDevice.BatteryState.unknown.rawValue) && batteryLevel >= 0 {
return Int(batteryLevel * 100)
Expand All @@ -177,7 +179,7 @@ class DeviceInfoMonitor {
/// Returns battery state for the device.
/// - Returns: One of "charging", "full", "unplugged" or NULL
var batteryState: String? {
#if os(iOS)
#if os(iOS) || os(visionOS)
switch UIDevice.current.batteryState {
case .charging:
return "charging"
Expand All @@ -196,7 +198,7 @@ class DeviceInfoMonitor {
/// Returns whether low power mode is activated.
/// - Returns: Boolean indicating the state of low power mode.
var isLowPowerModeEnabled: Bool? {
#if os(iOS)
#if os(iOS) || os(visionOS)
return ProcessInfo.processInfo.isLowPowerModeEnabled
#else
return nil
Expand Down Expand Up @@ -225,7 +227,7 @@ class DeviceInfoMonitor {
/// Returns number of bytes of storage remaining. The information is requested from the home directory.
/// - Returns: Bytes of storage remaining.
var availableStorage: Int64? {
#if os(iOS)
#if os(iOS) || os(visionOS)
let fileURL = URL(fileURLWithPath: NSHomeDirectory() as String)
do {
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
Expand All @@ -240,7 +242,7 @@ class DeviceInfoMonitor {
/// Returns the total number of bytes of storage. The information is requested from the home directory.
/// - Returns: Total size of storage in bytes.
var totalStorage: Int? {
#if os(iOS)
#if os(iOS) || os(visionOS)
let fileURL = URL(fileURLWithPath: NSHomeDirectory() as String)
do {
let values = try fileURL.resourceValues(forKeys: [.volumeTotalCapacityKey])
Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/Utils/SNOWReachability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

#if os(iOS)
#if os(iOS) || os(visionOS)
import Foundation
import SystemConfiguration

Expand Down Expand Up @@ -65,7 +65,7 @@ class SNOWReachability: NSObject {
return .offline
}

#if os(iOS)
#if os(iOS) || os(visionOS)
let isWWAN = (flags.rawValue & SCNetworkReachabilityFlags.isWWAN.rawValue) == SCNetworkReachabilityFlags.isWWAN.rawValue
if isWWAN {
return .wwan
Expand Down
6 changes: 3 additions & 3 deletions Sources/Core/Utils/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

#if os(iOS)
#if os(iOS) || os(tvOS) || os(visionOS)
import UIKit
#elseif os(macOS)
import AppKit
#elseif os(tvOS)
import UIKit
#elseif os(watchOS)
import WatchKit
#endif
Expand All @@ -40,6 +38,8 @@ class Utilities {
class var platform: DevicePlatform {
#if os(iOS)
return .mobile
#elseif os(visionOS)
return .headset
#else
return .desktop
#endif
Expand Down
5 changes: 4 additions & 1 deletion Sources/Snowplow/Tracker/DevicePlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum DevicePlatform : Int {
case connectedTV
case gameConsole
case internetOfThings
case headset
}

func devicePlatformToString(_ devicePlatform: DevicePlatform) -> String? {
Expand All @@ -43,11 +44,13 @@ func devicePlatformToString(_ devicePlatform: DevicePlatform) -> String? {
return "cnsl"
case .internetOfThings:
return "iot"
case .headset:
return "headset"
}
}

func stringToDevicePlatform(_ devicePlatformString: String) -> DevicePlatform? {
if let index = ["web", "mob", "pc", "srv", "app", "tv", "cnsl", "iot"].firstIndex(of: devicePlatformString) {
if let index = ["web", "mob", "pc", "srv", "app", "tv", "cnsl", "iot", "headset"].firstIndex(of: devicePlatformString) {
return DevicePlatform(rawValue: index)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestSQLiteEventStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

#if os(iOS) || os(macOS)
#if os(iOS) || os(macOS) || os(visionOS)

import XCTest
@testable import SnowplowTracker
Expand Down
4 changes: 4 additions & 0 deletions Tests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ class TestUtils: XCTestCase {
func testGetPlatform() {
#if os(iOS)
XCTAssertEqual(Utilities.platform, .mobile)
#elseif os(visionOS)
XCTAssertEqual(Utilities.platform, .headset)
#else
XCTAssertEqual(Utilities.platform, .desktop)
#endif
}

func testGetResolution() {
#if !os(visionOS)
let actualResolution = Utilities.resolution
XCTAssertTrue(actualResolution != nil)
#endif
}

func testGetEventId() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestWebViewMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

#if os(iOS) || os(macOS)
#if os(iOS) || os(macOS) || os(visionOS)
import XCTest
@testable import SnowplowTracker

Expand Down
2 changes: 1 addition & 1 deletion Tests/Utils/MockWKScriptMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

#if os(iOS) || os(macOS)
#if os(iOS) || os(macOS) || os(visionOS)
import WebKit

class MockWKScriptMessage: WKScriptMessage {
Expand Down

0 comments on commit 0355796

Please sign in to comment.