Skip to content

Commit

Permalink
feat: More DemoApp tests and cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Dec 2, 2024
1 parent 63d85b2 commit 5095331
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
37 changes: 26 additions & 11 deletions ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import Confidence
import SwiftUI

@main
struct ConfidenceDemoApp: App {
@AppStorage("loggedUser") private var loggedUser: String?
@AppStorage("appVersion") private var appVersion = 0

private let confidence: Confidence
private let flaggingState = ExperimentationFlags()
private let secret = ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "<Empty Secret>"

init() {
confidence = Confidence
.Builder(clientSecret: ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? "", loggerLevel: .TRACE)
.Builder(clientSecret: secret, loggerLevel: .TRACE)
.build()

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))
}
appVersion = 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)
}
confidence.putContextLocal(context: context, removeKeys: [])
updateConfidence()
}

var body: some Scene {
WindowGroup {
Text("Client secret: \(secret)")
.font(.caption)
if loggedUser == nil {
LoginView(confidence: confidence)
.environmentObject(flaggingState)
Expand All @@ -35,6 +36,20 @@ struct ConfidenceDemoApp: App {
}
}
}

private func updateConfidence() {
Task {
do {
flaggingState.state = .loading
try confidence.activate() // Activating the existing cache, in case the new fetch fails i.e. offline mode
try await Task.sleep(nanoseconds: 2 * 1_000_000_000)
try await confidence.fetchAndActivate()
flaggingState.state = .ready
} catch {
flaggingState.state = .error(ExperimentationFlags.CustomError(message: error.localizedDescription))
}
}
}
}

class ExperimentationFlags: ObservableObject {
Expand Down
53 changes: 45 additions & 8 deletions ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct ContentView: View {
@AppStorage("loggedUser") private var loggedUser: String?

Check failure on line 7 in ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Attributes Violation: Attributes with arguments or inside always_on_line_above must be on a new line instead of the same line (attributes)
@State var isLoggingOut: Bool = false
@State var loggedOut: Bool = false
@State var textColor: Color = .red

private let confidence: Confidence

Expand All @@ -19,21 +20,30 @@ struct ContentView: View {
if (status.state == .loading && !isLoggingOut) {

Check failure on line 20 in ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
VStack {
Spacer()
Text("Welcome \(loggedUser ?? "?")")
Text("Login successful: \(loggedUser ?? "?")")
Text("We are preparing your experience...")
ProgressView()
Spacer()
}
} else {
VStack {
Text("Hello World!")
.font(.largeTitle)
.foregroundStyle(ContentView.getColor(color: confidence.getValue(key: "swift-demoapp.color", defaultValue: "Gray")))
if (status.state == .ready) {

Check failure on line 28 in ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
VStack {
Spacer()
if let user = loggedUser {
let eval = confidence.getEvaluation(key: "swift-demoapp.color", defaultValue: "Gray")
Text("Hello \(user)")
.font(.largeTitle)
// Any change of flagState triggers a re-read of the value
// The "state = ready" wrapper makes sure only the reconciled value is shown at any time
.foregroundStyle(ContentView.getColor(color: eval.value))
.padding()
}
}
}
NavigationLink(destination: AboutPage()) {
Text("Navigate")
}
.padding()
Button("Logout") {
isLoggingOut = true

loggedUser = nil
status.state = .loading
Task {
Expand All @@ -46,6 +56,27 @@ struct ContentView: View {
.navigationDestination(isPresented: $loggedOut) {
LoginView(confidence: confidence)
}
Spacer()
}
ZStack {
VStack {
Spacer()
Text("This text doesn't wait for flags to be loaded")
.font(.caption)
.foregroundStyle(textColor) // This is set on onAppear and not changing, although it might show different values within the session if the user exit and re-enter ContentView (try tapping on Navigate and go Back)

Check failure on line 66 in ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 235 characters (line_length)
Text("This text dynamically change on flag re-load")
.font(.caption)
// Any change of flagState triggers a re-read of the value
.foregroundStyle(ContentView.getColor(color: confidence.getValue(key: "swift-demoapp.color", defaultValue: "Gray")))

Check failure on line 70 in ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 140 characters (line_length)
}
}.onAppear {
let eval = confidence.getEvaluation(key: "swift-demoapp.color", defaultValue: "Gray")
/*
Due to syntetic delay, the new context hasn't entered the SDK yet se we are operating with

Check failure on line 75 in ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
the last seen context before the view loads
*/
print(">> Evaluation reason: \(eval)")
textColor = ContentView.getColor(color: eval.value)
}
}
}
Expand All @@ -63,3 +94,9 @@ struct ContentView: View {
}
}
}

struct AboutPage: View {
var body: some View {
Text("About Page")
}
}
10 changes: 2 additions & 8 deletions ConfidenceDemoApp/ConfidenceDemoApp/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ struct LoginView: View {
status.state = .loading
// Load flags
Task {
try? await Task.sleep(nanoseconds: 5 * 1_000_000_000) // Sleep for 3 seconds
try? await Task.sleep(nanoseconds: 5 * 1_000_000_000)
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
try? await Task.sleep(nanoseconds: 1 * 1_000_000_000)
loggedUser = "user1"
loggingIn = false
loginCompleted = true
Expand All @@ -45,10 +45,4 @@ struct LoginView: View {
}
}
}
private var combinedBinding: Binding<Bool> {
Binding(
get: { loginCompleted && flagsLoaded },
set: { _ in } // No-op; navigation state is derived from other properties
)
}
}
4 changes: 0 additions & 4 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public class Confidence: ConfidenceEventSender {
}
let savedFlags = try storage.load(defaultValue: FlagResolution.EMPTY)
cache = savedFlags
debugLogger?.logMessage(
message: "[Activating stored cache]",
isWarning: false
)
}
}

Expand Down

0 comments on commit 5095331

Please sign in to comment.