Skip to content

Commit 42a85f9

Browse files
authored
Merge pull request #149 from optimizely/objcNotificationCenter
(feat):take a shot at notification center for objective-c
2 parents 400e49d + 53b7057 commit 42a85f9

File tree

3 files changed

+155
-7
lines changed

3 files changed

+155
-7
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3434
self.userId = [NSString stringWithFormat:@"%d", arc4random()];
3535
self.attributes = @{ @"browser_type": @"safari" };
3636

37+
3738
// initialize SDK in one of these two ways:
3839
// (1) asynchronous SDK initialization (RECOMMENDED)
3940
// - fetch a JSON datafile from the server
@@ -59,6 +60,10 @@ -(void)initializeOptimizelySDKAsynchronous {
5960
self.optimizely = nil;
6061
}
6162

63+
[self.optimizely.notificationCenter addActivateNotificationListenerWithActivateListener:^(NSDictionary<NSString *,id> * _Nonnull experiment, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull variation, NSDictionary<NSString *,id> * _Nonnull event) {
64+
NSLog(@"got activate with experiment");
65+
NSLog(@"%@", experiment[@"key"]);
66+
}];
6267
[self startAppWithExperimentActivated];
6368
}];
6469
}

OptimizelySDK/Optimizely/OptimizelyManager.swift

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ open class OptimizelyManager: NSObject {
4242
public var notificationCenter: OPTNotificationCenter {
4343
return HandlerRegistryService.shared.injectNotificationCenter(sdkKey: self.sdkKey)!
4444
}
45-
45+
4646
private let reInitLock = Dispatch.DispatchSemaphore(value: 1)
4747

4848
// MARK: - Public interfaces
@@ -671,10 +671,8 @@ open class OptimizelyManager: NSObject {
671671

672672
}
673673

674-
// MARK: - ObjC APIs
675-
676674
extension OptimizelyManager {
677-
675+
@available(swift, obsoleted: 1.0)
678676
@objc public convenience init(sdkKey: String) {
679677
self.init(sdkKey: sdkKey,
680678
logger: nil,
@@ -696,6 +694,7 @@ extension OptimizelyManager {
696694

697695
}
698696

697+
@available(swift, obsoleted: 1.0)
699698
@objc(initializeSDKWithCompletion:)
700699
public func _objcInitializeSDK(completion: ((Data?, NSError?) -> Void)?) {
701700
initializeSDK { result in
@@ -707,24 +706,110 @@ extension OptimizelyManager {
707706
}
708707
}
709708
}
709+
710+
@available(swift, obsoleted: 1.0)
711+
@objc(notificationCenter) public var objc_notificationCenter: _ObjcOPTNotificationCenter {
712+
class ObjcCenter : _ObjcOPTNotificationCenter {
713+
var notifications:OPTNotificationCenter
714+
715+
init(notificationCenter:OPTNotificationCenter) {
716+
notifications = notificationCenter
717+
}
718+
719+
internal func convertAttribues(attributes:OptimizelyAttributes?) -> [String:Any]? {
720+
return attributes?.mapValues({ (val) -> Any in
721+
if let val = val {
722+
return val
723+
}
724+
else {
725+
return NSNull()
726+
}
727+
})
728+
}
729+
730+
internal func returnVal(num:Int?) -> NSNumber? {
731+
if let num = num {
732+
return NSNumber(value: num)
733+
}
734+
735+
return nil
736+
}
737+
738+
func addActivateNotificationListener(activateListener: @escaping ([String : Any], String, [String : Any]?, [String : Any], Dictionary<String, Any>) -> Void) -> NSNumber? {
739+
740+
let num = notifications.addActivateNotificationListener { (experiment, userId, attributes, variation, event) in
741+
742+
activateListener(experiment, userId, self.convertAttribues(attributes: attributes), variation, event)
743+
}
744+
745+
return returnVal(num: num)
746+
}
747+
748+
func addTrackNotificationListener(trackListener: @escaping (String, String, [String : Any]?, Dictionary<String, Any>?, Dictionary<String, Any>) -> Void) -> NSNumber? {
749+
let num = notifications.addTrackNotificationListener { (eventKey, userId, attributes, eventTags, event) in
750+
751+
trackListener(eventKey, userId, self.convertAttribues(attributes: attributes), eventTags, event)
752+
}
753+
754+
return returnVal(num: num)
755+
}
756+
757+
func addDecisionNotificationListener(decisionListener: @escaping (String, String, [String : Any]?, Dictionary<String, Any>) -> Void) -> NSNumber? {
758+
759+
let num = notifications.addDecisionNotificationListener { (type, userId, attributes, decisionInfo) in
760+
decisionListener(type, userId, self.convertAttribues(attributes: attributes), decisionInfo)
761+
}
762+
return returnVal(num: num)
763+
}
764+
765+
func addDatafileChangeNotificationListener(datafileListener: @escaping (Data) -> Void) -> NSNumber? {
766+
let num = notifications.addDatafileChangeNotificationListener { (data) in
767+
datafileListener(data)
768+
}
769+
770+
return returnVal(num: num)
771+
}
772+
773+
func removeNotificationListener(notificationId: Int) {
774+
notifications.removeNotificationListener(notificationId: notificationId)
775+
}
776+
777+
func clearNotificationListeners(type: NotificationType) {
778+
notifications.clearNotificationListeners(type: type)
779+
}
780+
781+
func clearAllNotificationListeners() {
782+
notifications.clearAllNotificationListeners()
783+
}
784+
785+
786+
}
787+
788+
return ObjcCenter(notificationCenter: self.notificationCenter)
789+
}
710790

791+
792+
@available(swift, obsoleted: 1.0)
711793
@objc(initializeSDKWithDatafile:error:)
712794
public func _objcInitializeSDKWith(datafile:String) throws {
713795
try self.initializeSDK(datafile: datafile)
714796
}
715797

798+
@available(swift, obsoleted: 1.0)
716799
@objc(initializeSDKWithDatafile:doFetchDatafileBackground:error:)
717800
public func _objcInitializeSDK(datafile: Data, doFetchDatafileBackground: Bool = true) throws {
718801
try self.initializeSDK(datafile: datafile, doFetchDatafileBackground: doFetchDatafileBackground)
719802
}
720803

804+
@available(swift, obsoleted: 1.0)
721805
@objc(activateWithExperimentKey:userId:attributes:error:)
722806
public func _objcActivate(experimentKey: String,
723807
userId: String,
724808
attributes: [String:Any]?) throws -> String {
725809
return try self.activate(experimentKey: experimentKey, userId: userId, attributes: attributes as OptimizelyAttributes?)
726810
}
727811

812+
@available(swift, obsoleted: 1.0)
728813
@objc(getVariationKeyWithExperimentKey:userId:attributes:error:)
729814
public func _objcGetVariationKey(experimentKey: String,
730815
userId: String,
@@ -734,12 +819,14 @@ extension OptimizelyManager {
734819
attributes: attributes)
735820
}
736821

822+
@available(swift, obsoleted: 1.0)
737823
@objc(getForcedVariationWithExperimentKey:userId:)
738824
public func _objcGetForcedVariation(experimentKey: String, userId: String) -> String? {
739825
return getForcedVariation(experimentKey: experimentKey,
740826
userId: userId)
741827
}
742828

829+
@available(swift, obsoleted: 1.0)
743830
@objc(setForcedVariationWithExperimentKey:userId:variationKey:)
744831
public func _objcSetForcedVariation(experimentKey: String,
745832
userId: String,
@@ -749,6 +836,7 @@ extension OptimizelyManager {
749836
variationKey: variationKey)
750837
}
751838

839+
@available(swift, obsoleted: 1.0)
752840
@objc(isFeatureEnabledWithFeatureKey:userId:attributes:error:)
753841
public func _objcIsFeatureEnabled(featureKey: String,
754842
userId: String,
@@ -759,6 +847,8 @@ extension OptimizelyManager {
759847
return NSNumber(booleanLiteral: enabled)
760848
}
761849

850+
851+
@available(swift, obsoleted: 1.0)
762852
@objc(getFeatureVariableBooleanWithFeatureKey:variableKey:userId:attributes:error:)
763853
public func _objcGetFeatureVariableBoolean(featureKey: String,
764854
variableKey: String,
@@ -771,6 +861,7 @@ extension OptimizelyManager {
771861
return NSNumber(booleanLiteral: value)
772862
}
773863

864+
@available(swift, obsoleted: 1.0)
774865
@objc(getFeatureVariableDoubleWithFeatureKey:variableKey:userId:attributes:error:)
775866
public func _objcGetFeatureVariableDouble(featureKey: String,
776867
variableKey: String,
@@ -783,6 +874,7 @@ extension OptimizelyManager {
783874
return NSNumber(value: value)
784875
}
785876

877+
@available(swift, obsoleted: 1.0)
786878
@objc(getFeatureVariableIntegerWithFeatureKey:variableKey:userId:attributes:error:)
787879
public func _objcGetFeatureVariableInteger(featureKey: String,
788880
variableKey: String,
@@ -795,6 +887,7 @@ extension OptimizelyManager {
795887
return NSNumber(integerLiteral: value)
796888
}
797889

890+
@available(swift, obsoleted: 1.0)
798891
@objc(getFeatureVariableStringWithFeatureKey:variableKey:userId:attributes:error:)
799892
public func _objcGetFeatureVariableString(featureKey: String,
800893
variableKey: String,
@@ -806,12 +899,14 @@ extension OptimizelyManager {
806899
attributes: attributes)
807900
}
808901

902+
@available(swift, obsoleted: 1.0)
809903
@objc(getEnabledFeaturesWithUserId:attributes:error:)
810904
public func _objcGetEnabledFeatures(userId: String,
811905
attributes: [String: Any]?) throws -> [String] {
812906
return try self.getEnabledFeatures(userId:userId, attributes: attributes)
813907
}
814908

909+
@available(swift, obsoleted: 1.0)
815910
@objc(trackWithEventKey:userId:attributes:eventTags:error:)
816911
public func _objcTrack(eventKey:String,
817912
userId: String,
@@ -881,7 +976,6 @@ extension OptimizelyManager {
881976
}
882977

883978
// MARK: - ObjC protocols
884-
885979
@objc(OPTEventDispatcher) public protocol _ObjcOPTEventDispatcher {
886980
func dispatchEvent(event:EventForDispatch, completionHandler:((Data?, NSError?) -> Void)?)
887981

OptimizelySDK/Protocols/OPTNotificationCenter.swift

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import Foundation
1818

1919
/// Enum representing notification types.
20-
public enum NotificationType : Int {
20+
@objc public enum NotificationType : Int {
2121
case Activate = 1
2222
case Track
2323
case DatafileChange
@@ -76,7 +76,7 @@ func addDecisionNotificationListener(decisionListener:@escaping DecisionListener
7676

7777
/**
7878
Add a datafile change notification listener
79-
- Parameter datafileChangeListener: Notification to add.
79+
- Parameter datafileListener: Notification to add.
8080
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
8181
*/
8282
func addDatafileChangeNotificationListener(datafileListener:@escaping DatafileChangeListener) -> Int?
@@ -109,3 +109,52 @@ func sendNotifications(type:Int, args:Array<Any?>)
109109

110110
}
111111

112+
@objc(OPTNotificationCenter) public protocol _ObjcOPTNotificationCenter {
113+
/**
114+
Add an activate notification listener to the notification center.
115+
- Parameter activateListener: Notification to add.
116+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
117+
*/
118+
func addActivateNotificationListener(activateListener:@escaping (_ experiment:[String:Any], _ userId:String, _ attributes: [String:Any]?, _ variation:[String:Any], _ event:Dictionary<String, Any>) -> Void) -> NSNumber?
119+
120+
/**
121+
Add a track notification listener to the notification center.
122+
- Parameter trackListener: Notification to add.
123+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
124+
*/
125+
func addTrackNotificationListener(trackListener:@escaping (_ eventKey:String, _ userId:String, _ attributes: [String:Any]?, _ eventTags:Dictionary<String, Any>?, _ event:Dictionary<String, Any>) -> Void) -> NSNumber?
126+
127+
/**
128+
Add a decision notification listener to the notification center.
129+
- Parameter decisionListener: Notification to add.
130+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
131+
*/
132+
func addDecisionNotificationListener(decisionListener:@escaping (_ type:String, _ userId:String, _ attributes: [String:Any]?, _ decisionInfo:Dictionary<String, Any>) -> Void) -> NSNumber?
133+
134+
/**
135+
Add a datafile change notification listener
136+
- Parameter datafileListener: Notification to add.
137+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
138+
*/
139+
func addDatafileChangeNotificationListener(datafileListener:@escaping (_ datafile:Data) -> Void) -> NSNumber?
140+
141+
/**
142+
Remove the notification listener based on the notificationId passed back from addNotification.
143+
- Parameter notificationId: the id passed back from add notification.
144+
- Returns: true if removed otherwise false (if the notification is already removed, it returns false).
145+
*/
146+
func removeNotificationListener(notificationId:Int)
147+
148+
/**
149+
Clear notification listeners by notification type.
150+
- Parameter type: type of OPTLYNotificationType to remove.
151+
*/
152+
func clearNotificationListeners(type:NotificationType)
153+
154+
/**
155+
* Clear out all the notification listeners.
156+
*/
157+
func clearAllNotificationListeners()
158+
159+
}
160+

0 commit comments

Comments
 (0)