Skip to content

Commit

Permalink
feat: get currently registered device token (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ami-aman committed Oct 31, 2023
1 parent ee3c272 commit cdedab0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Apps/CocoaPods-FCM/src/View/SettingsView.swift
Expand Up @@ -156,7 +156,7 @@ struct SettingsView: View {
init() {
self.settingsManager = CioSettingsManager()
self.keyValueStorage = KeyValueStore()
self.pushToken = keyValueStorage.pushToken ?? "(none)"
self.pushToken = CustomerIO.shared.registeredDeviceToken ?? "(none)"
self.settings = settingsManager.appSetSettings ?? CioSettings.getFromCioSdk()
}

Expand Down
14 changes: 14 additions & 0 deletions Sources/Tracking/CustomerIO.swift
Expand Up @@ -21,6 +21,8 @@ public protocol CustomerIOInstance: AutoMockable {
body: RequestBody
)

var registeredDeviceToken: String? { get }

func clearIdentify()

func track(
Expand Down Expand Up @@ -238,6 +240,18 @@ public class CustomerIO: CustomerIOInstance {
)
}

/**
Use `registeredDeviceToken` to fetch the current FCM/APN device token.
This returns an optional string value.
Example use:
```
CustomerIO.shared.registeredDeviceToken
```
*/
public var registeredDeviceToken: String? {
implementation?.registeredDeviceToken
}

public var config: SdkConfig? {
implementation?.config
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Tracking/CustomerIOImplementation.swift
Expand Up @@ -72,6 +72,10 @@ class CustomerIOImplementation: CustomerIOInstance {
}
}

public var registeredDeviceToken: String? {
globalDataStore.pushDeviceToken
}

// this function could use a refactor. It's long and complex. Our automated tests are what keeps us feeling
// confident in the code, but the code here is difficult to maintain.
// swiftlint:disable:next function_body_length
Expand Down
39 changes: 39 additions & 0 deletions Sources/Tracking/autogenerated/AutoMockable.generated.swift
Expand Up @@ -210,6 +210,42 @@ public class CustomerIOInstanceMock: CustomerIOInstance, Mock {
}
}

/**
When setter of the property called, the value given to setter is set here.
When the getter of the property called, the value set here will be returned. Your chance to mock the property.
*/
public var underlyingRegisteredDeviceToken: String? = nil
/// `true` if the getter or setter of property is called at least once.
public var registeredDeviceTokenCalled: Bool {
registeredDeviceTokenGetCalled || registeredDeviceTokenSetCalled
}

/// `true` if the getter called on the property at least once.
public var registeredDeviceTokenGetCalled: Bool {
registeredDeviceTokenGetCallsCount > 0
}

public var registeredDeviceTokenGetCallsCount = 0
/// `true` if the setter called on the property at least once.
public var registeredDeviceTokenSetCalled: Bool {
registeredDeviceTokenSetCallsCount > 0
}

public var registeredDeviceTokenSetCallsCount = 0
/// The mocked property with a getter and setter.
public var registeredDeviceToken: String? {
get {
mockCalled = true
registeredDeviceTokenGetCallsCount += 1
return underlyingRegisteredDeviceToken
}
set(value) {
mockCalled = true
registeredDeviceTokenSetCallsCount += 1
underlyingRegisteredDeviceToken = value
}
}

/**
When setter of the property called, the value given to setter is set here.
When the getter of the property called, the value set here will be returned. Your chance to mock the property.
Expand Down Expand Up @@ -289,6 +325,9 @@ public class CustomerIOInstanceMock: CustomerIOInstance, Mock {
config = nil
configGetCallsCount = 0
configSetCallsCount = 0
registeredDeviceToken = nil
registeredDeviceTokenGetCallsCount = 0
registeredDeviceTokenSetCallsCount = 0
profileAttributesGetCallsCount = 0
profileAttributesSetCallsCount = 0
deviceAttributesGetCallsCount = 0
Expand Down
11 changes: 11 additions & 0 deletions Tests/Tracking/CustomerIOImplementationTest.swift
Expand Up @@ -296,6 +296,17 @@ class CustomerIOImplementationTest: UnitTest {
XCTAssertEqual(globalDataStoreMock.pushDeviceToken, givenDeviceToken)
}

func test_registeredDeviceToken_givenDeviceTokenAlreadySaved_expectToken() {
let givenDeviceToken = String.random
customerIO.registerDeviceToken(givenDeviceToken)

XCTAssertEqual(customerIO.registeredDeviceToken, givenDeviceToken)
}

func test_registeredDeviceToken_givenDeviceTokenNotSaved_expectNil() {
XCTAssertNil(customerIO.registeredDeviceToken)
}

func test_registerDeviceToken_givenCustomerIdentified_expectAddTaskToQueue_expectStoreDeviceToken() {
let givenDeviceToken = String.random
let givenIdentifier = String.random
Expand Down

0 comments on commit cdedab0

Please sign in to comment.