From 9d4fee037b6ddcfd884a1998f368bf36e77cf239 Mon Sep 17 00:00:00 2001 From: Andrei Solovev Date: Mon, 17 Jun 2024 13:05:49 +0700 Subject: [PATCH] Refactor CardExpiryDateTextField --- .../TextFields/CardExpiryDateTextField.swift | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/OmiseSDK/Sources/Views/Components/TextFields/CardExpiryDateTextField.swift b/OmiseSDK/Sources/Views/Components/TextFields/CardExpiryDateTextField.swift index 113d48a8..09e09d95 100644 --- a/OmiseSDK/Sources/Views/Components/TextFields/CardExpiryDateTextField.swift +++ b/OmiseSDK/Sources/Views/Components/TextFields/CardExpiryDateTextField.swift @@ -175,7 +175,22 @@ public class CardExpiryDateTextField: OmiseTextField { static let monthStringRegularExpression: NSRegularExpression! = try? NSRegularExpression(pattern: "^([0-1]?\\d)", options: []) - // swiftlint:disable:next cyclomatic_complexity function_body_length + var parsedSelectedYear: Int? { + get { + return selectedYear + } + set { + guard let value = newValue else { + return + } + if value < 100 { + self.selectedYear = 2000 + value + } else { + self.selectedYear = value + } + } + } + public override func paste(_ sender: Any?) { let pasteboard = UIPasteboard.general @@ -198,39 +213,35 @@ public class CardExpiryDateTextField: OmiseTextField { defer { typingAttributes = defaultTextAttributes } - - var parsedSelectedYear: Int? { - get { - return selectedYear - } - set { - guard let value = newValue else { - return - } - if value < 100 { - self.selectedYear = 2000 + value - } else { - self.selectedYear = value - } - } + + parseCardExpiryDate(text: text) + + if let attributedText = self.attributedText.map(NSMutableAttributedString.init(attributedString:)), + let separatorTextColor = self.dateSeparatorTextColor, + let dateSeparatorIndex = attributedText.string.firstIndex(of: "/") { + let range = NSRange(dateSeparatorIndex...dateSeparatorIndex, in: attributedText.string) + attributedText.addAttribute(.foregroundColor, value: separatorTextColor, range: range) + self.attributedText = attributedText } - + } + + func parseCardExpiryDate(text: String) { if let separatorIndex = text.firstIndex(of: "/") { selectedMonth = Int(text[text.startIndex..