From a4ddd994cd65bd3f88194f97630c2ac3405fe9c0 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 24 Aug 2020 23:28:32 +0300 Subject: [PATCH] Add TISwiftUtils and code correction --- OTPSwiftView/Sources/Models/OTPCodeConfig.swift | 2 ++ .../Sources/Views/OTPSwiftView/OTPSwiftView.swift | 13 +++++++------ .../Sources/Views/OTPTextField/OTPTextField.swift | 11 ++++++----- OTPSwiftView/Sources/Views/OTPView/OTPView.swift | 1 + Package.swift | 2 ++ TISwiftUtils/README.md | 7 +++++++ .../Extensions/Optional/Optional+Extensions.swift | 6 +++--- .../Extensions/Substring/Substring+Extensions.swift | 0 .../Sources/Helpers/Typealias.swift | 1 - 9 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 TISwiftUtils/README.md rename OTPSwiftView/Sources/Extensions/UITextField/UITextField+Extensions.swift => TISwiftUtils/Sources/Extensions/Optional/Optional+Extensions.swift (92%) rename {OTPSwiftView => TISwiftUtils}/Sources/Extensions/Substring/Substring+Extensions.swift (100%) rename {OTPSwiftView => TISwiftUtils}/Sources/Helpers/Typealias.swift (96%) diff --git a/OTPSwiftView/Sources/Models/OTPCodeConfig.swift b/OTPSwiftView/Sources/Models/OTPCodeConfig.swift index f0c85782..3fa0ae67 100644 --- a/OTPSwiftView/Sources/Models/OTPCodeConfig.swift +++ b/OTPSwiftView/Sources/Models/OTPCodeConfig.swift @@ -24,6 +24,8 @@ import UIKit /// Base configuration for OTPSwiftView open class OTPCodeConfig { + public typealias Spacing = [Int: CGFloat] + public let codeSymbolsCount: Int public let spacing: CGFloat public let customSpacing: Spacing? diff --git a/OTPSwiftView/Sources/Views/OTPSwiftView/OTPSwiftView.swift b/OTPSwiftView/Sources/Views/OTPSwiftView/OTPSwiftView.swift index dd1000ac..c596d1f2 100644 --- a/OTPSwiftView/Sources/Views/OTPSwiftView/OTPSwiftView.swift +++ b/OTPSwiftView/Sources/Views/OTPSwiftView/OTPSwiftView.swift @@ -22,11 +22,12 @@ import UIKit import TIUIKitCore +import TISwiftUtils /// Base full OTP View for entering the verification code open class OTPSwiftView: BaseInitializableControl { private var emptyOTPView: View? { - textFieldsCollection.first { $0.codeTextField.unwrappedText.isEmpty } ?? textFieldsCollection.last + textFieldsCollection.first { $0.codeTextField.text.orEmpty.isEmpty } ?? textFieldsCollection.last } public private(set) var codeStackView = UIStackView() @@ -93,19 +94,19 @@ open class OTPSwiftView: BaseInitializableControl { // MARK: - Configure textfields private extension OTPSwiftView { - func configure(customSpacing: Spacing?, for stackView: UIStackView) { + func configure(customSpacing: OTPCodeConfig.Spacing?, for stackView: UIStackView) { guard let customSpacing = customSpacing else { return } - customSpacing.forEach { [weak self] viewIndex, spacing in + customSpacing.forEach { viewIndex, spacing in guard viewIndex < stackView.arrangedSubviews.count, viewIndex >= .zero else { return } - self?.set(spacing: spacing, - after: stackView.arrangedSubviews[viewIndex], - for: stackView) + self.set(spacing: spacing, + after: stackView.arrangedSubviews[viewIndex], + for: stackView) } } diff --git a/OTPSwiftView/Sources/Views/OTPTextField/OTPTextField.swift b/OTPSwiftView/Sources/Views/OTPTextField/OTPTextField.swift index 45434535..8974c6a2 100644 --- a/OTPSwiftView/Sources/Views/OTPTextField/OTPTextField.swift +++ b/OTPSwiftView/Sources/Views/OTPTextField/OTPTextField.swift @@ -21,6 +21,7 @@ // import UIKit +import TISwiftUtils /// Base one symbol textfield open class OTPTextField: UITextField { @@ -34,7 +35,7 @@ open class OTPTextField: UITextField { public var caretHeight: CGFloat? public var lastNotEmpty: OTPTextField { - let isLastNotEmpty = !unwrappedText.isEmpty && nextTextField?.unwrappedText.isEmpty ?? true + let isLastNotEmpty = !text.orEmpty.isEmpty && nextTextField?.text.orEmpty.isEmpty ?? true return isLastNotEmpty ? self : nextTextField?.lastNotEmpty ?? self } @@ -58,7 +59,7 @@ open class OTPTextField: UITextField { } open override func deleteBackward() { - guard unwrappedText.isEmpty else { + guard text.orEmpty.isEmpty else { return } @@ -102,14 +103,14 @@ extension OTPTextField: UITextFieldDelegate { return true } - let isInputEmpty = textField.unwrappedText.isEmpty && string.isEmpty + let isInputEmpty = textField.text.orEmpty.isEmpty && string.isEmpty guard isInputEmpty || validationClosure?(string) ?? true else { return false } switch range.length { - case 0: + case 0: // set text to textfield textField.set(inputText: string) let currentTextField = textField.lastNotEmpty.nextTextField ?? textField.lastNotEmpty @@ -118,7 +119,7 @@ extension OTPTextField: UITextFieldDelegate { return false - case 1: + case 1: // remove character from textfield textField.text = "" textField.onTextChangedSignal?() return false diff --git a/OTPSwiftView/Sources/Views/OTPView/OTPView.swift b/OTPSwiftView/Sources/Views/OTPView/OTPView.swift index a416b1ce..c2a29468 100644 --- a/OTPSwiftView/Sources/Views/OTPView/OTPView.swift +++ b/OTPSwiftView/Sources/Views/OTPView/OTPView.swift @@ -21,6 +21,7 @@ // import TIUIKitCore +import TISwiftUtils /// Base OTP view with textfield for entering a one symbol open class OTPView: BaseInitializableView { diff --git a/Package.swift b/Package.swift index 22b09a56..374b919e 100644 --- a/Package.swift +++ b/Package.swift @@ -9,12 +9,14 @@ let package = Package( products: [ .library(name: "TITransitions", targets: ["TITransitions"]), .library(name: "TIUIKitCore", targets: ["TIUIKitCore"]), + .library(name: "TISwiftUtils", targets: ["TISwiftUtils"]), .library(name: "TIUIElements", targets: ["TIUIElements"]), .library(name: "OTPSwiftView", targets: ["OTPSwiftView"]) ], targets: [ .target(name: "TITransitions", path: "TITransitions/Sources"), .target(name: "TIUIKitCore", path: "TIUIKitCore/Sources"), + .target(name: "TISwiftUtils", path: "TISwiftUtils/Sources"), .target(name: "TIUIElements", dependencies: ["TIUIKitCore"], path: "TIUIElements/Sources"), .target(name: "OTPSwiftView", dependencies: ["TIUIKitCore"], path: "OTPSwiftView/Sources") ] diff --git a/TISwiftUtils/README.md b/TISwiftUtils/README.md new file mode 100644 index 00000000..d135d3e7 --- /dev/null +++ b/TISwiftUtils/README.md @@ -0,0 +1,7 @@ +# TISwiftUtils + +Bunch of useful helpers for development. + +# Installation via SPM + +You can install this framework as a target of LeadKit. diff --git a/OTPSwiftView/Sources/Extensions/UITextField/UITextField+Extensions.swift b/TISwiftUtils/Sources/Extensions/Optional/Optional+Extensions.swift similarity index 92% rename from OTPSwiftView/Sources/Extensions/UITextField/UITextField+Extensions.swift rename to TISwiftUtils/Sources/Extensions/Optional/Optional+Extensions.swift index 5371dd4e..0bddd3bb 100644 --- a/OTPSwiftView/Sources/Extensions/UITextField/UITextField+Extensions.swift +++ b/TISwiftUtils/Sources/Extensions/Optional/Optional+Extensions.swift @@ -22,8 +22,8 @@ import UIKit -public extension UITextField { - var unwrappedText: String { - text ?? "" +public extension Optional where Wrapped == String { + var orEmpty: String { + self ?? "" } } diff --git a/OTPSwiftView/Sources/Extensions/Substring/Substring+Extensions.swift b/TISwiftUtils/Sources/Extensions/Substring/Substring+Extensions.swift similarity index 100% rename from OTPSwiftView/Sources/Extensions/Substring/Substring+Extensions.swift rename to TISwiftUtils/Sources/Extensions/Substring/Substring+Extensions.swift diff --git a/OTPSwiftView/Sources/Helpers/Typealias.swift b/TISwiftUtils/Sources/Helpers/Typealias.swift similarity index 96% rename from OTPSwiftView/Sources/Helpers/Typealias.swift rename to TISwiftUtils/Sources/Helpers/Typealias.swift index 4fb8cf68..cccaaac7 100644 --- a/OTPSwiftView/Sources/Helpers/Typealias.swift +++ b/TISwiftUtils/Sources/Helpers/Typealias.swift @@ -22,7 +22,6 @@ import UIKit -public typealias Spacing = [Int: CGFloat] public typealias ValueClosure = ((T) -> Void) public typealias VoidClosure = (() -> Void) public typealias ValidationClosure = ((T) -> Bool)