-
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.
feat: Demo App emulates login scenarios
- Loading branch information
1 parent
e2a2476
commit 37c25de
Showing
6 changed files
with
142 additions
and
82 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
75 changes: 39 additions & 36 deletions
75
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,53 @@ | ||
import Confidence | ||
import SwiftUI | ||
|
||
class Status: ObservableObject { | ||
enum State { | ||
case unknown | ||
case ready | ||
case error(Error?) | ||
} | ||
|
||
@Published var state: State = .unknown | ||
} | ||
|
||
|
||
@main | ||
struct ConfidenceDemoApp: App { | ||
@StateObject private var lifecycleObserver = ConfidenceAppLifecycleProducer() | ||
@AppStorage("loggedUser") private var loggedUser: String? | ||
// @StateObject | ||
private var flaggingState = ExperimentationFlags() | ||
private let confidence = Confidence | ||
.Builder(clientSecret: ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "", loggerLevel: .TRACE) | ||
.build() | ||
|
||
init() { | ||
if let loggedUser = loggedUser { | ||
confidence.putContextLocal(context: ["user_id": ConfidenceValue.init(string: loggedUser)], removeKeys: []) | ||
} | ||
do { | ||
try confidence.activate() | ||
flaggingState.state = .ready | ||
} catch { | ||
print("Error during activation of Confidence") | ||
flaggingState.state = .error(ExperimentationFlags.CustomError(message: error.localizedDescription)) | ||
} | ||
} | ||
|
||
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) { | ||
Check failure on line 28 in ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift GitHub Actions / SwiftLint
|
||
LoginView(confidence: confidence) | ||
.environmentObject(flaggingState) | ||
} else { | ||
ContentView(confidence: confidence) | ||
.environmentObject(flaggingState) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension ConfidenceDemoApp { | ||
func setup(confidence: Confidence) async throws { | ||
try await confidence.fetchAndActivate() | ||
class ExperimentationFlags: ObservableObject { | ||
@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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import SwiftUI | ||
import Confidence | ||
|
||
struct LoginView: View { | ||
@EnvironmentObject var status: ExperimentationFlags | ||
@AppStorage("loggedUser") private var loggedUser: String? | ||
@State private var loginCompleted = false | ||
@State private var flagsLoaded = false | ||
@State private var loggingIn = false | ||
|
||
private let confidence: Confidence | ||
|
||
init(confidence: Confidence) { | ||
self.confidence = confidence | ||
} | ||
|
||
var body: some View { | ||
NavigationStack { | ||
VStack { | ||
Spacer() | ||
Button("Login as user1") { | ||
status.state = .loading | ||
// Load flags | ||
Task { | ||
try? await Task.sleep(nanoseconds: 5 * 1_000_000_000) // Sleep for 3 seconds | ||
await confidence.putContext(context: ["user_id": .init(string: "user1")]) | ||
status.state = .ready | ||
} | ||
// Load everything else for this user | ||
Task { | ||
loggingIn = true | ||
try? await Task.sleep(nanoseconds: 1 * 1_000_000_000) // Sleep for 3 seconds | ||
loggedUser = "user1" | ||
loggingIn = false | ||
loginCompleted = true | ||
} | ||
} | ||
.navigationDestination(isPresented: $loginCompleted) { | ||
ContentView(confidence: confidence) | ||
} | ||
if (loggingIn) { | ||
Check failure on line 41 in ConfidenceDemoApp/ConfidenceDemoApp/LoginView.swift GitHub Actions / SwiftLint
|
||
ProgressView() | ||
} | ||
Spacer() | ||
} | ||
} | ||
} | ||
private var combinedBinding: Binding<Bool> { | ||
Binding( | ||
get: { loginCompleted && flagsLoaded }, | ||
set: { _ in } // No-op; navigation state is derived from other properties | ||
) | ||
} | ||
} |
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