Commit 1705b739 authored by ueshiba's avatar ueshiba
Browse files

Created new util class to format the redeem codes

Refactor on RedeemViewController
parent 24684741
......@@ -241,6 +241,9 @@
0EFEB4C520077AC900F81029 /* PIATunnelProfile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EFEB4C02007784A00F81029 /* PIATunnelProfile.swift */; };
5EA54E484C7FD035341E021E /* Pods_PIALibrary_PIALibraryTests_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A22DD34A821A4EDAED288D2 /* Pods_PIALibrary_PIALibraryTests_iOS.framework */; };
841BE60D212AD0F3002EF2D1 /* ValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841BE60C212AD0F3002EF2D1 /* ValidatorTests.swift */; };
841BE60F212AFE49002EF2D1 /* GiftCardUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841BE60E212AFE49002EF2D1 /* GiftCardUtil.swift */; };
841BE610212AFE69002EF2D1 /* GiftCardUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841BE60E212AFE49002EF2D1 /* GiftCardUtil.swift */; };
841BE612212AFFA7002EF2D1 /* GiftCardUtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841BE611212AFFA7002EF2D1 /* GiftCardUtilTests.swift */; };
843C67C22122E714005A3FDA /* AccountInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 843C67C12122E714005A3FDA /* AccountInfoTests.swift */; };
843C67C32122EA13005A3FDA /* AccountInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 843C67C12122E714005A3FDA /* AccountInfoTests.swift */; };
84D5DA702126CE2900F753F8 /* QRCameraScannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D5DA6F2126CE2900F753F8 /* QRCameraScannerViewController.swift */; };
......@@ -484,6 +487,8 @@
476E8514A236C48F405ACA40 /* Pods_PIALibrary_PIALibraryHost_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PIALibrary_PIALibraryHost_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
64C04C071DE1CB44DE7945D6 /* Pods-PIALibrary-PIALibrary-iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PIALibrary-PIALibrary-iOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PIALibrary-PIALibrary-iOS/Pods-PIALibrary-PIALibrary-iOS.debug.xcconfig"; sourceTree = "<group>"; };
841BE60C212AD0F3002EF2D1 /* ValidatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatorTests.swift; sourceTree = "<group>"; };
841BE60E212AFE49002EF2D1 /* GiftCardUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiftCardUtil.swift; sourceTree = "<group>"; };
841BE611212AFFA7002EF2D1 /* GiftCardUtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiftCardUtilTests.swift; sourceTree = "<group>"; };
843C67C12122E714005A3FDA /* AccountInfoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountInfoTests.swift; sourceTree = "<group>"; };
84D5DA6F2126CE2900F753F8 /* QRCameraScannerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCameraScannerViewController.swift; sourceTree = "<group>"; };
9A22DD34A821A4EDAED288D2 /* Pods_PIALibrary_PIALibraryTests_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PIALibrary_PIALibraryTests_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
......@@ -678,6 +683,7 @@
children = (
0E4D4E9B1FA4CA7A007DA6DA /* Restylable.swift */,
0E4D4E9C1FA4CA7A007DA6DA /* Validator.swift */,
841BE60E212AFE49002EF2D1 /* GiftCardUtil.swift */,
);
path = Shared;
sourceTree = "<group>";
......@@ -920,6 +926,7 @@
0E2ADD441FE1583A00BB170C /* VPNTests.swift */,
843C67C12122E714005A3FDA /* AccountInfoTests.swift */,
841BE60C212AD0F3002EF2D1 /* ValidatorTests.swift */,
841BE611212AFFA7002EF2D1 /* GiftCardUtilTests.swift */,
0EE78B011F818A20002E4CDD /* Info-iOS.plist */,
0E2ADD201FE13B8600BB170C /* Info-macOS.plist */,
);
......@@ -1383,6 +1390,7 @@
0E7BC6F51F96B1120035C8B2 /* Credentials.swift in Sources */,
0E2ADCCB1FE06D7A00BB170C /* Array+Math.swift in Sources */,
0E7BC6EE1F96B1120035C8B2 /* AccountProvider.swift in Sources */,
841BE610212AFE69002EF2D1 /* GiftCardUtil.swift in Sources */,
0E7BC6F61F96B1120035C8B2 /* AccountInfo.swift in Sources */,
0E3D13D71F9E272B00434A48 /* GlossPayment.swift in Sources */,
0E2ADD491FE1595300BB170C /* IPSecProfile.swift in Sources */,
......@@ -1544,6 +1552,7 @@
0E492C6A1FE61485007F23DF /* Client+Database.swift in Sources */,
0E48A8531FDAD60900B9A4C0 /* OptionsViewController.swift in Sources */,
0EB8C06C1F9CD38B005857E4 /* SignupFailureViewController.swift in Sources */,
841BE60F212AFE49002EF2D1 /* GiftCardUtil.swift in Sources */,
0EB8C0681F9CD38B005857E4 /* PIAWelcomeViewController.swift in Sources */,
0E2ADD481FE1595300BB170C /* IPSecProfile.swift in Sources */,
0EFB51271F82A5F80033B81F /* Notification+Core.swift in Sources */,
......@@ -1584,6 +1593,7 @@
0EA4C4421FDDFD840041C3D8 /* ServerTests.swift in Sources */,
0EB8C0461F9CCB7C005857E4 /* AccountSignupTests.swift in Sources */,
0E2ADD451FE1583A00BB170C /* VPNTests.swift in Sources */,
841BE612212AFFA7002EF2D1 /* GiftCardUtilTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
//
// GiftCardUtil.swift
// PIALibrary-iOS
//
// Created by Jose Antonio Blaya Garcia on 20/8/18.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import Foundation
public class GiftCardUtil {
private static let rxCodeGrouping: NSRegularExpression = try! NSRegularExpression(pattern: "\\d{4}(?=\\d)", options: [])
/**
Add the stripped format to a given gift card code.
- Parameter code: The code to be formatted.
- Returns: String the code with the stripped format.
*/
public static func friendlyRedeemCode(_ code: String) -> String {
return GiftCardUtil.rxCodeGrouping.stringByReplacingMatches(in: code,
options: [],
range: NSMakeRange(0, code.count),
withTemplate: "$0-")
}
/**
Remove all stripped occurrences.
- Parameter code: The code to replace.
- Returns: String without the found occurrences.
*/
public static func strippedRedeemCode(_ code: String) -> String {
return code.replacingOccurrences(of: "-", with: "")
}
/**
Removes all whitespaces, dashes or minus characters.
- Parameter code: The code to replace.
- Returns: String without the found occurrences.
*/
public static func plainRedeemCode(_ code: String) -> String {
var formattedCode = self.strippedRedeemCode(code)
formattedCode = formattedCode.replacingOccurrences(of: "#", with: "")
formattedCode = formattedCode.components(separatedBy: .whitespacesAndNewlines).joined()
return formattedCode
}
}
......@@ -20,8 +20,6 @@ protocol RedeemScannerDelegate: class {
class RedeemViewController: AutolayoutViewController, WelcomeChild {
private static let codeInvalidSet = CharacterSet.decimalDigits.inverted
private static let rxCodeGrouping: NSRegularExpression = try! NSRegularExpression(pattern: "\\d{4}(?=\\d)", options: [])
private static let codePlaceholder = "1234-5678-9012-3456"
private static let codeLength = 16
......@@ -64,7 +62,7 @@ class RedeemViewController: AutolayoutViewController, WelcomeChild {
textCode.text = nil
return
}
textCode.text = friendlyRedeemCode(code)
textCode.text = GiftCardUtil.friendlyRedeemCode(code)
}
}
......@@ -96,7 +94,7 @@ class RedeemViewController: AutolayoutViewController, WelcomeChild {
viewLogin.accessibilityLabel = "\(labelLogin1.text!) \(labelLogin2.text!)"
textEmail.text = preset.redeemEmail
if let code = preset.redeemCode {
redeemCode = strippedRedeemCode(code) // will set textCode automatically
redeemCode = GiftCardUtil.strippedRedeemCode(code) // will set textCode automatically
}
}
......@@ -131,7 +129,7 @@ class RedeemViewController: AutolayoutViewController, WelcomeChild {
}
textEmail.text = email
textCode.text = friendlyRedeemCode(code)
textCode.text = GiftCardUtil.friendlyRedeemCode(code)
log.debug("Redeeming...")
redeemEmail = email
......@@ -183,7 +181,7 @@ class RedeemViewController: AutolayoutViewController, WelcomeChild {
metadata.title = L10n.Welcome.Redeem.title
metadata.bodySubtitle = L10n.Signup.InProgress.Redeem.message
vc.metadata = metadata
vc.redeemRequest = RedeemRequest(email: email, code: friendlyRedeemCode(code))
vc.redeemRequest = RedeemRequest(email: email, code: GiftCardUtil.friendlyRedeemCode(code))
vc.preset = preset
vc.completionDelegate = completionDelegate
case StoryboardSegue.Welcome.signupQRCameraScannerSegue.rawValue:
......@@ -218,21 +216,6 @@ class RedeemViewController: AutolayoutViewController, WelcomeChild {
buttonRedeem.startActivity()
}
}
// MARK: Helpers
private func friendlyRedeemCode(_ code: String) -> String {
return RedeemViewController.rxCodeGrouping.stringByReplacingMatches(
in: code,
options: [],
range: NSMakeRange(0, code.count),
withTemplate: "$0-"
)
}
private func strippedRedeemCode(_ code: String) -> String {
return code.replacingOccurrences(of: "-", with: "")
}
// MARK: Restylable
......@@ -277,7 +260,7 @@ extension RedeemViewController: UITextFieldDelegate {
}
let cursorLocation = textField.position(from: textField.beginningOfDocument, offset: range.location + string.count)
let newCode = strippedRedeemCode(newText)
let newCode = GiftCardUtil.strippedRedeemCode(newText)
guard newCode.count <= RedeemViewController.codeLength else {
return false
}
......@@ -299,8 +282,9 @@ extension RedeemViewController: UITextViewDelegate {
extension RedeemViewController: RedeemScannerDelegate {
func giftCardCodeFound(withCode code: String) {
if Validator.validate(giftCode: code) {
textCode.text = friendlyRedeemCode(code)
let plainRedeemCode = GiftCardUtil.plainRedeemCode(code)
if Validator.validate(giftCode: plainRedeemCode) {
textCode.text = GiftCardUtil.friendlyRedeemCode(plainRedeemCode)
} else {
presentAlertWith(title: L10n.Welcome.Redeem.Error.title,
andMessage: L10n.Welcome.Redeem.Error.Qrcode.invalid,
......
//
// GiftCardUtilTests.swift
// PIALibraryTests-iOS
//
// Created by Jose Antonio Blaya Garcia on 20/8/18.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import XCTest
@testable import PIALibrary
class GiftCardUtilTests: XCTestCase {
func testGiftCardUtilFormattedCode() {
let giftCode = "1234567812345678"
XCTAssertEqual(GiftCardUtil.friendlyRedeemCode(giftCode),
"1234-5678-1234-5678")
}
func testGiftCardUtilStrippedRedeemCode() {
let giftCode = "1234-5678-1234-5678"
XCTAssertEqual(GiftCardUtil.strippedRedeemCode(giftCode),
"1234567812345678")
}
func testFormatGiftCardCode() {
var giftCode = "1234-5678-1234-5678"
XCTAssertEqual(GiftCardUtil.plainRedeemCode(giftCode),
"1234567812345678")
giftCode = "1234#5678-1234#5678"
XCTAssertEqual(GiftCardUtil.plainRedeemCode(giftCode),
"1234567812345678")
giftCode = "1234 5678 1234 5678"
XCTAssertEqual(GiftCardUtil.plainRedeemCode(giftCode),
"1234567812345678")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment