Skip to content

Commit

Permalink
fix: ParseAnalytics trackAppOpened had ambiguous inits (#55)
Browse files Browse the repository at this point in the history
* fix: ParseAnalytics trackAppOpened had ambiguous inits

* improve trackAppOpen
  • Loading branch information
cbaker6 authored Jan 30, 2023
1 parent 2494b3f commit 9e7f393
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ __New features__
* The max connection attempts for LiveQuery can now be changed when initializing the SDK ([#43](https://github.com/netreconlab/Parse-Swift/pull/43)), thanks to [Corey Baker](https://github.com/cbaker6).

__Fixes__
* Fixed ambiguous ParseAnalytics trackAppOpenned ([#55](https://github.com/netreconlab/Parse-Swift/pull/55)), thanks to [Corey Baker](https://github.com/cbaker6).
* Refactored playground mount to be "/parse" instead "/1". Also do not require url when decoding a ParseFile ([#52](https://github.com/netreconlab/Parse-Swift/pull/52)), thanks to [Corey Baker](https://github.com/cbaker6).
* Fixed issues that can cause cache misses when querying ([#46](https://github.com/netreconlab/Parse-Swift/pull/46)), thanks to [Corey Baker](https://github.com/cbaker6).
* Fixed a threading issue with .current objects that can cause apps to crash
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "5.0.0-beta.6"
static let version = "5.0.0-beta.7"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
6 changes: 5 additions & 1 deletion Sources/ParseSwift/Types/ParseAnalytics+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public extension ParseAnalytics {
// MARK: Aysnc/Await

#if os(iOS)

/**
Tracks *asynchronously* this application being launched. If this happened as the result of the
user opening a push notification, this method sends along information to
Expand All @@ -31,9 +32,10 @@ public extension ParseAnalytics {
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- throws: An error of type `ParseError`.
*/
static func trackAppOpened(launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil,
static func trackAppOpened(launchOptions: [UIApplication.LaunchOptionsKey: Any],
at date: Date? = nil,
options: API.Options = []) async throws {

let result = try await withCheckedThrowingContinuation { continuation in
Self.trackAppOpened(launchOptions: launchOptions,
at: date,
Expand All @@ -43,7 +45,9 @@ public extension ParseAnalytics {
if case let .failure(error) = result {
throw error
}

}

#endif

/**
Expand Down
5 changes: 4 additions & 1 deletion Sources/ParseSwift/Types/ParseAnalytics+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public extension ParseAnalytics {
// MARK: Combine

#if os(iOS)

/**
Tracks *asynchronously* this application being launched. If this happened as the result of the
user opening a push notification, this method sends along information to
Expand All @@ -32,16 +33,18 @@ public extension ParseAnalytics {
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
*/
static func trackAppOpenedPublisher(launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil,
static func trackAppOpenedPublisher(launchOptions: [UIApplication.LaunchOptionsKey: Any],
at date: Date? = nil,
options: API.Options = []) -> Future<Void, ParseError> {

Future { promise in
Self.trackAppOpened(launchOptions: launchOptions,
at: date,
options: options,
completion: promise)
}
}

#endif

/**
Expand Down
13 changes: 9 additions & 4 deletions Sources/ParseSwift/Types/ParseAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public struct ParseAnalytics: ParseTypeable, Hashable {
// MARK: Intents

#if os(iOS)

/**
Tracks *asynchronously* this application being launched. If this happened as the result of the
user opening a push notification, this method sends along information to
Expand All @@ -99,16 +100,20 @@ public struct ParseAnalytics: ParseTypeable, Hashable {
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
desires a different policy, it should be inserted in `options`.
*/
static public func trackAppOpened(launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil,
static public func trackAppOpened(launchOptions: [UIApplication.LaunchOptionsKey: Any],
at date: Date? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<Void, ParseError>) -> Void) {

var options = options
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
var userInfo: [String: String]?
if let remoteOptions = launchOptions?[.remoteNotification] as? [String: String] {
userInfo = remoteOptions
var userInfo = [String: String]()
launchOptions.forEach { (key, value) in
guard let value = value as? String else {
return
}
userInfo[key.rawValue] = value
}
let appOppened = ParseAnalytics(name: "AppOpened",
dimensions: userInfo,
Expand Down
26 changes: 26 additions & 0 deletions Tests/ParseSwiftTests/ParseAnalyticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ class ParseAnalyticsTests: XCTestCase {
return MockURLResponse(data: encoded, statusCode: 200)
}

let expectation = XCTestExpectation(description: "Analytics save")
let options = [UIApplication.LaunchOptionsKey.remoteNotification: "stop"]
ParseAnalytics.trackAppOpened(launchOptions: options) { result in

if case .failure(let error) = result {
XCTFail(error.localizedDescription)
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 10.0)
}

func testTrackAppOpenedUIKitNotStringValue() {
let serverResponse = NoBody()
let encoded: Data!
do {
encoded = try ParseCoding.jsonEncoder().encode(serverResponse)
} catch {
XCTFail("Should encode/decode. Error \(error)")
return
}

MockURLProtocol.mockRequests { _ in
return MockURLResponse(data: encoded, statusCode: 200)
}

let expectation = XCTestExpectation(description: "Analytics save")
let options = [UIApplication.LaunchOptionsKey.remoteNotification: ["stop": "drop"]]
ParseAnalytics.trackAppOpened(launchOptions: options) { result in
Expand Down

0 comments on commit 9e7f393

Please sign in to comment.