Skip to content

Commit 318fb0d

Browse files
authored
fix: Cache Functionality Fixes and Comprehensive Test Suite (#90)
1 parent 88fdb10 commit 318fb0d

19 files changed

Lines changed: 2939 additions & 263 deletions

.github/workflows/pull-request.yml

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,39 @@ on:
77
branches: [main]
88

99
jobs:
10-
macos-build-14:
10+
macos-latest:
1111
# macOS-latest images are not the most recent
12-
# The macos-latest workflow label currently uses the macOS 12 runner image, which doesn't include the build-tools we need
1312
# The vast majority of macOS developers would be using the latest version of macOS
1413
# Current list here: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#choosing-github-hosted-runners
15-
runs-on: macOS-14
16-
17-
steps:
18-
- uses: actions/checkout@v4
19-
- name: Build (macOS)
20-
run: swift build -v
21-
- name: Run tests
22-
run: swift test -v
23-
24-
macos-build-13:
25-
# Let's also check that the code builds on macOS 13
26-
# At 23rd April 2023 the 'latest' macOS version is macOS 12
27-
runs-on: macOS-13
28-
14+
runs-on: macOS-latest
2915
steps:
3016
- uses: actions/checkout@v4
17+
- name: Check for FLAGSMITH_TEST_API_KEY
18+
run: |
19+
if [ -z "$FLAGSMITH_TEST_API_KEY" ]; then
20+
echo "Warning: FLAGSMITH_TEST_API_KEY environment variable is not set"
21+
exit 1
22+
fi
23+
env:
24+
FLAGSMITH_TEST_API_KEY: ${{ secrets.FLAGSMITH_TEST_API_KEY }}
3125
- name: Build (macOS)
32-
run: swift build -v
33-
- name: Run tests
34-
run: swift test -v
35-
36-
ubuntu-build:
37-
runs-on: ubuntu-latest
38-
39-
steps:
40-
- uses: actions/checkout@v4
41-
- name: Build (Ubuntu)
42-
run: swift build -v
26+
run: swift --version && swift build
4327
- name: Run tests
44-
run: swift test -v
45-
28+
run: swift test
29+
env:
30+
FLAGSMITH_TEST_API_KEY: ${{ secrets.FLAGSMITH_TEST_API_KEY }}
4631
swift-lint:
47-
4832
runs-on: ubuntu-latest
49-
5033
steps:
5134
- uses: actions/checkout@v4
35+
- name: Check for FLAGSMITH_TEST_API_KEY
36+
run: |
37+
if [ -z "$FLAGSMITH_TEST_API_KEY" ]; then
38+
echo "Warning: FLAGSMITH_TEST_API_KEY environment variable is not set"
39+
exit 1
40+
fi
41+
env:
42+
FLAGSMITH_TEST_API_KEY: ${{ secrets.FLAGSMITH_TEST_API_KEY }}
5243

5344
- name: Run SwiftLint
5445
uses: norio-nomura/action-swiftlint@3.2.1

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ Carthage/Build
5252
# This was causing confusion on PRs and is really just a record of the last time the example
5353
# pods were re-built from scratch. It's not useful to keep in the repo.
5454
Example/Podfile.lock
55+
56+
# Test configuration with real API keys (do not commit)
57+
FlagsmithClient/Tests/test-config.json

AppDelegate.swift

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// AppDelegate.swift
3+
// FlagsmithClient
4+
//
5+
// Created by Tomash Tsiupiak on 06/20/2019.
6+
// Copyright (c) 2019 Tomash Tsiupiak. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import FlagsmithClient
11+
#if canImport(SwiftUI)
12+
import SwiftUI
13+
#endif
14+
15+
func isSuccess<T, F>(_ result: Result<T, F>) -> Bool {
16+
if case .success = result { return true } else { return false }
17+
}
18+
19+
@UIApplicationMain
20+
class AppDelegate: UIResponder, UIApplicationDelegate {
21+
22+
var window: UIWindow?
23+
let concurrentQueue = DispatchQueue(label: "concurrentQueue", qos: .default, attributes: .concurrent)
24+
25+
func application(_ application: UIApplication,
26+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
27+
// Override point for customization after application launch.
28+
Flagsmith.shared.apiKey = "<add your API key from the Flagsmith dashboard here>"
29+
30+
// set default flags
31+
Flagsmith.shared.defaultFlags = [Flag(featureName: "feature_a", enabled: false),
32+
Flag(featureName: "font_size", intValue: 12, enabled: true),
33+
Flag(featureName: "my_name", stringValue: "Testing", enabled: true)]
34+
35+
// set cache on / off (defaults to off)
36+
Flagsmith.shared.cacheConfig.useCache = true
37+
38+
// set custom cache to use (defaults to shared URLCache)
39+
// Flagsmith.shared.cacheConfig.cache = <CUSTOM_CACHE>
40+
41+
// set skip API on / off (defaults to off)
42+
Flagsmith.shared.cacheConfig.skipAPI = false
43+
44+
// set cache TTL in seconds (defaults to 0, i.e. infinite)
45+
Flagsmith.shared.cacheConfig.cacheTTL = 90
46+
47+
// set analytics on or off
48+
Flagsmith.shared.enableAnalytics = true
49+
50+
// Enable real time updates
51+
Flagsmith.shared.enableRealtimeUpdates = true
52+
53+
// set the analytics flush period in seconds
54+
Flagsmith.shared.analyticsFlushPeriod = 10
55+
56+
Flagsmith.shared.getFeatureFlags { (result) in
57+
print(result)
58+
}
59+
Flagsmith.shared.hasFeatureFlag(withID: "freeze_delinquent_accounts") { (result) in
60+
print(result)
61+
}
62+
63+
// Try getting the feature flags concurrently to ensure that this does not cause any issues
64+
// This was originally highlighted in https://github.com/Flagsmith/flagsmith-ios-client/pull/40
65+
for _ in 1...20 {
66+
concurrentQueue.async {
67+
Flagsmith.shared.getFeatureFlags { (_) in
68+
}
69+
}
70+
}
71+
72+
// Flagsmith.shared.setTrait(Trait(key: "<my_key>", value: "<my_value>"), forIdentity: "<my_identity>") { (result) in print(result) }
73+
// Flagsmith.shared.getIdentity("<my_key>") { (result) in print(result) }
74+
75+
// Launch SwiftUIView
76+
if #available(iOS 13.0, *) {
77+
let swiftUIView = SwiftUIView()
78+
window = UIWindow(frame: UIScreen.main.bounds)
79+
window?.rootViewController = UIHostingController(rootView: swiftUIView)
80+
window?.makeKeyAndVisible()
81+
}
82+
83+
return true
84+
}
85+
86+
func applicationWillResignActive(_ application: UIApplication) {
87+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
88+
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
89+
}
90+
91+
func applicationDidEnterBackground(_ application: UIApplication) {
92+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
93+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
94+
}
95+
96+
func applicationWillEnterForeground(_ application: UIApplication) {
97+
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
98+
}
99+
100+
func applicationDidBecomeActive(_ application: UIApplication) {
101+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
102+
}
103+
104+
func applicationWillTerminate(_ application: UIApplication) {
105+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
106+
}
107+
108+
#if swift(>=5.5.2)
109+
/// (Example) Setup the app based on the available feature flags.
110+
///
111+
/// **Flagsmith** supports the Swift Concurrency feature `async`/`await`.
112+
/// Requests and logic can be handled in a streamlined order,
113+
/// eliminating the need to nest multiple completion handlers.
114+
@available(iOS 13.0, *)
115+
func determineAppConfiguration() async {
116+
let flagsmith = Flagsmith.shared
117+
118+
do {
119+
if try await flagsmith.hasFeatureFlag(withID: "ab_test_enabled") {
120+
if let theme = try await flagsmith.getValueForFeature(withID: "app_theme") {
121+
setTheme(theme)
122+
} else {
123+
let flags = try await flagsmith.getFeatureFlags()
124+
processFlags(flags)
125+
}
126+
} else {
127+
let trait = Trait(key: "selected_tint_color", value: "orange")
128+
let identity = "4DDBFBCA-3B6E-4C59-B107-954F84FD7F6D"
129+
try await flagsmith.setTrait(trait, forIdentity: identity)
130+
}
131+
} catch {
132+
print(error)
133+
}
134+
}
135+
136+
func setTheme(_ theme: TypedValue) {}
137+
func processFlags(_ flags: [Flag]) {}
138+
#endif
139+
}

Example/FlagsmithClient.xcodeproj/project.pbxproj

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 46;
6+
objectVersion = 77;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -143,14 +143,14 @@
143143
};
144144
};
145145
buildConfigurationList = 607FACCB1AFB9204008FA782 /* Build configuration list for PBXProject "FlagsmithClient" */;
146-
compatibilityVersion = "Xcode 3.2";
147146
developmentRegion = en;
148147
hasScannedForEncodings = 0;
149148
knownRegions = (
150149
en,
151150
Base,
152151
);
153152
mainGroup = 607FACC71AFB9204008FA782;
153+
preferredProjectObjectVersion = 77;
154154
productRefGroup = 607FACD11AFB9204008FA782 /* Products */;
155155
projectDirPath = "";
156156
projectRoot = "";
@@ -201,13 +201,16 @@
201201
buildActionMask = 2147483647;
202202
files = (
203203
);
204+
inputFileListPaths = (
205+
"${PODS_ROOT}/Target Support Files/Pods-FlagsmithClient_Example/Pods-FlagsmithClient_Example-frameworks-${CONFIGURATION}-input-files.xcfilelist",
206+
);
204207
inputPaths = (
205-
"${PODS_ROOT}/Target Support Files/Pods-FlagsmithClient_Example/Pods-FlagsmithClient_Example-frameworks.sh",
206-
"${BUILT_PRODUCTS_DIR}/FlagsmithClient/FlagsmithClient.framework",
207208
);
208209
name = "[CP] Embed Pods Frameworks";
210+
outputFileListPaths = (
211+
"${PODS_ROOT}/Target Support Files/Pods-FlagsmithClient_Example/Pods-FlagsmithClient_Example-frameworks-${CONFIGURATION}-output-files.xcfilelist",
212+
);
209213
outputPaths = (
210-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FlagsmithClient.framework",
211214
);
212215
runOnlyForDeploymentPostprocessing = 0;
213216
shellPath = /bin/sh;
@@ -350,7 +353,8 @@
350353
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
351354
MTL_ENABLE_DEBUG_INFO = NO;
352355
SDKROOT = iphoneos;
353-
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
356+
SWIFT_COMPILATION_MODE = wholemodule;
357+
SWIFT_OPTIMIZATION_LEVEL = "-O";
354358
VALIDATE_PRODUCT = YES;
355359
};
356360
name = Release;
@@ -362,7 +366,11 @@
362366
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
363367
DEVELOPMENT_TEAM = RYCT86V4LM;
364368
INFOPLIST_FILE = FlagsmithClient/Info.plist;
365-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
369+
IPHONEOS_DEPLOYMENT_TARGET = 15.6;
370+
LD_RUNPATH_SEARCH_PATHS = (
371+
"$(inherited)",
372+
"@executable_path/Frameworks",
373+
);
366374
MODULE_NAME = ExampleApp;
367375
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
368376
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -377,7 +385,11 @@
377385
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
378386
DEVELOPMENT_TEAM = RYCT86V4LM;
379387
INFOPLIST_FILE = FlagsmithClient/Info.plist;
380-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
388+
IPHONEOS_DEPLOYMENT_TARGET = 15.6;
389+
LD_RUNPATH_SEARCH_PATHS = (
390+
"$(inherited)",
391+
"@executable_path/Frameworks",
392+
);
381393
MODULE_NAME = ExampleApp;
382394
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
383395
PRODUCT_NAME = "$(TARGET_NAME)";

Example/FlagsmithClient/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2525
func application(_ application: UIApplication,
2626
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2727
// Override point for customization after application launch.
28-
Flagsmith.shared.apiKey = "<add your API key from the Flagsmith settings page>"
28+
Flagsmith.shared.apiKey = "<add your API key from the Flagsmith dashboard here>"
2929

3030
// set default flags
3131
Flagsmith.shared.defaultFlags = [Flag(featureName: "feature_a", enabled: false),

0 commit comments

Comments
 (0)