Skip to content

Commit e4889b7

Browse files
refactor
1 parent 8f91652 commit e4889b7

File tree

1 file changed

+26
-48
lines changed

1 file changed

+26
-48
lines changed

OptimizelySDK/Optimizely/OptimizelyManager.swift

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -716,80 +716,58 @@ extension OptimizelyManager {
716716
notifications = notificationCenter
717717
}
718718

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+
719738
func addActivateNotificationListener(activateListener: @escaping ([String : Any], String, [String : Any]?, [String : Any], Dictionary<String, Any>) -> Void) -> NSNumber? {
720739

721740
let num = notifications.addActivateNotificationListener { (experiment, userId, attributes, variation, event) in
722741

723-
let attrs = attributes?.mapValues({ (val) -> Any in
724-
if let val = val {
725-
return val
726-
}
727-
else {
728-
return NSNull()
729-
}
730-
})
731-
activateListener(experiment, userId, attrs, variation, event)
742+
activateListener(experiment, userId, self.convertAttribues(attributes: attributes), variation, event)
732743
}
733744

734-
if let num = num {
735-
return NSNumber(value: num)
736-
}
737-
738-
return nil
745+
return returnVal(num: num)
739746
}
740747

741748
func addTrackNotificationListener(trackListener: @escaping (String, String, [String : Any]?, Dictionary<String, Any>?, Dictionary<String, Any>) -> Void) -> NSNumber? {
742749
let num = notifications.addTrackNotificationListener { (eventKey, userId, attributes, eventTags, event) in
743750

744-
let attrs = attributes?.mapValues({ (val) -> Any in
745-
if let val = val {
746-
return val
747-
}
748-
else {
749-
return NSNull()
750-
}
751-
})
752-
753-
trackListener(eventKey, userId, attrs, eventTags, event)
754-
755-
}
756-
757-
if let num = num {
758-
return NSNumber(value: num)
751+
trackListener(eventKey, userId, self.convertAttribues(attributes: attributes), eventTags, event)
759752
}
760753

761-
return nil
754+
return returnVal(num: num)
762755
}
763756

764757
func addDecisionNotificationListener(decisionListener: @escaping (String, String, [String : Any]?, Dictionary<String, Any>) -> Void) -> NSNumber? {
765758

766759
let num = notifications.addDecisionNotificationListener { (type, userId, attributes, decisionInfo) in
767-
let attrs = attributes?.mapValues({ (val) -> Any in
768-
if let val = val {
769-
return val
770-
}
771-
else {
772-
return NSNull()
773-
}
774-
})
775-
decisionListener(type, userId, attrs, decisionInfo)
760+
decisionListener(type, userId, self.convertAttribues(attributes: attributes), decisionInfo)
776761
}
777-
if let num = num {
778-
return NSNumber(value: num)
779-
}
780-
781-
return nil
762+
return returnVal(num: num)
782763
}
783764

784765
func addDatafileChangeNotificationListener(datafileListener: @escaping (Data) -> Void) -> NSNumber? {
785766
let num = notifications.addDatafileChangeNotificationListener { (data) in
786767
datafileListener(data)
787768
}
788-
if let num = num {
789-
return NSNumber(value: num)
790-
}
791769

792-
return nil
770+
return returnVal(num: num)
793771
}
794772

795773
func removeNotificationListener(notificationId: Int) {

0 commit comments

Comments
 (0)