Skip to content

Commit 754fad1

Browse files
committed
clean up DemoSwiftApp
1 parent 5714a1e commit 754fad1

File tree

7 files changed

+250
-179
lines changed

7 files changed

+250
-179
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ -(void)initializeOptimizelySDKAsynchronous {
6666
}
6767

6868
-(void)initializeOptimizelySDKSynchronous {
69-
NSString *localDatafilePath = [[NSBundle bundleForClass:self.classForCoder] pathForResource:kOptimizelyDatafileName ofType:@"json"];
69+
NSString *localDatafilePath = [[NSBundle mainBundle] pathForResource:kOptimizelyDatafileName ofType:@"json"];
7070
if (localDatafilePath == nil) {
7171
NSAssert(false, @"Local datafile cannot be found");
7272
self.optimizely = nil;

DemoObjCApp/DemoObjcApp.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
isa = PBXFrameworksBuildPhase;
205205
buildActionMask = 2147483647;
206206
files = (
207-
6E4DD97C21E67AAD00B0C2C7 /* OptimizelySwiftSDK.framework in Frameworks */,
207+
6E4DD97C21E67AAD00B0C2C7 /* Optimizely.framework in Frameworks */,
208208
C2E08E88D2C89768C7551208 /* libPods-DemoObjciOS.a in Frameworks */,
209209
);
210210
runOnlyForDeploymentPostprocessing = 0;
@@ -226,13 +226,13 @@
226226
children = (
227227
6E4DD97521E67A6500B0C2C7 /* Optimizely.framework */,
228228
6E4DD97721E67A6500B0C2C7 /* Optimizely.framework */,
229-
6E4DD97921E67A6500B0C2C7 /* OptimizelySwiftSDK-iOSTests.xctest */,
229+
6E4DD97921E67A6500B0C2C7 /* OptimizelyTests-iOS.xctest */,
230230
6E8D3245226FAC4F00478458 /* OptimizelyTests-Common-iOS.xctest */,
231231
6E8D3247226FAC4F00478458 /* OptimizelyTests-APIs-iOS.xctest */,
232232
6E8D3249226FAC4F00478458 /* OptimizelyTests-DataModel-iOS.xctest */,
233233
6E8D324B226FAC4F00478458 /* OptimizelyTests-Others-iOS.xctest */,
234234
6E8D324D226FAC4F00478458 /* OptimizelyTests-Legacy-iOS.xctest */,
235-
6E4DD97B21E67A6500B0C2C7 /* OptimizelySwiftSDK-tvOSTests.xctest */,
235+
6E4DD97B21E67A6500B0C2C7 /* OptimizelyTests-tvOS.xctest */,
236236
6E8D324F226FAC4F00478458 /* OptimizelyTests-Common-tvOS.xctest */,
237237
6E8D3251226FAC4F00478458 /* OptimizelyTests-DataModel-tvOS.xctest */,
238238
6E8D3253226FAC4F00478458 /* OptimizelyTests-Others-tvOS.xctest */,
@@ -299,8 +299,8 @@
299299
6E9681A621EEA3D8009B7FEC /* Resources */ = {
300300
isa = PBXGroup;
301301
children = (
302-
6E9681A721EEA3D8009B7FEC /* Roboto-Medium.ttf */,
303302
6E9681A821EEA3D8009B7FEC /* demoTestDatafile.json */,
303+
6E9681A721EEA3D8009B7FEC /* Roboto-Medium.ttf */,
304304
6E9681A921EEA3D8009B7FEC /* ProximaNova-Regular.otf */,
305305
);
306306
path = Resources;
@@ -463,14 +463,14 @@
463463
remoteRef = 6E4DD97621E67A6500B0C2C7 /* PBXContainerItemProxy */;
464464
sourceTree = BUILT_PRODUCTS_DIR;
465465
};
466-
6E4DD97921E67A6500B0C2C7 /* OptimizelySwiftSDK-iOSTests.xctest */ = {
466+
6E4DD97921E67A6500B0C2C7 /* OptimizelyTests-iOS.xctest */ = {
467467
isa = PBXReferenceProxy;
468468
fileType = wrapper.cfbundle;
469469
path = "OptimizelyTests-iOS.xctest";
470470
remoteRef = 6E4DD97821E67A6500B0C2C7 /* PBXContainerItemProxy */;
471471
sourceTree = BUILT_PRODUCTS_DIR;
472472
};
473-
6E4DD97B21E67A6500B0C2C7 /* OptimizelySwiftSDK-tvOSTests.xctest */ = {
473+
6E4DD97B21E67A6500B0C2C7 /* OptimizelyTests-tvOS.xctest */ = {
474474
isa = PBXReferenceProxy;
475475
fileType = wrapper.cfbundle;
476476
path = "OptimizelyTests-tvOS.xctest";

DemoSwiftApp/AppDelegate.swift

Lines changed: 58 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2424
let experimentKey = "background_experiment"
2525
let eventKey = "sample_conversion"
2626

27-
let userId = String(Int.random(in: 0..<300000))
27+
let userId = String(Int.random(in: 0..<100000))
2828
let attributes: [String : Any?] = ["browser_type": "safari", "bool_attr": false]
2929

3030
var window: UIWindow?
@@ -50,48 +50,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5050
initializeOptimizelySDKAsynchronous()
5151
}
5252

53+
// MARK: - Initialization Examples
54+
5355
func initializeOptimizelySDKAsynchronous() {
56+
optimizely = OptimizelyManager(sdkKey: sdkKey)
5457

55-
// customization example (optional)
56-
let customLogger = makeCustomLogger()
57-
58-
optimizely = OptimizelyManager(sdkKey: sdkKey,
59-
logger: customLogger,
60-
periodicDownloadInterval:30)
61-
62-
_ = optimizely?.notificationCenter.addDatafileChangeNotificationListener(datafileListener: { (data) in
63-
DispatchQueue.main.async {
64-
#if os(iOS)
65-
let alert = UIAlertView(title: "Datafile change", message: "something changed.", delegate: nil, cancelButtonTitle: "cancel")
66-
alert.show()
67-
#else
68-
print("Datafile changed")
69-
#endif
70-
}
71-
if let controller = self.window?.rootViewController as? VariationViewController {
72-
//controller.showCoupon = toggle == FeatureFlagToggle.on ? true : false;
73-
if let showCoupon = try? self.optimizely?.isFeatureEnabled(featureKey: "show_coupon", userId: self.userId) {
74-
controller.showCoupon = showCoupon
75-
}
76-
77-
}
78-
})
79-
80-
_ = optimizely?.notificationCenter.addActivateNotificationListener(activateListener: { (experiment, userId, attributes, variation, event) in
81-
print("got activate notification")
82-
})
83-
84-
_ = optimizely?.notificationCenter.addTrackNotificationListener(trackListener: { (eventKey, userId, attributes, eventTags, event) in
85-
print(eventKey)
86-
print(userId)
87-
print(attributes)
88-
print(eventTags)
89-
print(event)
90-
print("got track notification")
91-
})
92-
93-
// initialize Optimizely Client from a datafile download
94-
optimizely!.initializeSDK { result in
58+
optimizely.initializeSDK { result in
9559
switch result {
9660
case .failure(let error):
9761
print("Optimizely SDK initiliazation failed: \(error)")
@@ -101,21 +65,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
10165
}
10266

10367
DispatchQueue.main.async {
104-
self.setRootViewController(optimizelyManager: self.optimizely)
68+
self.setRootViewController()
10569
}
10670
}
10771
}
10872

10973
func initializeOptimizelySDKSynchronous() {
110-
guard let localDatafilePath = Bundle(for: self.classForCoder).path(forResource: datafileName, ofType: "json") else {
74+
guard let localDatafilePath = Bundle.main.path(forResource: datafileName, ofType: "json") else {
11175
fatalError("Local datafile cannot be found")
11276
}
11377

114-
// customization example (optional)
115-
let customLogger = makeCustomLogger()
116-
117-
optimizely = OptimizelyManager(sdkKey: sdkKey,
118-
logger: customLogger)
78+
optimizely = OptimizelyManager(sdkKey: sdkKey)
11979

12080
do {
12181
let datafileJSON = try String(contentsOfFile: localDatafilePath, encoding: .utf8)
@@ -126,103 +86,84 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
12686
optimizely = nil
12787
}
12888

129-
setRootViewController(optimizelyManager: self.optimizely)
89+
setRootViewController()
13090
}
13191

132-
func setRootViewController(optimizelyManager: OptimizelyManager?) {
133-
guard let optimizely = optimizely else {
134-
openFailureView()
135-
return
136-
}
92+
func initializeOptimizelySDKWithCustomization() {
93+
// customization example (optional)
94+
95+
let customLogger = CustomLogger()
96+
let customDownloadIntervalInSecs = 30
97+
98+
optimizely = OptimizelyManager(sdkKey: sdkKey,
99+
logger: customLogger,
100+
periodicDownloadInterval: customDownloadIntervalInSecs)
101+
102+
// notification listeners
103+
104+
_ = optimizely?.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in
105+
print("Received decision notification: \(type) \(userId) \(String(describing: attributes)) \(decisionInfo)")
106+
})
107+
108+
_ = optimizely?.notificationCenter.addTrackNotificationListener(trackListener: { (eventKey, userId, attributes, eventTags, event) in
109+
print("Received track notification: \(eventKey) \(userId) \(String(describing: attributes)) \(String(describing: eventTags)) \(event)")
110+
})
111+
112+
// initialize SDK
137113

114+
optimizely!.initializeSDK { result in
115+
switch result {
116+
case .failure(let error):
117+
print("Optimizely SDK initiliazation failed: \(error)")
118+
self.optimizely = nil
119+
case .success:
120+
print("Optimizely SDK initialized successfully!")
121+
}
122+
123+
DispatchQueue.main.async {
124+
self.setRootViewController()
125+
}
126+
}
127+
}
128+
129+
// MARK: - ViewControl
130+
131+
func setRootViewController() {
138132
do {
139133
let variationKey = try optimizely.activate(experimentKey: experimentKey,
140134
userId: userId,
141135
attributes: attributes)
142-
openVariationView(optimizelyManager: optimizely, variationKey: variationKey)
143-
// used to test threading and datafile updates.
144-
//
145-
// DispatchQueue.global(qos: .background).async {
146-
// repeat {
147-
// do {
148-
// let userId = String(Int(arc4random_uniform(300000)))
149-
// let variationKey = try optimizely.activate(experimentKey: self.experimentKey,
150-
// userId: userId,
151-
// attributes: self.attributes)
152-
// print(variationKey)
153-
// }
154-
// catch let error {
155-
// print(error)
156-
// }
157-
// sleep(1)
158-
// }
159-
// while true
160-
//
161-
// }
136+
openVariationView(variationKey: variationKey)
162137
} catch OptimizelyError.variationUnknown(userId, experimentKey) {
163138
print("Optimizely SDK activation cannot map this user to experiemnt")
164-
openVariationView(optimizelyManager: optimizely, variationKey: nil)
139+
openVariationView(variationKey: nil)
165140
} catch {
166141
print("Optimizely SDK activation failed: \(error)")
167142
openFailureView()
168143
}
169144
}
170145

171-
172-
func makeCustomLogger() -> OPTLogger {
173-
class Logger : OPTLogger {
174-
static var level:OptimizelyLogLevel?
175-
static var logLevel: OptimizelyLogLevel {
176-
get {
177-
if let level = level {
178-
return level
179-
}
180-
return .all
181-
}
182-
set {
183-
if let _ = level {
184-
// already set.
185-
}
186-
else {
187-
level = newValue
188-
}
189-
}
190-
}
191-
192-
required init() {
193-
194-
}
195-
196-
func log(level: OptimizelyLogLevel, message: String) {
197-
if level.rawValue <= Logger.logLevel.rawValue {
198-
print("🐱 - [\(level.name)] Kitty - \(message)")
199-
}
200-
}
201-
202-
}
203-
204-
return Logger()
205-
}
206-
207-
func openVariationView(optimizelyManager: OptimizelyManager?, variationKey: String?) {
146+
func openVariationView(variationKey: String?) {
208147
let variationViewController = storyboard.instantiateViewController(withIdentifier: "VariationViewController") as! VariationViewController
209148

210-
if let showCoupon = try? optimizelyManager?.isFeatureEnabled(featureKey: "show_coupon", userId: self.userId) {
149+
if let showCoupon = try? optimizely.isFeatureEnabled(featureKey: "show_coupon", userId: userId) {
211150
variationViewController.showCoupon = showCoupon
212151
}
213152

214-
variationViewController.eventKey = eventKey
153+
variationViewController.optimizelyManager = optimizely
215154
variationViewController.userId = userId
216-
variationViewController.optimizelyManager = optimizelyManager
217155
variationViewController.variationKey = variationKey
218-
156+
variationViewController.eventKey = eventKey
157+
219158
window?.rootViewController = variationViewController
220159
}
221-
160+
222161
func openFailureView() {
223162
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "FailureViewController")
224163
}
225164

165+
// MARK: - AppDelegate
166+
226167
func applicationWillResignActive(_ application: UIApplication) {
227168
}
228169

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// CustomLogger
3+
// DemoSwiftiOS
4+
//
5+
// Created by Jae Kim on 1/11/19.
6+
// Copyright © 2019 Optimizely. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Optimizely
11+
12+
class CustomLogger: OPTLogger {
13+
14+
static var level:OptimizelyLogLevel?
15+
static var logLevel: OptimizelyLogLevel = .info
16+
17+
required init() {
18+
}
19+
20+
func log(level: OptimizelyLogLevel, message: String) {
21+
if level.rawValue <= CustomLogger.logLevel.rawValue {
22+
print("🐱 - [\(level.name)] Kitty - \(message)")
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)