Skip to content

Commit

Permalink
Minor code cleanup / reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewc committed Jan 26, 2024
1 parent a72af6a commit 2326c4b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
044F26AE2B5995EE00CEFEBC /* PrimerSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 044F26AD2B5995EE00CEFEBC /* PrimerSDK */; };
0471BB072AFD1FFB000B286D /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0471BB062AFD1FFB000B286D /* ContentView.swift */; };
0479FB1B2AF270B900D0AFC9 /* PrimerDataService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0479FB1A2AF270B900D0AFC9 /* PrimerDataService.swift */; };
04819DAD2B63A8D8004BBDE1 /* PrimerCardDataErrorsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04819DAC2B63A8D8004BBDE1 /* PrimerCardDataErrorsModel.swift */; };
04AFFEE72B02797A002B2019 /* StartPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04AFFEE62B02797A002B2019 /* StartPage.swift */; };
04D781562AF517BD00A3B29B /* ImageColorInverterModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04D781552AF517BD00A3B29B /* ImageColorInverterModifier.swift */; };
04D79C5C2B59446500E6448E /* PrimerSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 04D79C5B2B59446500E6448E /* PrimerSDK */; };
Expand Down Expand Up @@ -83,6 +84,7 @@
04420E782B14A84500EA8790 /* ExampleAppLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleAppLogger.swift; sourceTree = "<group>"; };
0471BB062AFD1FFB000B286D /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
0479FB1A2AF270B900D0AFC9 /* PrimerDataService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimerDataService.swift; sourceTree = "<group>"; };
04819DAC2B63A8D8004BBDE1 /* PrimerCardDataErrorsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimerCardDataErrorsModel.swift; sourceTree = "<group>"; };
04AFFEE62B02797A002B2019 /* StartPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartPage.swift; sourceTree = "<group>"; };
04D781552AF517BD00A3B29B /* ImageColorInverterModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageColorInverterModifier.swift; sourceTree = "<group>"; };
04DAAECE2B03CA9F002E2614 /* SettingsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -278,8 +280,9 @@
isa = PBXGroup;
children = (
04FAFA2D2AE90049002E4BAE /* PrimerCardDataModel.swift */,
04DAAECE2B03CA9F002E2614 /* SettingsModel.swift */,
04819DAC2B63A8D8004BBDE1 /* PrimerCardDataErrorsModel.swift */,
04345A392B1F865C006B40CC /* PaymentResultModel.swift */,
04DAAECE2B03CA9F002E2614 /* SettingsModel.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -453,6 +456,7 @@
04345A4A2B21D6F1006B40CC /* Appearance.swift in Sources */,
04FAFA2E2AE90049002E4BAE /* PrimerCardDataModel.swift in Sources */,
04345A3E2B1F8D0E006B40CC /* Font+Monospace.swift in Sources */,
04819DAD2B63A8D8004BBDE1 /* PrimerCardDataErrorsModel.swift in Sources */,
040050C22AFE5CEB001843CA /* SettingsView.swift in Sources */,
04FAFA2A2AE81850002E4BAE /* PrimerTextField.swift in Sources */,
04345A402B208046006B40CC /* FormInfo.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// PrimerCardDataErrorsModel.swift
// Co-Badged Cards Example
//
// Created by Jack Newcombe on 26/01/2024.
//

import Foundation
import PrimerSDK

class PrimerCardDataErrorsModel: PrimerBaseCardDataModel {

override init() {
super.init()
logger.info("[PrimerCardDataErrorsModel.init]")
}

fileprivate func clearErrors() {
self.cardNumber = ""
self.expiryDate = ""
self.cvvNumber = ""
self.cardholderName = ""
self.selectedCardNetwork = .unknown
}
}

extension PrimerCardDataErrorsModel: PrimerDataServiceErrorsDelegate {
func didReceiveErrors(errors: [Error]) {
logger.info("[PrimerCardDataModel.didReceiveErrors]")
DispatchQueue.main.async {
self.clearErrors()

let validationErrors = errors.reversed().compactMap { $0 as? PrimerValidationError }

validationErrors.forEach { error in
logger.info("[PrimerCardDataModel.didReceiveErrors] error => \(error.errorId)")
switch error {
case .invalidCardnumber(let message, _, _):
self.cardNumber = message
case .invalidCardType(let message, _, _):
self.cardNumber = message // Overrides above
case .invalidExpiryDate(let message, _, _):
self.expiryDate = message
case .invalidCvv(let message, _, _):
self.cvvNumber = message
case .invalidCardholderName(let message, _, _):
self.cardholderName = message
default: break
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,43 +91,3 @@ extension PrimerCardDataModel: PrimerDataServiceModelsDelegate {
}
}
}

// MARK: Errors Model

class PrimerCardDataErrorsModel: PrimerBaseCardDataModel {

fileprivate func clearErrors() {
self.cardNumber = ""
self.expiryDate = ""
self.cvvNumber = ""
self.cardholderName = ""
self.selectedCardNetwork = .unknown
}
}

extension PrimerCardDataErrorsModel: PrimerDataServiceErrorsDelegate {
func didReceiveErrors(errors: [Error]) {
DispatchQueue.main.async {
self.clearErrors()

let validationErrors = errors.reversed().compactMap { $0 as? PrimerValidationError }

validationErrors.forEach { error in
logger.info("[PrimerCardDataModel.didReceiveErrors] => \(error.errorId)")
switch error {
case .invalidCardnumber(let message, _, _):
self.cardNumber = message
case .invalidCardType(let message, _, _):
self.cardNumber = message // Overrides above
case .invalidExpiryDate(let message, _, _):
self.expiryDate = message
case .invalidCvv(let message, _, _):
self.cvvNumber = message
case .invalidCardholderName(let message, _, _):
self.cardholderName = message
default: break
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SettingsModel: ObservableObject {
let components = token.components(separatedBy: ".")

guard components.count == 3 else {
return false // JWT should have three parts
return false
}

return components.allSatisfy { (try? /[\w\d=\+\-\/]+/.wholeMatch(in: $0)) != nil }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class PrimerDataService: NSObject {
self.clientToken = clientToken
}

// MARK: SDK Configuration

func fetchClientToken(from url: String) async throws -> String {
do {
var request = URLRequest(url: URL(string: "\(url)/client-session")!)
Expand Down

0 comments on commit 2326c4b

Please sign in to comment.