Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Nov 2, 2023
2 parents 9e2940b + 52a6d76 commit 3ef6553
Show file tree
Hide file tree
Showing 37 changed files with 1,888 additions and 1,018 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/build-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build Examples
on:
push:
branches:
- master
- main
- v3.0.0
pull_request:
jobs:
build:
runs-on: macos-13
steps:
- uses: actions/checkout@v4
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'
- name: Build Example
run: make buildExample
8 changes: 0 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ jobs:
runs-on: macos-13
steps:
- uses: actions/checkout@v4

- uses: swift-actions/setup-swift@v1
with:
swift-version: "5.3.0"
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'
- name: Build SDK
run: make buildSdk
- name: Build Example
run: make buildExample
- name: Test SDK
run: make test
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests
on:
push:
branches:
- master
- main
- v3.0.0
pull_request:
jobs:
test:
runs-on: macos-13
steps:
- uses: actions/checkout@v4
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'
- name: Test SDK
run: make test
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build buildSdk buildExample format swiftLint swiftFormat test lint bootstrap releaseCocoaPods
.PHONY: build buildSdk buildExample format swiftLint swiftFormat test testOnSimulator lint bootstrap releaseCocoaPods

build: buildSdk buildExample

Expand All @@ -16,9 +16,12 @@ swiftLint:
swiftFormat:
swiftformat . --swiftversion 5.3

test:
testOnSimulator:
set -o pipefail && xcodebuild test -project PostHog.xcodeproj -scheme PostHog -destination 'platform=iOS Simulator,name=iPhone 15,OS=17.0.1' | xcpretty

test:
swift test

lint:
swiftformat . --lint --swiftversion 5.3 && swiftlint

Expand Down
52 changes: 52 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"object": {
"pins": [
{
"package": "CwlCatchException",
"repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git",
"state": {
"branch": null,
"revision": "3b123999de19bf04905bc1dfdb76f817b0f2cc00",
"version": "2.1.2"
}
},
{
"package": "CwlPreconditionTesting",
"repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state": {
"branch": null,
"revision": "a23ded2c91df9156628a6996ab4f347526f17b6b",
"version": "2.1.2"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "edaedc1ec86f14ac6e2ca495b94f0ff7150d98d0",
"version": "12.3.0"
}
},
{
"package": "OHHTTPStubs",
"repositoryURL": "https://github.com/AliSoftware/OHHTTPStubs.git",
"state": {
"branch": null,
"revision": "12f19662426d0434d6c330c6974d53e2eb10ecd9",
"version": "9.1.0"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "16910e406be96e08923918315388c3e989deac9e",
"version": "6.1.0"
}
}
]
},
"version": 1
}
11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Quick/Quick.git", from: "6.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "12.0.0"),
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -25,7 +28,13 @@ let package = Package(
),
.testTarget(
name: "PostHogTests",
dependencies: ["PostHog"],
dependencies: [
"PostHog",
"Quick",
"Nimble",
"OHHTTPStubs",
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
],
path: "PostHogTests"
),
]
Expand Down
120 changes: 64 additions & 56 deletions PostHog.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions PostHog/PostHogApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PostHogApi {

do {
data = try JSONSerialization.data(withJSONObject: toSend)
} catch let error as NSError {
} catch {
return completion(PostHogBatchUploadInfo(statusCode: nil, error: error))
}

Expand Down Expand Up @@ -109,7 +109,7 @@ class PostHogApi {

do {
data = try JSONSerialization.data(withJSONObject: toSend)
} catch let error as NSError {
} catch {
return completion(nil, error)
}

Expand All @@ -128,7 +128,7 @@ class PostHogApi {
do {
let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
completion(jsonData, nil)
} catch let error as NSError {
} catch {
completion(nil, error)
}
}.resume()
Expand Down
4 changes: 4 additions & 0 deletions PostHog/PostHogConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import Foundation
@objc public var optOut: Bool = false
public static let defaultHost: String = "https://app.posthog.com"

// only internal
var disableReachabilityForTesting: Bool = false
var disableQueueTimerForTesting: Bool = false

@objc(apiKey:)
public init(
apiKey: String
Expand Down
5 changes: 2 additions & 3 deletions PostHog/PostHogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class PostHogContext {
if deviceType != nil {
properties["$device_type"] = deviceType
}

#endif

return properties
Expand All @@ -87,8 +86,8 @@ class PostHogContext {
var properties: [String: Any] = [:]

#if os(iOS) || os(tvOS)
properties["$screen_width"] = UIScreen.main.bounds.width
properties["$screen_height"] = UIScreen.main.bounds.height
properties["$screen_width"] = Float(UIScreen.main.bounds.width)
properties["$screen_height"] = Float(UIScreen.main.bounds.height)
#endif

properties["$lib"] = "posthog-ios"
Expand Down
47 changes: 29 additions & 18 deletions PostHog/PostHogFeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class PostHogFeatureFlags {
private let featureFlagsLock = NSLock()
private var isLoadingFeatureFlags = false

private let dispatchQueue = DispatchQueue(label: "com.posthog.FeatureFlags", target: .global(qos: .utility))
private let dispatchQueue = DispatchQueue(label: "com.posthog.FeatureFlags",
target: .global(qos: .utility))

init(_ config: PostHogConfig, _ storage: PostHogStorage, _ api: PostHogApi) {
init(_ config: PostHogConfig,
_ storage: PostHogStorage,
_ api: PostHogApi)
{
self.config = config
self.storage = storage
self.api = api
Expand Down Expand Up @@ -52,7 +56,9 @@ class PostHogFeatureFlags {
let featureFlagPayloads = data?["featureFlagPayloads"] as? [String: Any]
else {
hedgeLog("Error: Decide response missing correct featureFlags format")
self.setLoading(false)

self.notifyAndRelease()

return callback()
}
let errorsWhileComputingFlags = data?["errorsWhileComputingFlags"] as? Bool ?? false
Expand All @@ -74,17 +80,21 @@ class PostHogFeatureFlags {
}
}

DispatchQueue.main.async {
NotificationCenter.default.post(name: PostHogSDK.didReceiveFeatureFlags, object: nil)
}

self.setLoading(false)
self.notifyAndRelease()

return callback()
}
}
}

private func notifyAndRelease() {
DispatchQueue.main.async {
NotificationCenter.default.post(name: PostHogSDK.didReceiveFeatureFlags, object: nil)
}

setLoading(false)
}

func getFeatureFlags() -> [String: Any]? {
var flags: [String: Any]?
featureFlagsLock.withLock {
Expand All @@ -103,9 +113,9 @@ class PostHogFeatureFlags {
let value = flags?[key]

if value != nil {
let boolValue = value as? Bool ?? false
if boolValue {
return boolValue
let boolValue = value as? Bool
if boolValue != nil {
return boolValue!
} else {
return true
}
Expand Down Expand Up @@ -135,14 +145,15 @@ class PostHogFeatureFlags {
return value
}

// The payload value is stored as a string and is not pre-parsed...
// We need to mimic the JSON.parse of JS which is what posthog-js uses
let jsonData = try? JSONSerialization.jsonObject(with: stringValue.data(using: .utf8)!, options: .fragmentsAllowed)

if jsonData == nil {
return value
do {
// The payload value is stored as a string and is not pre-parsed...
// We need to mimic the JSON.parse of JS which is what posthog-js uses
return try JSONSerialization.jsonObject(with: stringValue.data(using: .utf8)!, options: .fragmentsAllowed)
} catch {
hedgeLog("Error parsing the object \(String(describing: value)): \(error)")
}

return jsonData
// fallbak to original value if not possible to serialize
return value
}
}
Loading

0 comments on commit 3ef6553

Please sign in to comment.