Skip to content

Commit

Permalink
Demo App for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Nov 7, 2024
1 parent d2926be commit 8407647
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct ConfidenceDemoApp: App {
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)])
// .withContext(initialContext: ["targeting_key": ConfidenceValue(string: UUID.init().uuidString)])
.build()

Check failure on line 24 in ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.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)

let status = Status()
Expand All @@ -42,6 +42,6 @@ struct ConfidenceDemoApp: App {

extension ConfidenceDemoApp {
func setup(confidence: Confidence) async throws {
try await confidence.fetchAndActivate()
// try await confidence.fetchAndActivate()
}
}
78 changes: 74 additions & 4 deletions ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ struct ContentView: View {
@ObservedObject var status: Status
@StateObject var text = DisplayText()
@StateObject var color = FlagColor()
@State private var user = "logged out"
@State private var logginIn = false
@State private var evaluationReason = "Last Reason: ---"
@State private var evaluationError = "Last Error: ---"

private let confidence: Confidence

Expand All @@ -17,26 +21,88 @@ struct ContentView: View {
var body: some View {
if case .ready = status.state {
VStack {
Text("Current user:")
if (logginIn) {

Check failure on line 25 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)
ProgressView()
} else {
Text("\(user)")
.font(.title)
.bold()
}
Spacer()
Image(systemName: "flag")
.imageScale(.large)
.font(.title)
.foregroundColor(color.color)
.padding(10)
Text(text.text)
Spacer()
Button("Login yellow user") {
confidence.putContext(key: "user_id", value: ConfidenceValue.init(string: "user1"))
Task {
logginIn = true
try await confidence.fetchAndActivate()
user = "yellow_user"
logginIn = false
}
}
Button("Login green user") {
confidence.putContext(key: "user_id", value: ConfidenceValue.init(string: "user2"))
Task {
logginIn = true
try await confidence.fetchAndActivate()
logginIn = false
user = "green_user"
}
}
.padding(.bottom)
Button("Get remote flag value") {
let test = confidence.getEvaluation(key: "swift-demoapp.color", defaultValue: true)
print("\(test)")
text.text = confidence.getValue(key: "swift-demoapp.color", defaultValue: "ERROR")
let eval = confidence.getEvaluation(key: "swift-demoapp.color", defaultValue: "DefaultValue")
evaluationReason = "Last Reason: \(eval.reason)"
evaluationError = "Last Error: \(eval.errorCode?.description ?? .none ?? "None")"
text.text = eval.value
if text.text == "Green" {
color.color = .green
} else if text.text == "Yellow" {
color.color = .yellow
} else {
color.color = .red
color.color = .gray
}
}
VStack(alignment: .leading) {
HStack {
Text(evaluationReason)
.padding(.horizontal)
Spacer()
}
HStack {
Text(evaluationError)
.padding(.horizontal)
Spacer()
}
}
.padding(.bottom)
Button("Get remote flag value with TypeMismatch ⚠️") {
let eval = confidence.getEvaluation(key: "swift-demoapp.color", defaultValue: true)
evaluationReason = "Last Reason: \(eval.reason)"
evaluationError = "Last Error: \(eval.errorCode?.description ?? .none ?? "None")"
if text.text == "Green" {
color.color = .green
} else if text.text == "Yellow" {
color.color = .yellow
} else {
color.color = .gray
}
}
.padding(.bottom)
Button("Generate event") {
try! confidence.track(eventName: "Test", data: [:])

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Try Violation: Force tries should be avoided (force_try)
}
.padding(.top)
Button("Flush 🚽") {
confidence.flush()
}
.padding(.bottom)
}
.padding()
} else if case .error(let error) = status.state {
Expand All @@ -53,6 +119,10 @@ struct ContentView: View {
}
}

class Model: ObservableObject {

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace before Closing Braces Violation: Don't include vertical whitespace (empty line) before closing braces (vertical_whitespace_closing_braces)

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace after Opening Braces Violation: Don't include vertical whitespace (empty line) after opening braces (vertical_whitespace_opening_braces)
}

class DisplayText: ObservableObject {
@Published var text = "Hello World!"
}
Expand Down
17 changes: 16 additions & 1 deletion Sources/Confidence/FlagEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@ public struct Evaluation<T> {
public let errorMessage: String?
}

public enum ErrorCode {
public enum ErrorCode: CustomStringConvertible {
case providerNotReady
case invalidContext
case flagNotFound
case evaluationError
case typeMismatch

public var description: String {
switch self {
case .providerNotReady:
return "Provider is not ready."
case .invalidContext:
return "Invalid context."
case .flagNotFound:
return "Flag not found."
case .evaluationError:
return "Evaluation error occurred."
case .typeMismatch:
return "Type mismatch encountered."
}
}
}

struct FlagResolution: Encodable, Decodable, Equatable {
Expand Down
15 changes: 14 additions & 1 deletion Sources/Confidence/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,20 @@ extension NetworkClient {
}
}
// TMP - TESTING
print(">> \(request.allHTTPHeaderFields)")
if let headers = request.allHTTPHeaderFields, let metadata = headers["Confidence-Metadata"] {
if let data = metadata.data(using: .utf8) {
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
let prettyData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)

if let prettyPrintedString = String(data: prettyData, encoding: .utf8) {
print(prettyPrintedString)
}
} catch {
print("Failed to pretty print JSON: \(error)")
}
}
}

let jsonData = try encoder.encode(data)
request.httpBody = jsonData
Expand Down
Empty file.

0 comments on commit 8407647

Please sign in to comment.