-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureFlagsHelper.swift
64 lines (55 loc) · 2.38 KB
/
FeatureFlagsHelper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Foundation
import ff_ios_client_sdk
class FeatureFlagsHelper {
static var app: FeatureFlagsHelper = {
return FeatureFlagsHelper()
}()
private let apiKey = "enter client sdk key here"
func initClient(onCompletion:@escaping(Bool)->()) {
let configuration = CfConfiguration.builder().setStreamEnabled(true).build()
let target = CfTarget.builder().setName("ios-test").setIdentifier("ios-test").build()
CfClient.sharedInstance.initialize(apiKey: apiKey, configuration: configuration, target: target) { (result) in
switch result {
case .failure(let error):
print(error.errorData)
onCompletion(false)
case .success:
self.setupStream(){(result) in
onCompletion(true)
}
onCompletion(true)
}
}
}
func setupStream(onCompletion:@escaping(Bool)->()) {
CfClient.sharedInstance.registerEventsListener() { (result) in
switch result {
case .failure(let error):
print(error)
onCompletion(false)
case .success(let eventType):
switch eventType {
case .onPolling:
print("Event: Received all evaluation flags")
case .onEventListener(let evaluation):
print("Event: Received an evaluation flag, \(evaluation!)")
case .onComplete:
print("Event: SSE stream has completed")
case .onOpen:
print("Event: SSE stream has been opened")
case .onMessage(let messageObj):
print(messageObj?.event ?? "Event: Message received")
}
onCompletion(true)
}
}
}
func stringVariation(evaluationId: String, defaultValue: String, onCompletion:@escaping(String)->()) {
CfClient.sharedInstance.stringVariation(evaluationId: evaluationId, defaultValue: defaultValue) { (eval) in
onCompletion(eval!.value.stringValue!)
}
}
func destroy() {
CfClient.sharedInstance.destroy()
}
}