Skip to content

Commit

Permalink
Merge pull request #4 from gandalf-network/ft/ts-add-options-parameter
Browse files Browse the repository at this point in the history
Add Options Parameter
  • Loading branch information
TosinJs authored Jul 4, 2024
2 parents 9c3a70a + 4876246 commit ac033fc
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,22 @@ let services: InputData = [
"netflix": .service(Service(activities: ["watch"])),
]

<!-- styling parameters to modify the Connect UI -->
let style = StylingOptions(
primaryColor: "#7949D1",
backgroundColor: "#fff",
foregroundColor: "#562BA6",
accentColor: "#F4F0FB",
)

let options = ConnectOptions(style: style)

let input = ConnectInput(
publicKey: "yourPublicKey",
redirectURL: "https://example.com",
services: services
services: services,
// Optional styling parameter to modify the Connect UI
options: options
)
```

Expand Down
5 changes: 4 additions & 1 deletion Sources/Example/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ func testConnect() async {
"uber": .service(Service(traits: ["rating"], activities: ["trip"], required: false)),
"netflix": .service(Service(activities: ["watch"])),
]
let style = StylingOptions(primaryColor: "#7949D1", backgroundColor: "#fff000", foregroundColor: "#562BA6", accentColor: "#F4F0FB")
let connectOptions = ConnectOptions(style: style)
let input = ConnectInput(
publicKey: "0x02073d3b9daf439c19a267dcfc19bc1ac1aea5066d8c754554b046476099b6fa22",
redirectURL: "https://gandalf.network",
services: services
services: services,
options: connectOptions
)

let connect = Connect(input: input)
Expand Down
53 changes: 51 additions & 2 deletions Sources/GandalfConnect/GandalfConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ public struct GandalfError: Error {
public let code: GandalfErrorCode
}

public struct StylingOptions {
public var primaryColor: String?
public var backgroundColor: String?
public var foregroundColor: String?
public var accentColor: String?

public init(primaryColor: String? = nil, backgroundColor: String? = nil, foregroundColor: String? = nil, accentColor: String? = nil) {
self.primaryColor = primaryColor
self.backgroundColor = backgroundColor
self.foregroundColor = foregroundColor
self.accentColor = accentColor
}
}

public struct ConnectOptions {
public var style: StylingOptions

public init(style: StylingOptions) {
self.style = style
}
}

public struct Service {
public var traits: [String]?
public var activities: [String]?
Expand All @@ -35,11 +57,13 @@ public struct ConnectInput {
public var publicKey: String
public var redirectURL: String
public var services: InputData
public var options: ConnectOptions? = nil

public init(publicKey: String, redirectURL: String, services: InputData) {
public init(publicKey: String, redirectURL: String, services: InputData, options: ConnectOptions? = nil) {
self.publicKey = publicKey
self.redirectURL = redirectURL
self.services = services
self.options = options
}
}

Expand All @@ -48,18 +72,26 @@ public class Connect {
public var redirectURL: String
public var data: InputData
public var verificationComplete: Bool = false
public var options: ConnectOptions? = nil

public init(input: ConnectInput) {
self.publicKey = input.publicKey
self.redirectURL = input.redirectURL.hasSuffix("/") ? String(input.redirectURL.dropLast()) : input.redirectURL
self.data = input.services
self.options = input.options
}

public func generateURL() async throws -> String {
let inputData = data
try await allValidations(publicKey: publicKey, redirectURL: redirectURL, data: inputData)

var inputDataDictionary = self.dataToDictionary(inputData)

if let style = options?.style {
inputDataDictionary["options"] = styleToDictionary(style)
}

let jsonData = try JSONSerialization.data(withJSONObject: self.dataToDictionary(self.data))
let jsonData = try JSONSerialization.data(withJSONObject: inputDataDictionary)
let dataString = String(data: jsonData, encoding: .utf8) ?? ""
print(jsonData)

Expand All @@ -83,6 +115,23 @@ public class Connect {
}
return dictionary
}

private func styleToDictionary(_ style: StylingOptions) -> [String: Any] {
var dictionary: [String: Any] = [:]
if let primaryColor = style.primaryColor {
dictionary["primaryColor"] = primaryColor
}
if let backgroundColor = style.backgroundColor {
dictionary["backgroundColor"] = backgroundColor
}
if let foregroundColor = style.foregroundColor {
dictionary["foregroundColor"] = foregroundColor
}
if let accentColor = style.accentColor {
dictionary["accentColor"] = accentColor
}
return dictionary
}

private static func getSupportedServicesAndTraits() async throws -> SupportedServicesAndTraits {
return try await withCheckedThrowingContinuation { continuation in
Expand Down
17 changes: 17 additions & 0 deletions Tests/GandalfConnectTests/GandalfConnectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class ConnectTests: XCTestCase {
"netflix": .service(Service(traits: ["plan"], activities: ["watch"], required: false)),
"instacart": .service(Service(traits: [], activities: ["shop"], required: false))
]
let styling = StylingOptions(primaryColor: "#7949D1", backgroundColor: "#fff000", foregroundColor: "#562BA6", accentColor: "#F4F0FB")

func testInitialization() {
let input = ConnectInput(publicKey: publicKey, redirectURL: redirectURL, services: services)
Expand Down Expand Up @@ -99,6 +100,22 @@ final class ConnectTests: XCTestCase {
}
}

func testGenerateURLWithStylingOptions() async {
let connectOptions = ConnectOptions(style: styling)
let input = ConnectInput(publicKey: publicKey, redirectURL: redirectURL, services: multipleServices, options: connectOptions)
let connect = Connect(input: input)

do {
let generatedURL = try await connect.generateURL()
XCTAssertTrue(generatedURL.contains(publicKey))
XCTAssertTrue(generatedURL.contains(redirectURL))
} catch let error as GandalfError {
XCTAssertEqual(error.code, .InvalidService)
} catch {
XCTFail("Unexpected error type: \(type(of: error))")
}
}

func testGetDataKeyFromURL() {
let url = "https://example.com?dataKey=testDataKey"
do {
Expand Down

0 comments on commit ac033fc

Please sign in to comment.