-
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 0d9ae04
Showing
6 changed files
with
160 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
93 changes: 57 additions & 36 deletions
93
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,71 @@ | ||
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? | ||
var body: some Scene { | ||
WindowGroup { | ||
if loggedUser == nil { | ||
LoginContainerView() | ||
} else { | ||
ContentContainerView(loggedUser: loggedUser!) | ||
} | ||
} | ||
} | ||
|
||
@Published var state: State = .unknown | ||
} | ||
|
||
struct LoginContainerView: View { | ||
var body: some View { | ||
let confidence = Confidence | ||
.Builder(clientSecret: ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "", loggerLevel: .TRACE) | ||
.build() | ||
let flaggingState = ExperimentationFlags() | ||
|
||
@main | ||
struct ConfidenceDemoApp: App { | ||
@StateObject private var lifecycleObserver = ConfidenceAppLifecycleProducer() | ||
LoginView(confidence: confidence) | ||
.environmentObject(flaggingState) | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} | ||
struct ContentContainerView: View { | ||
let loggedUser: String | ||
let confidence = Confidence | ||
.Builder(clientSecret: ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "", loggerLevel: .TRACE) | ||
.build() | ||
let flaggingState = ExperimentationFlags() | ||
|
||
init(loggedUser: String) { | ||
self.loggedUser = loggedUser | ||
// Activate confidence | ||
do { | ||
confidence.putContextLocal(context: ["user_id": ConfidenceValue.init(string: loggedUser)], removeKeys: []) | ||
try confidence.activate() | ||
flaggingState.state = .ready | ||
} catch { | ||
print("Error during activation of Confidence") | ||
flaggingState.state = .error(ExperimentationFlags.CustomError(message: error.localizedDescription)) | ||
} | ||
} | ||
|
||
var body: some View { | ||
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