Skip to content

Commit

Permalink
Multiplatform Swift Support (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpiazza authored Mar 23, 2022
1 parent 9d27ab1 commit 6d23afa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 63 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ jobs:
run: swift build -v
- name: Run tests
run: swift test -v

ubuntu-build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build (Ubuntu)
run: swift build -v
- name: Run tests
run: swift test -v
59 changes: 0 additions & 59 deletions FlagsmithAnalytics.swift

This file was deleted.

3 changes: 3 additions & 0 deletions FlagsmithClient/Classes/Internal/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// Handles interaction with a **Flagsmith** api.
class APIManager {
Expand Down
35 changes: 31 additions & 4 deletions FlagsmithClient/Classes/Internal/FlagsmithAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,33 @@ class FlagsmithAnalytics {
}

/// Invalidate and re-schedule timer for processing events
///
/// On Apple (Darwin) platforms, this uses the Objective-C based
/// target/selector message sending API.
///
/// Non-Darwin systems will use the corelibs Foundation block-based
/// api. Both platforms could use this approach, but the podspec
/// declares iOS 8.0 as a minimum target, and that api is only
/// available on 10+. (12.0 would be a good base in the future).
private func setupTimer() {
timer?.invalidate()
#if canImport(ObjectiveC)
timer = Timer.scheduledTimer(
timeInterval: TimeInterval(flushPeriod),
target: self,
selector: #selector(postAnalytics(_:)),
selector: #selector(postAnalyticsWhenEnabled(_:)),
userInfo: nil,
repeats: true
)
#else
timer = Timer.scheduledTimer(
withTimeInterval: TimeInterval(flushPeriod),
repeats: true,
block: { [weak self] _ in
self?.postAnalytics()
}
)
#endif
}

/// Reset events after successful processing.
Expand All @@ -61,15 +79,15 @@ class FlagsmithAnalytics {
}

/// Send analytics to the api when enabled.
@objc private func postAnalytics(_ timer: Timer) {
private func postAnalytics() {
guard enableAnalytics else {
return
}

guard !events.isEmpty else {
return
}

apiManager.request(.postAnalytics(events: events)) { [weak self] (result: Result<Void, Error>) in
switch result {
case .failure:
Expand All @@ -79,4 +97,13 @@ class FlagsmithAnalytics {
}
}
}

#if canImport(ObjectiveC)
/// Event triggered when timer fired.
///
/// Exposed on Apple platforms to relay selector-based events
@objc private func postAnalyticsWhenEnabled(_ timer: Timer) {
postAnalytics()
}
#endif
}
3 changes: 3 additions & 0 deletions FlagsmithClient/Classes/Internal/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

enum Router {
private enum HTTPMethod: String {
Expand Down

0 comments on commit 6d23afa

Please sign in to comment.