Add TISwiftUtils and code correction

This commit is contained in:
Vlad 2020-08-24 23:28:32 +03:00
parent cba4833d46
commit a4ddd994cd
9 changed files with 28 additions and 15 deletions

View File

@ -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?

View File

@ -22,11 +22,12 @@
import UIKit
import TIUIKitCore
import TISwiftUtils
/// Base full OTP View for entering the verification code
open class OTPSwiftView<View: OTPView>: 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<View: OTPView>: 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)
}
}

View File

@ -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

View File

@ -21,6 +21,7 @@
//
import TIUIKitCore
import TISwiftUtils
/// Base OTP view with textfield for entering a one symbol
open class OTPView: BaseInitializableView {

View File

@ -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")
]

7
TISwiftUtils/README.md Normal file
View File

@ -0,0 +1,7 @@
# TISwiftUtils
Bunch of useful helpers for development.
# Installation via SPM
You can install this framework as a target of LeadKit.

View File

@ -22,8 +22,8 @@
import UIKit
public extension UITextField {
var unwrappedText: String {
text ?? ""
public extension Optional where Wrapped == String {
var orEmpty: String {
self ?? ""
}
}

View File

@ -22,7 +22,6 @@
import UIKit
public typealias Spacing = [Int: CGFloat]
public typealias ValueClosure<T> = ((T) -> Void)
public typealias VoidClosure = (() -> Void)
public typealias ValidationClosure<T> = ((T) -> Bool)