-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: Context APIs changes and documentation/onboarding (#180)
release-as: 1.2.0
- Loading branch information
1 parent
5369e04
commit 6eb5dc7
Showing
18 changed files
with
926 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+57.3 KB
...DemoApp/ConfidenceDemoApp/Assets.xcassets/AppIcon.appiconset/ConfidenceLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
ConfidenceDemoApp/ConfidenceDemoApp/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 76 additions & 35 deletions
111
ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,91 @@ | ||
import Confidence | ||
import SwiftUI | ||
|
||
class Status: ObservableObject { | ||
enum State { | ||
case unknown | ||
case ready | ||
case error(Error?) | ||
} | ||
@main | ||
struct ConfidenceDemoApp: App { | ||
@AppStorage("loggedUser") | ||
private var loggedUser: String? | ||
@AppStorage("appVersion") | ||
private var appVersion = 0 | ||
|
||
@Published var state: State = .unknown | ||
} | ||
private let confidence: Confidence | ||
private let flaggingState = ExperimentationFlags() | ||
private let secret = ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "<Empty Secret>" | ||
|
||
init() { | ||
@AppStorage("appVersion") var appVersion = 0 | ||
@AppStorage("loggedUser") var loggedUser: String? | ||
appVersion += 1 // Simulate update of the app on every new run | ||
var context = ["app_version": ConfidenceValue.init(integer: appVersion)] | ||
if let user = loggedUser { | ||
context["user_id"] = ConfidenceValue.init(string: user) | ||
} | ||
|
||
@main | ||
struct ConfidenceDemoApp: App { | ||
@StateObject private var lifecycleObserver = ConfidenceAppLifecycleProducer() | ||
confidence = Confidence | ||
.Builder(clientSecret: secret, loggerLevel: .TRACE) | ||
.withContext(initialContext: context) | ||
.build() | ||
do { | ||
// NOTE: here we are activating all the flag values from storage, regardless of how `context` looks now | ||
try confidence.activate() | ||
} catch { | ||
flaggingState.state = .error(ExperimentationFlags.CustomError(message: error.localizedDescription)) | ||
} | ||
// flaggingState.color is set here at startup and remains immutable until a user logs out | ||
let eval = confidence.getEvaluation( | ||
key: "swift-demoapp.color", | ||
defaultValue: "Gray") | ||
flaggingState.color = ContentView.getColor( | ||
color: eval.value) | ||
flaggingState.reason = eval.reason | ||
|
||
self.appVersion = appVersion | ||
self.loggedUser = loggedUser | ||
updateConfidence() | ||
} | ||
|
||
var body: some Scene { | ||
WindowGroup { | ||
let secret = ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "" | ||
let confidence = Confidence.Builder(clientSecret: secret, loggerLevel: .TRACE) | ||
.withContext(initialContext: [ | ||
"targeting_key": ConfidenceValue(string: UUID.init().uuidString), | ||
"user_id": .init(string: "user2") | ||
]) | ||
.build() | ||
|
||
let status = Status() | ||
|
||
ContentView(confidence: confidence, status: status) | ||
.task { | ||
do { | ||
confidence.track(producer: lifecycleObserver) | ||
try await self.setup(confidence: confidence) | ||
status.state = .ready | ||
} catch { | ||
status.state = .error(error) | ||
print(error.localizedDescription) | ||
} | ||
} | ||
if loggedUser == nil { | ||
LoginView(confidence: confidence) | ||
.environmentObject(flaggingState) | ||
} else { | ||
ContentView(confidence: confidence) | ||
.environmentObject(flaggingState) | ||
} | ||
} | ||
} | ||
|
||
private func updateConfidence() { | ||
Task { | ||
do { | ||
flaggingState.state = .loading | ||
try await Task.sleep(nanoseconds: 2 * 1_000_000_000) // simulating slow network | ||
// The flags in storage are refreshed for the current `context`, and activated | ||
// After this line, fresh (and potentially new) flags values can be accessed | ||
try await confidence.fetchAndActivate() | ||
flaggingState.state = .ready | ||
} catch { | ||
flaggingState.state = .error(ExperimentationFlags.CustomError(message: error.localizedDescription)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension ConfidenceDemoApp { | ||
func setup(confidence: Confidence) async throws { | ||
try await confidence.fetchAndActivate() | ||
class ExperimentationFlags: ObservableObject { | ||
var color: Color = .red // This is set on applicaaton start, and reset on user logout | ||
var reason: ResolveReason = .unknown | ||
@Published var state: State = .notReady | ||
|
||
enum State: Equatable { | ||
case unknown | ||
case notReady | ||
case loading | ||
case ready | ||
case error(CustomError?) | ||
} | ||
|
||
public struct CustomError: Error, Equatable { | ||
let message: String | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.