From 1432cc4e70b6b9cd7aadc0ea1c7911c4ae7bc60c Mon Sep 17 00:00:00 2001 From: tosinjs Date: Wed, 3 Jul 2024 23:05:21 +0100 Subject: [PATCH 1/9] Add Options Parameter --- Package.resolved | 9 ++++ Sources/GandalfConnect/GandalfConnect.swift | 42 ++++++++++++++++++- .../GandalfConnectTests.swift | 17 ++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/Package.resolved b/Package.resolved index 11ded11..c6e0f7d 100644 --- a/Package.resolved +++ b/Package.resolved @@ -9,6 +9,15 @@ "version" : "1.12.2" } }, + { + "identity" : "csqlite", + "kind" : "remoteSourceControl", + "location" : "https://github.com/stephencelis/CSQLite.git", + "state" : { + "revision" : "9106e983d5e3d5149ee35281ec089484b0def018", + "version" : "0.0.3" + } + }, { "identity" : "gandalf-ios-apollo-api", "kind" : "remoteSourceControl", diff --git a/Sources/GandalfConnect/GandalfConnect.swift b/Sources/GandalfConnect/GandalfConnect.swift index 717a393..04888e6 100644 --- a/Sources/GandalfConnect/GandalfConnect.swift +++ b/Sources/GandalfConnect/GandalfConnect.swift @@ -12,6 +12,17 @@ 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 struct ConnectOptions { + public var style: StylingOptions +} + public struct Service { public var traits: [String]? public var activities: [String]? @@ -35,11 +46,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) { self.publicKey = publicKey self.redirectURL = redirectURL self.services = services + self.options = options } } @@ -48,18 +61,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) + + let inputDataDictionary = self.dataToDictionary(inputData) + + if let style = options?.style { + inputDataDictionary.merge(self.styleToDictionary(style)) { (current, _) in current } + } - 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) @@ -83,6 +104,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 diff --git a/Tests/GandalfConnectTests/GandalfConnectTests.swift b/Tests/GandalfConnectTests/GandalfConnectTests.swift index fc74726..2c77878 100644 --- a/Tests/GandalfConnectTests/GandalfConnectTests.swift +++ b/Tests/GandalfConnectTests/GandalfConnectTests.swift @@ -18,6 +18,8 @@ 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") + let connectOptions = ConnectOptions(style: styling) func testInitialization() { let input = ConnectInput(publicKey: publicKey, redirectURL: redirectURL, services: services) @@ -99,6 +101,21 @@ final class ConnectTests: XCTestCase { } } + func testGenerateURLWithStylingOptions() async { + 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 { From f603dfde429fb876c9aa42ec19b5b06c30ad654d Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 11:16:37 +0100 Subject: [PATCH 2/9] Fix Test --- Sources/GandalfConnect/GandalfConnect.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/GandalfConnect/GandalfConnect.swift b/Sources/GandalfConnect/GandalfConnect.swift index 04888e6..27c2daa 100644 --- a/Sources/GandalfConnect/GandalfConnect.swift +++ b/Sources/GandalfConnect/GandalfConnect.swift @@ -74,7 +74,7 @@ public class Connect { let inputData = data try await allValidations(publicKey: publicKey, redirectURL: redirectURL, data: inputData) - let inputDataDictionary = self.dataToDictionary(inputData) + var inputDataDictionary = self.dataToDictionary(inputData) if let style = options?.style { inputDataDictionary.merge(self.styleToDictionary(style)) { (current, _) in current } From 75d18e9cb4b7e62055bcc34f66db7cec17f58616 Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 11:21:49 +0100 Subject: [PATCH 3/9] Update --- Sources/GandalfConnect/GandalfConnect.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/GandalfConnect/GandalfConnect.swift b/Sources/GandalfConnect/GandalfConnect.swift index 27c2daa..1e55b54 100644 --- a/Sources/GandalfConnect/GandalfConnect.swift +++ b/Sources/GandalfConnect/GandalfConnect.swift @@ -48,7 +48,7 @@ public struct ConnectInput { public var services: InputData public var options: ConnectOptions? = nil - public init(publicKey: String, redirectURL: String, services: InputData, options: ConnectOptions) { + public init(publicKey: String, redirectURL: String, services: InputData, options: ConnectOptions? = nil) { self.publicKey = publicKey self.redirectURL = redirectURL self.services = services From 5ff998528cd5a89e7413faa7129a323a5f98a0a0 Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 11:38:29 +0100 Subject: [PATCH 4/9] Update --- Sources/GandalfConnect/GandalfConnect.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sources/GandalfConnect/GandalfConnect.swift b/Sources/GandalfConnect/GandalfConnect.swift index 1e55b54..d982777 100644 --- a/Sources/GandalfConnect/GandalfConnect.swift +++ b/Sources/GandalfConnect/GandalfConnect.swift @@ -17,10 +17,21 @@ public struct StylingOptions { 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 { From 2fe1a7f85b373346ef3d7f5d0c635f6c372d57db Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 11:47:05 +0100 Subject: [PATCH 5/9] Fix Tests --- Tests/GandalfConnectTests/GandalfConnectTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/GandalfConnectTests/GandalfConnectTests.swift b/Tests/GandalfConnectTests/GandalfConnectTests.swift index 2c77878..33a8096 100644 --- a/Tests/GandalfConnectTests/GandalfConnectTests.swift +++ b/Tests/GandalfConnectTests/GandalfConnectTests.swift @@ -19,7 +19,6 @@ final class ConnectTests: XCTestCase { "instacart": .service(Service(traits: [], activities: ["shop"], required: false)) ] let styling = StylingOptions(primaryColor: "#7949D1", backgroundColor: "#fff000", foregroundColor: "#562BA6", accentColor: "#F4F0FB") - let connectOptions = ConnectOptions(style: styling) func testInitialization() { let input = ConnectInput(publicKey: publicKey, redirectURL: redirectURL, services: services) @@ -102,6 +101,7 @@ 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) From 6f9034cc16ebfee77ee3f8589fa69d3cb4c5c6a4 Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 12:17:32 +0100 Subject: [PATCH 6/9] Update --- Sources/GandalfConnect/GandalfConnect.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/GandalfConnect/GandalfConnect.swift b/Sources/GandalfConnect/GandalfConnect.swift index d982777..f0dce30 100644 --- a/Sources/GandalfConnect/GandalfConnect.swift +++ b/Sources/GandalfConnect/GandalfConnect.swift @@ -88,7 +88,7 @@ public class Connect { var inputDataDictionary = self.dataToDictionary(inputData) if let style = options?.style { - inputDataDictionary.merge(self.styleToDictionary(style)) { (current, _) in current } + inputDataDictionary["options"] = styleToDictionary(style) } let jsonData = try JSONSerialization.data(withJSONObject: inputDataDictionary) From c76f949d98d96647cbf19b2e7ebac1176a94ab51 Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 12:52:13 +0100 Subject: [PATCH 7/9] Update Example --- Sources/Example/Example.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/Example/Example.swift b/Sources/Example/Example.swift index b063bb9..228e5e6 100644 --- a/Sources/Example/Example.swift +++ b/Sources/Example/Example.swift @@ -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) From 53f7cb0f5ff3d7ddaf461667580d997535f8c7ce Mon Sep 17 00:00:00 2001 From: Samaila Bala Date: Thu, 4 Jul 2024 12:56:53 +0100 Subject: [PATCH 8/9] update package file --- Package.resolved | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Package.resolved b/Package.resolved index c6e0f7d..11ded11 100644 --- a/Package.resolved +++ b/Package.resolved @@ -9,15 +9,6 @@ "version" : "1.12.2" } }, - { - "identity" : "csqlite", - "kind" : "remoteSourceControl", - "location" : "https://github.com/stephencelis/CSQLite.git", - "state" : { - "revision" : "9106e983d5e3d5149ee35281ec089484b0def018", - "version" : "0.0.3" - } - }, { "identity" : "gandalf-ios-apollo-api", "kind" : "remoteSourceControl", From 487624603a3f7e4250667a30432c97ed1778ebc6 Mon Sep 17 00:00:00 2001 From: tosinjs Date: Thu, 4 Jul 2024 13:05:52 +0100 Subject: [PATCH 9/9] Update Readme --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58c3d6b..336c29a 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,22 @@ let services: InputData = [ "netflix": .service(Service(activities: ["watch"])), ] + +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 ) ```